Skip to content

Commit

Permalink
feat(vote): rating update on torrent request
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Jun 16, 2018
1 parent 605672b commit 8c28728
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/background/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = async ({
crypto,
insertTorrentToDB,
removeTorrentFromDB,
updateTorrentToDB,
checkTorrent,
p2pStore,
feed
Expand Down Expand Up @@ -141,6 +142,13 @@ module.exports = async ({
send('votes', {
hash, good, bad, selfVote
});
if(torrent.good != good || torrent.bad != bad)
{
console.log('finded new rating on', torrent.name, 'update votes to it')
torrent.good = good
torrent.bad = bad
updateTorrentToDB(torrent)
}
});
}

Expand Down Expand Up @@ -760,6 +768,8 @@ module.exports = async ({
let {good, bad} = await getVotes(torrent.hash)
torrent.good = good
torrent.bad = bad
if(torrent.good > 0 || torrent.bad > 0)
updateTorrentToDB(torrent)

feed.add(torrent)

Expand Down
40 changes: 40 additions & 0 deletions src/background/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,46 @@ const expand = (sphinx) => {
})
})

sphinx.updateValues = (table, values, whereObject, callback) => new Promise((resolve) => {
let set = ''
for(const val in values)
{
if(values[val] === null)
continue;

if(typeof values[val] == 'object')
continue;

// skip text indexes (manticore bug https://github.com/manticoresoftware/manticoresearch/issues/84)
if(typeof values[val] == 'string')
continue;

set += '`' + val + '` = ' + sphinx.escape(values[val]) + ',';
}
if(set.length == 0)
return
set = set.slice(0, -1)

let where = ''
for(const w in whereObject)
{
if(whereObject[w] === null)
continue;

where += '`' + w + '` = ' + sphinx.escape(whereObject[w]) + ' and';
}
if(where.length == 0)
return
where = where.slice(0, -3)

const query = `UPDATE ${table} SET ${set} WHERE ${where}`;
queryCall(query, (...responce) => {
if(callback)
callback(...responce)
resolve(...responce)
})
})

return sphinx
}

Expand Down
16 changes: 13 additions & 3 deletions src/background/spider.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,19 @@ const insertTorrentToDB = (torrent, silent) => {
})
}

const removeTorrentFromDB = (torrent) => {
const removeTorrentFromDB = async (torrent) => {
const {hash} = torrent
mysqlSingle.query('DELETE FROM torrents WHERE hash = ?', hash)
mysqlSingle.query('DELETE FROM files WHERE hash = ?', hash)
await mysqlSingle.query('DELETE FROM torrents WHERE hash = ?', hash)
await mysqlSingle.query('DELETE FROM files WHERE hash = ?', hash)
}

const updateTorrentToDB = async (torrent) => {
if(typeof torrent !== 'object')
return

delete torrent.id

await mysqlSingle.updateValues('torrents', torrent, {hash: torrent.hash})
}

const updateTorrent = (metadata, infohash, rinfo) => {
Expand Down Expand Up @@ -718,6 +727,7 @@ API({
crypto,
insertTorrentToDB,
removeTorrentFromDB,
updateTorrentToDB,
checkTorrent,
p2pStore,
feed
Expand Down

0 comments on commit 8c28728

Please sign in to comment.