From 9d684b89a697b51c126263b8ad1f800636890ab8 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Mon, 22 Feb 2021 01:40:35 +0300 Subject: [PATCH] fix(search): improve search on magnet queries --- src/background/api.js | 42 ++++++++++++++++++++++++++++++++---------- tests/details.test.js | 2 +- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/background/api.js b/src/background/api.js index ebc6491..dfc895b 100644 --- a/src/background/api.js +++ b/src/background/api.js @@ -278,6 +278,7 @@ module.exports = async ({ return /[0-9a-f]+/i.test(hash) } + let dhtCheckTimeout; const searchTorrentCall = function(text, navigation, callback, isP2P) { if(typeof callback != 'function') @@ -328,6 +329,11 @@ module.exports = async ({ where += ' and files > ' + sphinx.escape(navigation.files.min) + ' '; } + if (dhtCheckTimeout) { + clearTimeout(dhtCheckTimeout); + dhtCheckTimeout = null; + } + let searchList = []; const isSHA1 = isSH1Hash(text) sphinx.query('SELECT * FROM `torrents` WHERE ' + (isSHA1 ? 'hash = ?' : 'MATCH(?)') + ' ' + where + ' ' + order + ' LIMIT ?,?', args, function (error, rows, fields) { @@ -339,6 +345,31 @@ module.exports = async ({ if(rows.length === 0 && isSHA1 && !isP2P) // trying to get via dht { logT('search', 'get torrent via infohash with dht') + // 3 try to get torrent from metadata + const getTorrentMetadata = (tryCount = 4) => { + if(tryCount <= 0) { + logT('search', 'dht NOT found anything with dht', text); + return + } + let lock = false; + dhtCheckTimeout = setTimeout(() => { + lock = true + getTorrentMetadata(--tryCount) + }, 8000); + torrentClient.getMetadata(text, (torrent) => { + if(lock) { + logT('search', 'this dht response not actual for', text); + return + } + clearTimeout(dhtCheckTimeout); + dhtCheckTimeout = null; + logT('search', 'dht search found something on try', tryCount) + searchList.push(baseRowData(torrent)); + callback(searchList); + }) + } + getTorrentMetadata(); + // double check torrent magnet let secondTry = false; const doubleCheckTimeout = setTimeout(() => { @@ -350,16 +381,7 @@ module.exports = async ({ callback(searchList); }) }, 8000) - torrentClient.getMetadata(text, (torrent) => { - clearTimeout(doubleCheckTimeout); - if(secondTry) { - logT('search', 'ignore search dht resopond because of second try') - return - } - logT('search', 'dht search found something') - searchList.push(baseRowData(torrent)); - callback(searchList); - }) + } else { diff --git a/tests/details.test.js b/tests/details.test.js index f4e5e3a..f1d00ab 100644 --- a/tests/details.test.js +++ b/tests/details.test.js @@ -5,7 +5,7 @@ const config = require('../src/background/config') const fs = require('fs') describe("details", function() { - this.timeout(30000); + this.timeout(60000); it("open details", async function() {