Skip to content

Commit

Permalink
feat(p2p): p2p feed
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Jun 16, 2018
1 parent 8c28728 commit e368910
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
48 changes: 21 additions & 27 deletions src/background/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -747,39 +733,47 @@ module.exports = async ({

// store torrent to feed
await feed.load()
p2pStore.on('store', async ({data: record, temp}) => {
if(!temp || !temp.torrent)
return

const { torrent } = temp

p2pStore.on('store', async ({data: record, temp, myself}) => {
if(record.type !== 'vote')
return

if(record.vote !== 'good')
if(!temp || !temp.torrent)
return

const { torrent } = temp

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))
})
}
11 changes: 9 additions & 2 deletions src/background/spider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -450,6 +453,7 @@ const insertTorrentToDB = (torrent, silent) => {

if(!checkTorrent(torrent))
{
resolve()
return
}

Expand All @@ -462,11 +466,13 @@ const insertTorrentToDB = (torrent, silent) => {
if(!single)
{
console.log(err)
resolve()
return
}

if(single.length > 0)
{
resolve()
return
}

Expand All @@ -491,6 +497,7 @@ const insertTorrentToDB = (torrent, silent) => {
console.log(torrent);
console.error(err);
}
resolve()
});
})

Expand Down Expand Up @@ -526,7 +533,7 @@ const insertTorrentToDB = (torrent, silent) => {
})
}
})
}
})

const removeTorrentFromDB = async (torrent) => {
const {hash} = torrent
Expand Down

0 comments on commit e368910

Please sign in to comment.