From dd8dc7b4189cb46122fd6fa26c033418387425a6 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Sat, 16 Jun 2018 16:25:07 +0300 Subject: [PATCH] feat(p2p): p2p feed --- src/background/api.js | 48 ++++++++++++++++++---------------------- src/background/spider.js | 11 +++++++-- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/background/api.js b/src/background/api.js index d918221..06d5d70 100644 --- a/src/background/api.js +++ b/src/background/api.js @@ -696,20 +696,6 @@ module.exports = async ({ return {good, bad, selfVote} } - p2pStore.on('store', (value) => { - if(!value.temp) - return - - if(!value.temp.torrent) - return - - if(value.myself) - return - - console.log('replicate torrent from store record', value.temp.torrent.hash) - insertTorrentToDB(value.temp.torrent) - }) - recive('vote', async (hash, isGood, callback) => { if(hash.length != 40) @@ -747,39 +733,47 @@ module.exports = async ({ // store torrent to feed await feed.load() - p2pStore.on('store', async ({data: record, temp}) => { + p2pStore.on('store', async ({data: record, temp, myself}) => { + if(record.type !== 'vote') + return + if(!temp || !temp.torrent) return const { torrent } = temp - - if(record.type !== 'vote') - return - - if(record.vote !== 'good') - return - if(!torrent) - return - if(torrent.hash !== record.torrentHash) return + if(!myself) + { + console.log('replicate torrent from store record', torrent.hash) + await insertTorrentToDB(torrent) + } + let {good, bad} = await getVotes(torrent.hash) torrent.good = good torrent.bad = bad if(torrent.good > 0 || torrent.bad > 0) updateTorrentToDB(torrent) + // update feed + if(record.vote !== 'good') + return + feed.add(torrent) - send('feedUpdate', { feed: feed.feed }); }) - recive('feed', (callback) => + const feedCall = (callback) => { callback(feed.feed) - }); + } + recive('feed', feedCall); + + p2p.on('feed', ({}, callback) => { + feedCall((data) => callback(data)) + }) } \ No newline at end of file diff --git a/src/background/spider.js b/src/background/spider.js index 3602d8a..04cab7e 100644 --- a/src/background/spider.js +++ b/src/background/spider.js @@ -432,9 +432,12 @@ const checkTorrent = (torrent) => { return true } -const insertTorrentToDB = (torrent, silent) => { +const insertTorrentToDB = (torrent, silent) => new Promise((resolve) => { if(!torrent) + { + resolve() return + } // fix cases for low cases letters if(torrent.contentcategory) @@ -450,6 +453,7 @@ const insertTorrentToDB = (torrent, silent) => { if(!checkTorrent(torrent)) { + resolve() return } @@ -462,11 +466,13 @@ const insertTorrentToDB = (torrent, silent) => { if(!single) { console.log(err) + resolve() return } if(single.length > 0) { + resolve() return } @@ -491,6 +497,7 @@ const insertTorrentToDB = (torrent, silent) => { console.log(torrent); console.error(err); } + resolve() }); }) @@ -526,7 +533,7 @@ const insertTorrentToDB = (torrent, silent) => { }) } }) -} +}) const removeTorrentFromDB = async (torrent) => { const {hash} = torrent