feat(p2p): p2p feed

This commit is contained in:
Alexey Kasyanchuk 2018-06-16 16:25:07 +03:00
parent 51caccbce4
commit dd8dc7b418
2 changed files with 30 additions and 29 deletions

View File

@ -696,20 +696,6 @@ module.exports = async ({
return {good, bad, selfVote} 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) => recive('vote', async (hash, isGood, callback) =>
{ {
if(hash.length != 40) if(hash.length != 40)
@ -747,39 +733,47 @@ module.exports = async ({
// store torrent to feed // store torrent to feed
await feed.load() 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) if(!temp || !temp.torrent)
return return
const { torrent } = temp const { torrent } = temp
if(record.type !== 'vote')
return
if(record.vote !== 'good')
return
if(!torrent)
return
if(torrent.hash !== record.torrentHash) if(torrent.hash !== record.torrentHash)
return return
if(!myself)
{
console.log('replicate torrent from store record', torrent.hash)
await insertTorrentToDB(torrent)
}
let {good, bad} = await getVotes(torrent.hash) let {good, bad} = await getVotes(torrent.hash)
torrent.good = good torrent.good = good
torrent.bad = bad torrent.bad = bad
if(torrent.good > 0 || torrent.bad > 0) if(torrent.good > 0 || torrent.bad > 0)
updateTorrentToDB(torrent) updateTorrentToDB(torrent)
// update feed
if(record.vote !== 'good')
return
feed.add(torrent) feed.add(torrent)
send('feedUpdate', { send('feedUpdate', {
feed: feed.feed feed: feed.feed
}); });
}) })
recive('feed', (callback) => const feedCall = (callback) =>
{ {
callback(feed.feed) callback(feed.feed)
}); }
recive('feed', feedCall);
p2p.on('feed', ({}, callback) => {
feedCall((data) => callback(data))
})
} }

View File

@ -432,9 +432,12 @@ const checkTorrent = (torrent) => {
return true return true
} }
const insertTorrentToDB = (torrent, silent) => { const insertTorrentToDB = (torrent, silent) => new Promise((resolve) => {
if(!torrent) if(!torrent)
{
resolve()
return return
}
// fix cases for low cases letters // fix cases for low cases letters
if(torrent.contentcategory) if(torrent.contentcategory)
@ -450,6 +453,7 @@ const insertTorrentToDB = (torrent, silent) => {
if(!checkTorrent(torrent)) if(!checkTorrent(torrent))
{ {
resolve()
return return
} }
@ -462,11 +466,13 @@ const insertTorrentToDB = (torrent, silent) => {
if(!single) if(!single)
{ {
console.log(err) console.log(err)
resolve()
return return
} }
if(single.length > 0) if(single.length > 0)
{ {
resolve()
return return
} }
@ -491,6 +497,7 @@ const insertTorrentToDB = (torrent, silent) => {
console.log(torrent); console.log(torrent);
console.error(err); console.error(err);
} }
resolve()
}); });
}) })
@ -526,7 +533,7 @@ const insertTorrentToDB = (torrent, silent) => {
}) })
} }
}) })
} })
const removeTorrentFromDB = async (torrent) => { const removeTorrentFromDB = async (torrent) => {
const {hash} = torrent const {hash} = torrent