feat(p2p): p2p feed
This commit is contained in:
parent
51caccbce4
commit
dd8dc7b418
@ -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))
|
||||
})
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user