From c8e37ae4b2208e1635dc14ee700767e162fd356a Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Fri, 13 Apr 2018 15:56:45 +0300 Subject: [PATCH] feat(vote): restored voting (now working over p2p) --- src/background/api.js | 31 ++++++++++++++++++++----------- src/background/config.js | 3 ++- src/background/p2p.js | 6 ++++++ src/background/sphinx.js | 1 + src/background/store.js | 14 +++++++++----- 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/background/api.js b/src/background/api.js index 98e2580..3083d00 100644 --- a/src/background/api.js +++ b/src/background/api.js @@ -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 + } - p2pStore.store({ - type: 'vote', - torrentHash: hash, - vote: action, - _index: `vote:${hash}` - }) send('vote', { hash, good, bad }); diff --git a/src/background/config.js b/src/background/config.js index 8554b96..0e2d654 100644 --- a/src/background/config.js +++ b/src/background/config.js @@ -8,7 +8,8 @@ let config = { httpPort: 8095, spiderPort: 4445, udpTrackersPort: 4446, - udpTrackersTimeout: 3 * 60 * 1000, + udpTrackersTimeout: 3 * 60 * 1000, + peerId: undefined, p2p: true, p2pConnections: 10, diff --git a/src/background/p2p.js b/src/background/p2p.js index a62bb99..f14d36b 100644 --- a/src/background/p2p.js +++ b/src/background/p2p.js @@ -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(); diff --git a/src/background/sphinx.js b/src/background/sphinx.js index 6c248c3..0b6417e 100644 --- a/src/background/sphinx.js +++ b/src/background/sphinx.js @@ -63,6 +63,7 @@ const writeSphinxConfig = (path, dbPath) => { rt_field = storeIndex rt_attr_json = data rt_attr_string = hash + rt_attr_string = peerId } searchd diff --git a/src/background/store.js b/src/background/store.js index a8e9c3f..b6cc40b 100644 --- a/src/background/store.js +++ b/src/background/store.js @@ -14,7 +14,9 @@ module.exports = class P2PStore { return 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) => { @@ -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 }) )) }) }) }