Node.js based forum software built for the modern web
refactor: dont save partial searches
Barış Soner Uşaklıc7e078d4950e
diff --git a/src/controllers/search.js b/src/controllers/search.jsindex 76a34d1e39..004fe67e3b 100644--- a/src/controllers/search.js+++ b/src/controllers/search.js@@ -97,12 +97,26 @@ searchController.search = async function (req, res, next) {res.render('search', searchData);};+const searches = {};+async function recordSearch(data) {const { query, searchIn } = data;if (query) {const cleanedQuery = String(query).trim().toLowerCase().substr(0, 255);if (['titles', 'titlesposts', 'posts'].includes(searchIn) && cleanedQuery.length > 2) {- await db.sortedSetIncrBy('searches:all', 1, cleanedQuery);+ searches[data.uid] = searches[data.uid] || { timeoutId: 0, queries: [] };+ searches[data.uid].queries.push(cleanedQuery);+ if (searches[data.uid].timeoutId) {+ clearTimeout(searches[data.uid].timeoutId);+ }+ searches[data.uid].timeoutId = setTimeout(async () => {+ const copy = searches[data.uid].queries.slice();+ const filtered = searches[data.uid].queries.filter(+ q => !copy.find(query => query.startsWith(q) && query.length > q.length)+ );+ await Promise.all(filtered.map(query => db.sortedSetIncrBy('searches:all', 1, query)));+ delete searches[data.uid];+ }, 5000);}}}