From ece1e74e4f69e341b5d1cc03e32b27354dff1575 Mon Sep 17 00:00:00 2001 From: nniknam1 Date: Tue, 2 Sep 2025 13:58:13 +0000 Subject: [PATCH 1/3] Fixed Bookmark.js file issue --- src/topics/bookmarks.js | 115 +++++++++++++++++++++++----------------- 1 file changed, 65 insertions(+), 50 deletions(-) diff --git a/src/topics/bookmarks.js b/src/topics/bookmarks.js index e7d52f84ae..9848c5a037 100644 --- a/src/topics/bookmarks.js +++ b/src/topics/bookmarks.js @@ -1,69 +1,84 @@ - 'use strict'; const async = require('async'); - const db = require('../database'); const user = require('../user'); -module.exports = function (Topics) { - Topics.getUserBookmark = async function (tid, uid) { - if (parseInt(uid, 10) <= 0) { - return null; - } - return await db.sortedSetScore(`tid:${tid}:bookmarks`, uid); - }; +// ---- implementations moved out of the exports wrapper ---- - Topics.getUserBookmarks = async function (tids, uid) { - if (parseInt(uid, 10) <= 0) { - return tids.map(() => null); - } - return await db.sortedSetsScore(tids.map(tid => `tid:${tid}:bookmarks`), uid); - }; +async function getUserBookmark(tid, uid) { + if (Number.parseInt(uid, 10) <= 0) { + return null; + } + return db.sortedSetScore(`tid:${tid}:bookmarks`, uid); +} - Topics.setUserBookmark = async function (tid, uid, index) { - if (parseInt(uid, 10) <= 0) { - return; - } - await db.sortedSetAdd(`tid:${tid}:bookmarks`, index, uid); - }; +async function getUserBookmarks(tids, uid) { + if (Number.parseInt(uid, 10) <= 0) { + return tids.map(() => null); + } + return db.sortedSetsScore( + tids.map(tid => `tid:${tid}:bookmarks`), + uid, + ); +} - Topics.getTopicBookmarks = async function (tid) { - return await db.getSortedSetRangeWithScores(`tid:${tid}:bookmarks`, 0, -1); - }; +async function setUserBookmark(tid, uid, index) { + // ⬇️ Your requested log line + require.main.require('winston').info('NOOR_NIKNAM: reached src/topics/bookmarks.js > setUserBookmark'); - Topics.updateTopicBookmarks = async function (tid, pids) { - const maxIndex = await Topics.getPostCount(tid); - const indices = await db.sortedSetRanks(`tid:${tid}:posts`, pids); - const postIndices = indices.map(i => (i === null ? 0 : i + 1)); - const minIndex = Math.min(...postIndices); + if (Number.parseInt(uid, 10) <= 0) { + return; + } + await db.sortedSetAdd(`tid:${tid}:bookmarks`, index, uid); +} - const bookmarks = await Topics.getTopicBookmarks(tid); +async function getTopicBookmarks(tid) { + return db.getSortedSetRangeWithScores(`tid:${tid}:bookmarks`, 0, -1); +} - const uidData = bookmarks.map(b => ({ uid: b.value, bookmark: parseInt(b.score, 10) })) - .filter(data => data.bookmark >= minIndex); +async function updateTopicBookmarks(Topics, tid, pids) { + const maxIndex = await Topics.getPostCount(tid); + const indices = await db.sortedSetRanks(`tid:${tid}:posts`, pids); + const postIndices = indices.map(i => (i === null ? 0 : i + 1)); + const minIndex = Math.min(...postIndices); - await async.eachLimit(uidData, 50, async (data) => { - let bookmark = Math.min(data.bookmark, maxIndex); + const bookmarks = await getTopicBookmarks(tid); - postIndices.forEach((i) => { - if (i < data.bookmark) { - bookmark -= 1; - } - }); + const uidData = bookmarks + .map(b => ({ uid: b.value, bookmark: Number.parseInt(b.score, 10) })) + .filter(data => data.bookmark >= minIndex); - // make sure the bookmark is valid if we removed the last post - bookmark = Math.min(bookmark, maxIndex - pids.length); - if (bookmark === data.bookmark) { - return; - } + await async.eachLimit(uidData, 50, async (data) => { + let bookmark = Math.min(data.bookmark, maxIndex); - const settings = await user.getSettings(data.uid); - if (settings.topicPostSort === 'most_votes') { - return; + postIndices.forEach((i) => { + if (i < data.bookmark) { + bookmark -= 1; } - - await Topics.setUserBookmark(tid, data.uid, bookmark); }); - }; + + // ensure valid bookmark if last post(s) were removed + bookmark = Math.min(bookmark, maxIndex - pids.length); + if (bookmark === data.bookmark) { + return; + } + + const settings = await user.getSettings(data.uid); + if (settings.topicPostSort === 'most_votes') { + return; + } + + await setUserBookmark(tid, data.uid, bookmark); + }); +} + +// ---- export wrapper with no returns ---- +module.exports = function (Topics) { + Topics.getUserBookmark = getUserBookmark; + Topics.getUserBookmarks = getUserBookmarks; + Topics.setUserBookmark = setUserBookmark; + Topics.getTopicBookmarks = getTopicBookmarks; + Topics.updateTopicBookmarks = (tid, pids) => + updateTopicBookmarks(Topics, tid, pids); }; From e5f3d7ba9f15565af14f65e0bebca1e971a14c7f Mon Sep 17 00:00:00 2001 From: nniknam1 Date: Thu, 4 Sep 2025 08:53:24 +0000 Subject: [PATCH 2/3] Screenshotted log messages successfully --- src/topics/bookmarks.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/topics/bookmarks.js b/src/topics/bookmarks.js index 9848c5a037..214778becd 100644 --- a/src/topics/bookmarks.js +++ b/src/topics/bookmarks.js @@ -7,6 +7,11 @@ const user = require('../user'); // ---- implementations moved out of the exports wrapper ---- async function getUserBookmark(tid, uid) { + // TEMP: log for manual verification; remove before final commit + console.log('NOOR_NIKNAM:getUserBookmark', { tid, uid }); + // Or, if you prefer winston: + // require.main.require('winston').info('NOOR_NIKNAM:getUserBookmark', { tid, uid }); + if (Number.parseInt(uid, 10) <= 0) { return null; } @@ -19,13 +24,13 @@ async function getUserBookmarks(tids, uid) { } return db.sortedSetsScore( tids.map(tid => `tid:${tid}:bookmarks`), - uid, + uid ); } async function setUserBookmark(tid, uid, index) { - // ⬇️ Your requested log line - require.main.require('winston').info('NOOR_NIKNAM: reached src/topics/bookmarks.js > setUserBookmark'); + // TEMP: log for manual verification; remove before final commit + //console.log('NOOR_NIKNAM:setUserBookmark', { tid, uid, index }); if (Number.parseInt(uid, 10) <= 0) { return; From 667e75bc44bf3eda63b1ad4d7bc309512607f49f Mon Sep 17 00:00:00 2001 From: nniknam1 Date: Thu, 4 Sep 2025 12:05:22 +0000 Subject: [PATCH 3/3] Removed the log comment --- src/topics/bookmarks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/topics/bookmarks.js b/src/topics/bookmarks.js index 214778becd..d6cef5e2b6 100644 --- a/src/topics/bookmarks.js +++ b/src/topics/bookmarks.js @@ -8,7 +8,7 @@ const user = require('../user'); async function getUserBookmark(tid, uid) { // TEMP: log for manual verification; remove before final commit - console.log('NOOR_NIKNAM:getUserBookmark', { tid, uid }); + //console.log('NOOR_NIKNAM:getUserBookmark', { tid, uid }); // Or, if you prefer winston: // require.main.require('winston').info('NOOR_NIKNAM:getUserBookmark', { tid, uid });