refactor: dont save partial searches

isekai-main
Barış Soner Uşaklı 3 years ago
parent 25ebbd6563
commit c7e078d495

@ -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);
}
}
}

Loading…
Cancel
Save