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 votes = await p2pStore.find(`vote:${hash}`)
|
||||
let good = isGood ? 1 : 0
|
||||
let bad = !isGood ? 1 : 0
|
||||
let good = 0
|
||||
let bad = 0
|
||||
let selfVote = false
|
||||
if(votes)
|
||||
{
|
||||
console.log(votes)
|
||||
votes.forEach(({vote}) => {
|
||||
votes.forEach(({vote, _peerId}) => {
|
||||
if(_peerId === p2p.peerId)
|
||||
selfVote = true
|
||||
|
||||
if(vote == 'bad')
|
||||
bad++
|
||||
else
|
||||
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
|
||||
}
|
||||
|
||||
send('vote', {
|
||||
hash, good, bad
|
||||
});
|
||||
|
@ -9,6 +9,7 @@ let config = {
|
||||
spiderPort: 4445,
|
||||
udpTrackersPort: 4446,
|
||||
udpTrackersTimeout: 3 * 60 * 1000,
|
||||
peerId: undefined,
|
||||
|
||||
p2p: true,
|
||||
p2pConnections: 10,
|
||||
|
@ -17,6 +17,12 @@ class p2p {
|
||||
this.p2pStatus = 0
|
||||
this.version = '0'
|
||||
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.tcpServer = net.createServer();
|
||||
|
@ -63,6 +63,7 @@ const writeSphinxConfig = (path, dbPath) => {
|
||||
rt_field = storeIndex
|
||||
rt_attr_json = data
|
||||
rt_attr_string = hash
|
||||
rt_attr_string = peerId
|
||||
}
|
||||
|
||||
searchd
|
||||
|
@ -15,6 +15,8 @@ module.exports = class P2PStore {
|
||||
|
||||
if(rows[0] && rows[0].mx >= 1)
|
||||
this.id = rows[0].mx + 1;
|
||||
|
||||
console.log('store db index', this.id)
|
||||
})
|
||||
|
||||
this.p2p.on('dbStore', (record, callback) => {
|
||||
@ -79,8 +81,8 @@ module.exports = class P2PStore {
|
||||
{
|
||||
const data = this.sphinx.escape(JSON.stringify(value.data))
|
||||
this.sphinx.query(
|
||||
`insert into store(id, hash, data` + (value.index ? ', storeIndex' : '') + `)
|
||||
values('${value.id}', '${value.hash}', ${data}` + (value.index ? ',' + this.sphinx.escape(value.index) : '') + ')',
|
||||
`insert into store(id, hash, peerId, data` + (value.index ? ', storeIndex' : '') + `)
|
||||
values('${value.id}', '${value.hash}', '${value.peerId}', ${data}` + (value.index ? ',' + this.sphinx.escape(value.index) : '') + ')',
|
||||
(err) => {
|
||||
if(err)
|
||||
{
|
||||
@ -99,8 +101,10 @@ module.exports = class P2PStore {
|
||||
id: this.id++,
|
||||
hash: objectHash(obj),
|
||||
data: obj,
|
||||
index: obj._index
|
||||
index: obj._index,
|
||||
peerId: this.p2p.peerId
|
||||
}
|
||||
console.log('store object', value.id)
|
||||
|
||||
this._pushToDb(value, () => {
|
||||
// store record
|
||||
@ -119,7 +123,7 @@ module.exports = class P2PStore {
|
||||
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