feat(vote): restored voting (now working over p2p)
This commit is contained in:
@ -643,26 +643,35 @@ module.exports = ({
|
|||||||
const action = isGood ? 'good' : 'bad';
|
const action = isGood ? 'good' : 'bad';
|
||||||
|
|
||||||
const votes = await p2pStore.find(`vote:${hash}`)
|
const votes = await p2pStore.find(`vote:${hash}`)
|
||||||
let good = isGood ? 1 : 0
|
let good = 0
|
||||||
let bad = !isGood ? 1 : 0
|
let bad = 0
|
||||||
|
let selfVote = false
|
||||||
if(votes)
|
if(votes)
|
||||||
{
|
{
|
||||||
console.log(votes)
|
votes.forEach(({vote, _peerId}) => {
|
||||||
votes.forEach(({vote}) => {
|
if(_peerId === p2p.peerId)
|
||||||
|
selfVote = true
|
||||||
|
|
||||||
if(vote == 'bad')
|
if(vote == 'bad')
|
||||||
bad++
|
bad++
|
||||||
else
|
else
|
||||||
good++
|
good++
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
console.log(bad, good)
|
console.log('votes before', bad, good)
|
||||||
|
|
||||||
|
if(!selfVote)
|
||||||
|
{
|
||||||
|
p2pStore.store({
|
||||||
|
type: 'vote',
|
||||||
|
torrentHash: hash,
|
||||||
|
vote: action,
|
||||||
|
_index: `vote:${hash}`
|
||||||
|
})
|
||||||
|
good += isGood ? 1 : 0
|
||||||
|
bad += !isGood ? 1 : 0
|
||||||
|
}
|
||||||
|
|
||||||
p2pStore.store({
|
|
||||||
type: 'vote',
|
|
||||||
torrentHash: hash,
|
|
||||||
vote: action,
|
|
||||||
_index: `vote:${hash}`
|
|
||||||
})
|
|
||||||
send('vote', {
|
send('vote', {
|
||||||
hash, good, bad
|
hash, good, bad
|
||||||
});
|
});
|
||||||
|
@ -8,7 +8,8 @@ let config = {
|
|||||||
httpPort: 8095,
|
httpPort: 8095,
|
||||||
spiderPort: 4445,
|
spiderPort: 4445,
|
||||||
udpTrackersPort: 4446,
|
udpTrackersPort: 4446,
|
||||||
udpTrackersTimeout: 3 * 60 * 1000,
|
udpTrackersTimeout: 3 * 60 * 1000,
|
||||||
|
peerId: undefined,
|
||||||
|
|
||||||
p2p: true,
|
p2p: true,
|
||||||
p2pConnections: 10,
|
p2pConnections: 10,
|
||||||
|
@ -17,6 +17,12 @@ class p2p {
|
|||||||
this.p2pStatus = 0
|
this.p2pStatus = 0
|
||||||
this.version = '0'
|
this.version = '0'
|
||||||
this.info = {}
|
this.info = {}
|
||||||
|
if(!config.peerId)
|
||||||
|
{
|
||||||
|
console.log('generate peerId')
|
||||||
|
config.peerId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
|
||||||
|
}
|
||||||
|
this.peerId = config.peerId;
|
||||||
|
|
||||||
this.send = send
|
this.send = send
|
||||||
this.tcpServer = net.createServer();
|
this.tcpServer = net.createServer();
|
||||||
|
@ -63,6 +63,7 @@ const writeSphinxConfig = (path, dbPath) => {
|
|||||||
rt_field = storeIndex
|
rt_field = storeIndex
|
||||||
rt_attr_json = data
|
rt_attr_json = data
|
||||||
rt_attr_string = hash
|
rt_attr_string = hash
|
||||||
|
rt_attr_string = peerId
|
||||||
}
|
}
|
||||||
|
|
||||||
searchd
|
searchd
|
||||||
|
@ -14,7 +14,9 @@ module.exports = class P2PStore {
|
|||||||
return
|
return
|
||||||
|
|
||||||
if(rows[0] && rows[0].mx >= 1)
|
if(rows[0] && rows[0].mx >= 1)
|
||||||
this.id = rows[0].mx + 1;
|
this.id = rows[0].mx + 1;
|
||||||
|
|
||||||
|
console.log('store db index', this.id)
|
||||||
})
|
})
|
||||||
|
|
||||||
this.p2p.on('dbStore', (record, callback) => {
|
this.p2p.on('dbStore', (record, callback) => {
|
||||||
@ -79,8 +81,8 @@ module.exports = class P2PStore {
|
|||||||
{
|
{
|
||||||
const data = this.sphinx.escape(JSON.stringify(value.data))
|
const data = this.sphinx.escape(JSON.stringify(value.data))
|
||||||
this.sphinx.query(
|
this.sphinx.query(
|
||||||
`insert into store(id, hash, data` + (value.index ? ', storeIndex' : '') + `)
|
`insert into store(id, hash, peerId, data` + (value.index ? ', storeIndex' : '') + `)
|
||||||
values('${value.id}', '${value.hash}', ${data}` + (value.index ? ',' + this.sphinx.escape(value.index) : '') + ')',
|
values('${value.id}', '${value.hash}', '${value.peerId}', ${data}` + (value.index ? ',' + this.sphinx.escape(value.index) : '') + ')',
|
||||||
(err) => {
|
(err) => {
|
||||||
if(err)
|
if(err)
|
||||||
{
|
{
|
||||||
@ -99,8 +101,10 @@ module.exports = class P2PStore {
|
|||||||
id: this.id++,
|
id: this.id++,
|
||||||
hash: objectHash(obj),
|
hash: objectHash(obj),
|
||||||
data: obj,
|
data: obj,
|
||||||
index: obj._index
|
index: obj._index,
|
||||||
|
peerId: this.p2p.peerId
|
||||||
}
|
}
|
||||||
|
console.log('store object', value.id)
|
||||||
|
|
||||||
this._pushToDb(value, () => {
|
this._pushToDb(value, () => {
|
||||||
// store record
|
// store record
|
||||||
@ -119,7 +123,7 @@ module.exports = class P2PStore {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve(records.map(({data}) => JSON.parse(data)))
|
resolve(records.map( ({data, peerid}) => Object.assign(JSON.parse(data), { _peerId: peerid }) ))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user