diff --git a/src/background/api.js b/src/background/api.js index 160de460..d9182219 100644 --- a/src/background/api.js +++ b/src/background/api.js @@ -17,6 +17,7 @@ module.exports = async ({ crypto, insertTorrentToDB, removeTorrentFromDB, + updateTorrentToDB, checkTorrent, p2pStore, feed @@ -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) + } }); } @@ -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) diff --git a/src/background/mysql.js b/src/background/mysql.js index f01abccf..4e8be5da 100644 --- a/src/background/mysql.js +++ b/src/background/mysql.js @@ -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 } diff --git a/src/background/spider.js b/src/background/spider.js index 77589627..3602d8ac 100644 --- a/src/background/spider.js +++ b/src/background/spider.js @@ -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) => { @@ -718,6 +727,7 @@ API({ crypto, insertTorrentToDB, removeTorrentFromDB, + updateTorrentToDB, checkTorrent, p2pStore, feed