From b51b6087a6fcf5783ad3ea62b2899ba3565522fe Mon Sep 17 00:00:00 2001 From: NickDevi Date: Thu, 5 Sep 2024 18:02:27 +0300 Subject: [PATCH 01/12] ReducingDepthToThree --- src/upgrades/1.0.0/chat_room_hashes.js | 54 +++++++++++++++----------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/upgrades/1.0.0/chat_room_hashes.js b/src/upgrades/1.0.0/chat_room_hashes.js index 78de201a85..49bf09c4ab 100644 --- a/src/upgrades/1.0.0/chat_room_hashes.js +++ b/src/upgrades/1.0.0/chat_room_hashes.js @@ -3,7 +3,6 @@ const async = require('async'); const db = require('../../database'); - module.exports = { name: 'Chat room hashes', timestamp: Date.UTC(2015, 11, 23), @@ -12,28 +11,37 @@ module.exports = { if (err) { return callback(err); } - let currentChatRoomId = 1; - async.whilst((next) => { - next(null, currentChatRoomId <= nextChatRoomId); - }, (next) => { - db.getSortedSetRange(`chat:room:${currentChatRoomId}:uids`, 0, 0, (err, uids) => { - if (err) { - return next(err); - } - if (!Array.isArray(uids) || !uids.length || !uids[0]) { - currentChatRoomId += 1; - return next(); - } - - db.setObject(`chat:room:${currentChatRoomId}`, { owner: uids[0], roomId: currentChatRoomId }, (err) => { - if (err) { - return next(err); - } - currentChatRoomId += 1; - next(); - }); - }); - }, callback); + processChatRooms(1, nextChatRoomId, callback); }); }, }; + +function processChatRooms(currentChatRoomId, nextChatRoomId, callback) { + async.whilst( + (next) => next(null, currentChatRoomId <= nextChatRoomId), + (next) => processSingleChatRoom(currentChatRoomId++, next), + callback + ); +} + +function processSingleChatRoom(chatRoomId, next) { + getFirstUserFromChatRoom(chatRoomId, (err, firstUser) => { + if (err || !firstUser) { + return next(err); // Continue on error or if no user found + } + setChatRoomOwner(chatRoomId, firstUser, next); + }); +} + +function getFirstUserFromChatRoom(chatRoomId, callback) { + db.getSortedSetRange(`chat:room:${chatRoomId}:uids`, 0, 0, (err, uids) => { + if (err) { + return callback(err); + } + callback(null, Array.isArray(uids) && uids.length && uids[0] ? uids[0] : null); + }); +} + +function setChatRoomOwner(chatRoomId, ownerId, callback) { + db.setObject(`chat:room:${chatRoomId}`, { owner: ownerId, roomId: chatRoomId }, callback); +} From 23b6d442e020d26b21a76f4fd1ce39eab25ac1e5 Mon Sep 17 00:00:00 2001 From: NickDevi Date: Thu, 5 Sep 2024 18:03:32 +0300 Subject: [PATCH 02/12] FixedSyntaxErrors --- src/upgrades/1.0.0/chat_room_hashes.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/upgrades/1.0.0/chat_room_hashes.js b/src/upgrades/1.0.0/chat_room_hashes.js index 49bf09c4ab..e5b4a1cab6 100644 --- a/src/upgrades/1.0.0/chat_room_hashes.js +++ b/src/upgrades/1.0.0/chat_room_hashes.js @@ -18,8 +18,13 @@ module.exports = { function processChatRooms(currentChatRoomId, nextChatRoomId, callback) { async.whilst( - (next) => next(null, currentChatRoomId <= nextChatRoomId), - (next) => processSingleChatRoom(currentChatRoomId++, next), + next => next(null, currentChatRoomId <= nextChatRoomId), + next => { + processSingleChatRoom(currentChatRoomId, () => { + currentChatRoomId += 1; // Replacing unary operator '++' + next(); + }); + }, callback ); } From bc332d087710e477fd81f0120073efb867f8366f Mon Sep 17 00:00:00 2001 From: NickDevi Date: Thu, 5 Sep 2024 18:06:16 +0300 Subject: [PATCH 03/12] FixedSyntax --- src/upgrades/1.0.0/chat_room_hashes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/upgrades/1.0.0/chat_room_hashes.js b/src/upgrades/1.0.0/chat_room_hashes.js index e5b4a1cab6..eb613ce69c 100644 --- a/src/upgrades/1.0.0/chat_room_hashes.js +++ b/src/upgrades/1.0.0/chat_room_hashes.js @@ -19,7 +19,7 @@ module.exports = { function processChatRooms(currentChatRoomId, nextChatRoomId, callback) { async.whilst( next => next(null, currentChatRoomId <= nextChatRoomId), - next => { + (next) => { // Added parentheses around (next) processSingleChatRoom(currentChatRoomId, () => { currentChatRoomId += 1; // Replacing unary operator '++' next(); From 81d4dfed37adbabb2fd91dd9a7ba9bfda6ade3a3 Mon Sep 17 00:00:00 2001 From: NickDevi Date: Thu, 5 Sep 2024 18:15:32 +0300 Subject: [PATCH 04/12] console-log-messages-written --- src/upgrades/1.0.0/chat_room_hashes.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/upgrades/1.0.0/chat_room_hashes.js b/src/upgrades/1.0.0/chat_room_hashes.js index eb613ce69c..0212bee603 100644 --- a/src/upgrades/1.0.0/chat_room_hashes.js +++ b/src/upgrades/1.0.0/chat_room_hashes.js @@ -17,6 +17,7 @@ module.exports = { }; function processChatRooms(currentChatRoomId, nextChatRoomId, callback) { + console.log(">>>processChatRooms accessed"); async.whilst( next => next(null, currentChatRoomId <= nextChatRoomId), (next) => { // Added parentheses around (next) @@ -30,6 +31,7 @@ function processChatRooms(currentChatRoomId, nextChatRoomId, callback) { } function processSingleChatRoom(chatRoomId, next) { + console.log(">>>processSingleChatRoom accessed"); getFirstUserFromChatRoom(chatRoomId, (err, firstUser) => { if (err || !firstUser) { return next(err); // Continue on error or if no user found @@ -39,6 +41,7 @@ function processSingleChatRoom(chatRoomId, next) { } function getFirstUserFromChatRoom(chatRoomId, callback) { + console.log(">>>getFirst accessed"); db.getSortedSetRange(`chat:room:${chatRoomId}:uids`, 0, 0, (err, uids) => { if (err) { return callback(err); @@ -48,5 +51,6 @@ function getFirstUserFromChatRoom(chatRoomId, callback) { } function setChatRoomOwner(chatRoomId, ownerId, callback) { + console.log(">>>setChatRoomOwner accessed"); db.setObject(`chat:room:${chatRoomId}`, { owner: ownerId, roomId: chatRoomId }, callback); } From 7a78e6f20c8d86db704e3c8a2a4acd330ac5d58c Mon Sep 17 00:00:00 2001 From: NickDevi Date: Thu, 5 Sep 2024 18:16:40 +0300 Subject: [PATCH 05/12] fixed-lint-errors-parantheses --- src/upgrades/1.0.0/chat_room_hashes.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/upgrades/1.0.0/chat_room_hashes.js b/src/upgrades/1.0.0/chat_room_hashes.js index 0212bee603..dfc81bdfaa 100644 --- a/src/upgrades/1.0.0/chat_room_hashes.js +++ b/src/upgrades/1.0.0/chat_room_hashes.js @@ -17,7 +17,7 @@ module.exports = { }; function processChatRooms(currentChatRoomId, nextChatRoomId, callback) { - console.log(">>>processChatRooms accessed"); + console.log('>>>processChatRooms accessed'); async.whilst( next => next(null, currentChatRoomId <= nextChatRoomId), (next) => { // Added parentheses around (next) @@ -31,7 +31,7 @@ function processChatRooms(currentChatRoomId, nextChatRoomId, callback) { } function processSingleChatRoom(chatRoomId, next) { - console.log(">>>processSingleChatRoom accessed"); + console.log('>>>processSingleChatRoom accessed'); getFirstUserFromChatRoom(chatRoomId, (err, firstUser) => { if (err || !firstUser) { return next(err); // Continue on error or if no user found @@ -41,7 +41,7 @@ function processSingleChatRoom(chatRoomId, next) { } function getFirstUserFromChatRoom(chatRoomId, callback) { - console.log(">>>getFirst accessed"); + console.log('>>>getFirst accessed'); db.getSortedSetRange(`chat:room:${chatRoomId}:uids`, 0, 0, (err, uids) => { if (err) { return callback(err); @@ -51,6 +51,6 @@ function getFirstUserFromChatRoom(chatRoomId, callback) { } function setChatRoomOwner(chatRoomId, ownerId, callback) { - console.log(">>>setChatRoomOwner accessed"); + console.log('>>>setChatRoomOwner accessed'); db.setObject(`chat:room:${chatRoomId}`, { owner: ownerId, roomId: chatRoomId }, callback); } From ce2eec24e4c08e66322f9c95f44a101d7bbef874 Mon Sep 17 00:00:00 2001 From: NickDevi Date: Thu, 5 Sep 2024 19:12:43 +0300 Subject: [PATCH 06/12] FixingLogicIssue --- src/upgrades/1.0.0/chat_room_hashes.js | 46 +++++++++++++------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/upgrades/1.0.0/chat_room_hashes.js b/src/upgrades/1.0.0/chat_room_hashes.js index dfc81bdfaa..7c5406def9 100644 --- a/src/upgrades/1.0.0/chat_room_hashes.js +++ b/src/upgrades/1.0.0/chat_room_hashes.js @@ -11,18 +11,22 @@ module.exports = { if (err) { return callback(err); } - processChatRooms(1, nextChatRoomId, callback); + processChatRooms(nextChatRoomId, callback); }); }, }; -function processChatRooms(currentChatRoomId, nextChatRoomId, callback) { - console.log('>>>processChatRooms accessed'); +function processChatRooms(nextChatRoomId, callback) { + let currentChatRoomId = 1; + async.whilst( - next => next(null, currentChatRoomId <= nextChatRoomId), - (next) => { // Added parentheses around (next) - processSingleChatRoom(currentChatRoomId, () => { - currentChatRoomId += 1; // Replacing unary operator '++' + () => currentChatRoomId <= nextChatRoomId, + (next) => { + handleChatRoom(currentChatRoomId, (err) => { + if (err) { + return next(err); + } + currentChatRoomId += 1; next(); }); }, @@ -30,27 +34,23 @@ function processChatRooms(currentChatRoomId, nextChatRoomId, callback) { ); } -function processSingleChatRoom(chatRoomId, next) { - console.log('>>>processSingleChatRoom accessed'); - getFirstUserFromChatRoom(chatRoomId, (err, firstUser) => { - if (err || !firstUser) { - return next(err); // Continue on error or if no user found - } - setChatRoomOwner(chatRoomId, firstUser, next); - }); -} - -function getFirstUserFromChatRoom(chatRoomId, callback) { - console.log('>>>getFirst accessed'); +function handleChatRoom(chatRoomId, callback) { db.getSortedSetRange(`chat:room:${chatRoomId}:uids`, 0, 0, (err, uids) => { if (err) { return callback(err); } - callback(null, Array.isArray(uids) && uids.length && uids[0] ? uids[0] : null); + if (!isValidUid(uids)) { + return callback(); + } + storeChatRoom(chatRoomId, uids[0], callback); }); } -function setChatRoomOwner(chatRoomId, ownerId, callback) { - console.log('>>>setChatRoomOwner accessed'); - db.setObject(`chat:room:${chatRoomId}`, { owner: ownerId, roomId: chatRoomId }, callback); +function isValidUid(uids) { + return Array.isArray(uids) && uids.length > 0 && uids[0]; +} + +function storeChatRoom(chatRoomId, ownerUid, callback) { + const chatRoomData = { owner: ownerUid, roomId: chatRoomId }; + db.setObject(`chat:room:${chatRoomId}`, chatRoomData, callback); } From 49c4901bad3c1a9c60085cc33bd0ee0a2b2c2cd2 Mon Sep 17 00:00:00 2001 From: NickDevi Date: Thu, 5 Sep 2024 19:28:33 +0300 Subject: [PATCH 07/12] FixedSyntaxIssue --- src/upgrades/1.0.0/chat_room_hashes.js | 64 +++++++++++++++----------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/src/upgrades/1.0.0/chat_room_hashes.js b/src/upgrades/1.0.0/chat_room_hashes.js index 7c5406def9..6d107ce138 100644 --- a/src/upgrades/1.0.0/chat_room_hashes.js +++ b/src/upgrades/1.0.0/chat_room_hashes.js @@ -11,46 +11,54 @@ module.exports = { if (err) { return callback(err); } - processChatRooms(nextChatRoomId, callback); + + let currentChatRoomId = 1; + async.whilst( + (next) => { shouldProcessNextRoom(currentChatRoomId, nextChatRoomId, next); }, + (next) => { + processChatRoom(currentChatRoomId, next, () => { + currentChatRoomId += 1; + }); + }, + callback + ); }); }, }; -function processChatRooms(nextChatRoomId, callback) { - let currentChatRoomId = 1; - - async.whilst( - () => currentChatRoomId <= nextChatRoomId, - (next) => { - handleChatRoom(currentChatRoomId, (err) => { - if (err) { - return next(err); - } - currentChatRoomId += 1; - next(); - }); - }, - callback - ); +function shouldProcessNextRoom(currentChatRoomId, nextChatRoomId, next) { + next(null, currentChatRoomId <= nextChatRoomId); } -function handleChatRoom(chatRoomId, callback) { - db.getSortedSetRange(`chat:room:${chatRoomId}:uids`, 0, 0, (err, uids) => { +function processChatRoom(currentChatRoomId, next, incrementCurrentChatRoom) { + getRoomUids(currentChatRoomId, (err, uids) => { if (err) { - return callback(err); + return next(err); } - if (!isValidUid(uids)) { - return callback(); + + if (!isValidUids(uids)) { + incrementCurrentChatRoom(); + return next(); } - storeChatRoom(chatRoomId, uids[0], callback); + + setChatRoomOwner(currentChatRoomId, uids[0], (err) => { + if (err) { + return next(err); + } + incrementCurrentChatRoom(); + next(); + }); }); } -function isValidUid(uids) { - return Array.isArray(uids) && uids.length > 0 && uids[0]; +function getRoomUids(currentChatRoomId, callback) { + db.getSortedSetRange(`chat:room:${currentChatRoomId}:uids`, 0, 0, callback); +} + +function isValidUids(uids) { + return Array.isArray(uids) && uids.length && uids[0]; } -function storeChatRoom(chatRoomId, ownerUid, callback) { - const chatRoomData = { owner: ownerUid, roomId: chatRoomId }; - db.setObject(`chat:room:${chatRoomId}`, chatRoomData, callback); +function setChatRoomOwner(roomId, ownerUid, callback) { + db.setObject(`chat:room:${roomId}`, { owner: ownerUid, roomId }, callback); } From 9d5aa039968e15982e2b29f2ca70236d7431ed3b Mon Sep 17 00:00:00 2001 From: NickDevi Date: Thu, 5 Sep 2024 19:30:53 +0300 Subject: [PATCH 08/12] AddingConsoleLogs --- src/upgrades/1.0.0/chat_room_hashes.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/upgrades/1.0.0/chat_room_hashes.js b/src/upgrades/1.0.0/chat_room_hashes.js index 6d107ce138..1699137ae9 100644 --- a/src/upgrades/1.0.0/chat_room_hashes.js +++ b/src/upgrades/1.0.0/chat_room_hashes.js @@ -7,6 +7,7 @@ module.exports = { name: 'Chat room hashes', timestamp: Date.UTC(2015, 11, 23), method: function (callback) { + console.log('ndevidze'); db.getObjectField('global', 'nextChatRoomId', (err, nextChatRoomId) => { if (err) { return callback(err); @@ -27,10 +28,12 @@ module.exports = { }; function shouldProcessNextRoom(currentChatRoomId, nextChatRoomId, next) { + console.log('ndevidze'); next(null, currentChatRoomId <= nextChatRoomId); } function processChatRoom(currentChatRoomId, next, incrementCurrentChatRoom) { + console.log('ndevidze'); getRoomUids(currentChatRoomId, (err, uids) => { if (err) { return next(err); @@ -52,13 +55,16 @@ function processChatRoom(currentChatRoomId, next, incrementCurrentChatRoom) { } function getRoomUids(currentChatRoomId, callback) { + console.log('ndevidze'); db.getSortedSetRange(`chat:room:${currentChatRoomId}:uids`, 0, 0, callback); } function isValidUids(uids) { + console.log('ndevidze'); return Array.isArray(uids) && uids.length && uids[0]; } function setChatRoomOwner(roomId, ownerUid, callback) { + console.log('ndevidze'); db.setObject(`chat:room:${roomId}`, { owner: ownerUid, roomId }, callback); } From 67b5971624d8af9548a09426c9d8ada79480ddd1 Mon Sep 17 00:00:00 2001 From: NickDevi Date: Thu, 5 Sep 2024 19:44:32 +0300 Subject: [PATCH 09/12] ConloseLogs --- src/upgrades/1.0.0/chat_room_hashes.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/upgrades/1.0.0/chat_room_hashes.js b/src/upgrades/1.0.0/chat_room_hashes.js index 1699137ae9..499fc349fc 100644 --- a/src/upgrades/1.0.0/chat_room_hashes.js +++ b/src/upgrades/1.0.0/chat_room_hashes.js @@ -7,6 +7,7 @@ module.exports = { name: 'Chat room hashes', timestamp: Date.UTC(2015, 11, 23), method: function (callback) { + console.log('Chat room hashes method triggered'); console.log('ndevidze'); db.getObjectField('global', 'nextChatRoomId', (err, nextChatRoomId) => { if (err) { From aba7ce48b59a878bc867a3bec672c93ac292083a Mon Sep 17 00:00:00 2001 From: NickDevi Date: Thu, 5 Sep 2024 21:20:20 +0300 Subject: [PATCH 10/12] refactored functions --- src/topics/sorted.js | 160 +++++++++++++------------ src/upgrades/1.0.0/chat_room_hashes.js | 72 ++++------- 2 files changed, 105 insertions(+), 127 deletions(-) diff --git a/src/topics/sorted.js b/src/topics/sorted.js index 98292f0ddb..60813a32e0 100644 --- a/src/topics/sorted.js +++ b/src/topics/sorted.js @@ -17,7 +17,16 @@ module.exports = function (Topics) { topicCount: 0, topics: [], }; - + params = initializeParams(params); + data.tids = await getTids(params); + data.tids = await sortTids(data.tids, params); + data.tids = await filterTids(data.tids.slice(0, meta.config.recentMaxTopics), params); + data.topicCount = data.tids.length; + data.topics = await getTopics(data.tids, params); + data.nextStart = params.stop + 1; + return data; + }; + function initializeParams(params) { params.term = params.term || 'alltime'; params.sort = params.sort || 'recent'; params.query = params.query || {}; @@ -28,15 +37,8 @@ module.exports = function (Topics) { if (params.tags && !Array.isArray(params.tags)) { params.tags = [params.tags]; } - data.tids = await getTids(params); - data.tids = await sortTids(data.tids, params); - data.tids = await filterTids(data.tids.slice(0, meta.config.recentMaxTopics), params); - data.topicCount = data.tids.length; - data.topics = await getTopics(data.tids, params); - data.nextStart = params.stop + 1; - return data; - }; - + return params; + } async function getTids(params) { if (plugins.hooks.hasListeners('filter:topics.getSortedTids')) { const result = await plugins.hooks.fire('filter:topics.getSortedTids', { params: params, tids: [] }); @@ -138,68 +140,77 @@ module.exports = function (Topics) { async function getCidTids(params) { if (params.tags.length) { - return _.intersection(...await Promise.all(params.tags.map(async (tag) => { - const sets = params.cids.map(cid => `cid:${cid}:tag:${tag}:topics`); - return await db.getSortedSetRevRange(sets, 0, -1); - }))); + return await getTidsForTagsAndCids(params); } - + const sets = getCidSets(params.cids, params.sort); + let pinnedTids = await db.getSortedSetRevRange(sets.pinnedSets, 0, -1); + pinnedTids = await Topics.tools.checkPinExpiry(pinnedTids); + const tids = await db[sets.method](sets.normalSets, 0, meta.config.recentMaxTopics - 1); + return pinnedTids.concat(tids); + } + function getCidSets(cids, sort) { const sets = []; const pinnedSets = []; - params.cids.forEach((cid) => { - if (params.sort === 'recent' || params.sort === 'old') { + cids.forEach((cid) => { + if (sort === 'recent' || sort === 'old') { sets.push(`cid:${cid}:tids`); } else { - sets.push(`cid:${cid}:tids${params.sort ? `:${params.sort}` : ''}`); + sets.push(`cid:${cid}:tids${sort ? `:${sort}` : ''}`); } pinnedSets.push(`cid:${cid}:tids:pinned`); }); - let pinnedTids = await db.getSortedSetRevRange(pinnedSets, 0, -1); - pinnedTids = await Topics.tools.checkPinExpiry(pinnedTids); - const method = params.sort === 'old' ? - 'getSortedSetRange' : - 'getSortedSetRevRange'; - const tids = await db[method](sets, 0, meta.config.recentMaxTopics - 1); - return pinnedTids.concat(tids); + const method = (sort === 'old') ? 'getSortedSetRange' : 'getSortedSetRevRange'; + return { normalSets: sets, pinnedSets: pinnedSets, method: method }; + } + async function getTidsForTagsAndCids(params) { + return _.intersection( + ...await Promise.all(params.tags.map(async (tag) => { + const sets = params.cids.map(cid => `cid:${cid}:tag:${tag}:topics`); + return await db.getSortedSetRevRange(sets, 0, -1); + })) + ); } async function sortTids(tids, params) { - if (params.term === 'alltime' && !params.cids && !params.tags.length && params.filter !== 'watched' && !params.floatPinned) { + if (canSkipSorting(params)) { return tids; } - if (params.sort === 'posts' && params.term !== 'alltime') { return tids; } - const { sortMap, fields } = await plugins.hooks.fire('filter:topics.sortOptions', { params, - fields: [ - 'tid', 'timestamp', 'lastposttime', 'upvotes', 'downvotes', 'postcount', 'pinned', - ], - sortMap: { - recent: sortRecent, - old: sortOld, - create: sortCreate, - posts: sortPopular, - votes: sortVotes, - views: sortViews, - }, + fields: getFields(), + sortMap: getSortMap(), }); - const topicData = await Topics.getTopicsFields(tids, fields); - const sortFn = sortMap.hasOwnProperty(params.sort) && sortMap[params.sort] ? - sortMap[params.sort] : sortRecent; - + const sortFn = getSortFunction(params.sort, sortMap); if (params.floatPinned) { floatPinned(topicData, sortFn); } else { topicData.sort(sortFn); } - return topicData.map(topic => topic && topic.tid); } - + function canSkipSorting(params) { + return params.term === 'alltime' && !params.cids && !params.tags.length && params.filter !== 'watched' && !params.floatPinned; + } + function getFields() { + return ['tid', 'timestamp', 'lastposttime', 'upvotes', 'downvotes', 'postcount', 'pinned']; + } + function getSortMap() { + return { + recent: sortRecent, + old: sortOld, + create: sortCreate, + posts: sortPopular, + votes: sortVotes, + views: sortViews, + }; + } + function getSortFunction(sort, sortMap) { + return sortMap.hasOwnProperty(sort) && sortMap[sort] ? sortMap[sort] : sortRecent; + } function floatPinned(topicData, sortFn) { topicData.sort((a, b) => (a.pinned !== b.pinned ? b.pinned - a.pinned : sortFn(a, b))); } @@ -214,7 +225,7 @@ module.exports = function (Topics) { function sortCreate(a, b) { return b.timestamp - a.timestamp; - } + } function sortVotes(a, b) { if (a.votes !== b.votes) { @@ -235,49 +246,48 @@ module.exports = function (Topics) { } async function filterTids(tids, params) { - const { filter } = params; - const { uid } = params; - - if (filter === 'new') { - tids = await Topics.filterNewTids(tids, uid); - } else if (filter === 'unreplied') { - tids = await Topics.filterUnrepliedTids(tids); - } else { - tids = await Topics.filterNotIgnoredTids(tids, uid); - } - - tids = await privileges.topics.filterTids('topics:read', tids, uid); - let topicData = await Topics.getTopicsFields(tids, ['uid', 'tid', 'cid', 'tags']); + tids = await applyFilterByType(tids, params); + tids = await privileges.topics.filterTids('topics:read', tids, params.uid); + const topicData = await Topics.getTopicsFields(tids, ['uid', 'tid', 'cid', 'tags']); const topicCids = _.uniq(topicData.map(topic => topic.cid)).filter(Boolean); - - async function getIgnoredCids() { - if (params.cids || filter === 'watched' || meta.config.disableRecentCategoryFilter) { - return []; - } - return await categories.isIgnored(topicCids, uid); - } const [ignoredCids, filtered] = await Promise.all([ - getIgnoredCids(), - user.blocks.filter(uid, topicData), + getIgnoredCids(params, topicCids), + user.blocks.filter(params.uid, topicData), ]); + return filterTopicsByCidsAndTags(filtered, ignoredCids, params); + } - const isCidIgnored = _.zipObject(topicCids, ignoredCids); - topicData = filtered; + + async function applyFilterByType(tids, params) { + switch (params.filter) { + case 'new': + return await Topics.filterNewTids(tids, params.uid); + case 'unreplied': + return await Topics.filterUnrepliedTids(tids); + default: + return await Topics.filterNotIgnoredTids(tids, params.uid); + } + } + async function getIgnoredCids(params, topicCids) { + if (params.cids || params.filter === 'watched' || meta.config.disableRecentCategoryFilter) { + return []; + } + return await categories.isIgnored(topicCids, params.uid); + } + function filterTopicsByCidsAndTags(topicData, ignoredCids, params) { + const isCidIgnored = _.zipObject(topicData.map(t => t.cid), ignoredCids); const cids = params.cids && params.cids.map(String); const { tags } = params; - tids = topicData.filter(t => ( + const tids = topicData.filter(t => ( t && t.cid && !isCidIgnored[t.cid] && (!cids || cids.includes(String(t.cid))) && (!tags.length || tags.every(tag => t.tags.find(topicTag => topicTag.value === tag))) )).map(t => t.tid); - - const result = await plugins.hooks.fire('filter:topics.filterSortedTids', { tids: tids, params: params }); - return result.tids; + return plugins.hooks.fire('filter:topics.filterSortedTids', { tids, params }).then(result => result.tids); } - async function getTopics(tids, params) { tids = tids.slice(params.start, params.stop !== -1 ? params.stop + 1 : undefined); const topicData = await Topics.getTopicsByTids(tids, params); diff --git a/src/upgrades/1.0.0/chat_room_hashes.js b/src/upgrades/1.0.0/chat_room_hashes.js index 499fc349fc..78de201a85 100644 --- a/src/upgrades/1.0.0/chat_room_hashes.js +++ b/src/upgrades/1.0.0/chat_room_hashes.js @@ -3,69 +3,37 @@ const async = require('async'); const db = require('../../database'); + module.exports = { name: 'Chat room hashes', timestamp: Date.UTC(2015, 11, 23), method: function (callback) { - console.log('Chat room hashes method triggered'); - console.log('ndevidze'); db.getObjectField('global', 'nextChatRoomId', (err, nextChatRoomId) => { if (err) { return callback(err); } - let currentChatRoomId = 1; - async.whilst( - (next) => { shouldProcessNextRoom(currentChatRoomId, nextChatRoomId, next); }, - (next) => { - processChatRoom(currentChatRoomId, next, () => { + async.whilst((next) => { + next(null, currentChatRoomId <= nextChatRoomId); + }, (next) => { + db.getSortedSetRange(`chat:room:${currentChatRoomId}:uids`, 0, 0, (err, uids) => { + if (err) { + return next(err); + } + if (!Array.isArray(uids) || !uids.length || !uids[0]) { + currentChatRoomId += 1; + return next(); + } + + db.setObject(`chat:room:${currentChatRoomId}`, { owner: uids[0], roomId: currentChatRoomId }, (err) => { + if (err) { + return next(err); + } currentChatRoomId += 1; + next(); }); - }, - callback - ); + }); + }, callback); }); }, }; - -function shouldProcessNextRoom(currentChatRoomId, nextChatRoomId, next) { - console.log('ndevidze'); - next(null, currentChatRoomId <= nextChatRoomId); -} - -function processChatRoom(currentChatRoomId, next, incrementCurrentChatRoom) { - console.log('ndevidze'); - getRoomUids(currentChatRoomId, (err, uids) => { - if (err) { - return next(err); - } - - if (!isValidUids(uids)) { - incrementCurrentChatRoom(); - return next(); - } - - setChatRoomOwner(currentChatRoomId, uids[0], (err) => { - if (err) { - return next(err); - } - incrementCurrentChatRoom(); - next(); - }); - }); -} - -function getRoomUids(currentChatRoomId, callback) { - console.log('ndevidze'); - db.getSortedSetRange(`chat:room:${currentChatRoomId}:uids`, 0, 0, callback); -} - -function isValidUids(uids) { - console.log('ndevidze'); - return Array.isArray(uids) && uids.length && uids[0]; -} - -function setChatRoomOwner(roomId, ownerUid, callback) { - console.log('ndevidze'); - db.setObject(`chat:room:${roomId}`, { owner: ownerUid, roomId }, callback); -} From f216500194b35f5e42dfde291fe12b8f48eaf802 Mon Sep 17 00:00:00 2001 From: NickDevi Date: Thu, 5 Sep 2024 21:48:11 +0300 Subject: [PATCH 11/12] finished working on lints --- src/topics/sorted.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/topics/sorted.js b/src/topics/sorted.js index 60813a32e0..2690237f87 100644 --- a/src/topics/sorted.js +++ b/src/topics/sorted.js @@ -225,7 +225,7 @@ module.exports = function (Topics) { function sortCreate(a, b) { return b.timestamp - a.timestamp; - } + } function sortVotes(a, b) { if (a.votes !== b.votes) { @@ -256,9 +256,6 @@ module.exports = function (Topics) { ]); return filterTopicsByCidsAndTags(filtered, ignoredCids, params); } - - - async function applyFilterByType(tids, params) { switch (params.filter) { case 'new': @@ -269,7 +266,7 @@ module.exports = function (Topics) { return await Topics.filterNotIgnoredTids(tids, params.uid); } } - async function getIgnoredCids(params, topicCids) { + async function getIgnoredCids(params, topicCids) { if (params.cids || params.filter === 'watched' || meta.config.disableRecentCategoryFilter) { return []; } From 8ac13d843468835dffdc2f7e32b49717c2b0613e Mon Sep 17 00:00:00 2001 From: NickDevi Date: Thu, 5 Sep 2024 21:53:33 +0300 Subject: [PATCH 12/12] Adding Logs --- src/topics/sorted.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/topics/sorted.js b/src/topics/sorted.js index 2690237f87..bceda88015 100644 --- a/src/topics/sorted.js +++ b/src/topics/sorted.js @@ -11,6 +11,7 @@ const meta = require('../meta'); const plugins = require('../plugins'); module.exports = function (Topics) { + console.log('ndevidze'); Topics.getSortedTopics = async function (params) { const data = { nextStart: 0, @@ -139,6 +140,7 @@ module.exports = function (Topics) { } async function getCidTids(params) { + console.log('getting Tids'); if (params.tags.length) { return await getTidsForTagsAndCids(params); } @@ -172,6 +174,7 @@ module.exports = function (Topics) { } async function sortTids(tids, params) { + console.log('sorting Tids'); if (canSkipSorting(params)) { return tids; } @@ -246,6 +249,7 @@ module.exports = function (Topics) { } async function filterTids(tids, params) { + console.log('filtering Tids'); tids = await applyFilterByType(tids, params); tids = await privileges.topics.filterTids('topics:read', tids, params.uid); const topicData = await Topics.getTopicsFields(tids, ['uid', 'tid', 'cid', 'tags']);