Node.js based forum software built for the modern web
fix: honor topic sort in navigator scroller, closes #12480 (#14634)
palmoni547bb31422ab0
diff --git a/public/src/modules/navigator.js b/public/src/modules/navigator.jsindex 7783bf7112..0ef8501333 100644--- a/public/src/modules/navigator.js+++ b/public/src/modules/navigator.js@@ -201,6 +201,7 @@ define('navigator', [return await socket.emit('posts.getPostTimestampByIndex', {tid: ajaxify.data.tid,index: index - 1,+ sort: ajaxify.data.sortOption,});}@@ -410,7 +411,11 @@ define('navigator', [}renderPostIndex = index;- const postData = await socket.emit('posts.getPostSummaryByIndex', { tid: ajaxify.data.tid, index: index - 1 });+ const postData = await socket.emit('posts.getPostSummaryByIndex', {+ tid: ajaxify.data.tid,+ index: index - 1,+ sort: ajaxify.data.sortOption,+ });const html = await app.parseAndTranslate('partials/topic/navigation-post', { post: postData });paginationBlockEl@@ -443,7 +448,11 @@ define('navigator', [function generateUrl(index) {const pathname = window.location.pathname.replace(config.relative_path, '');const parts = pathname.split('/');- const newUrl = parts[1] + '/' + parts[2] + '/' + parts[3] + (index ? '/' + index : '');+ let newUrl = parts[1] + '/' + parts[2] + '/' + parts[3] + (index ? '/' + index : '');+ const { sort } = utils.params();+ if (sort) {+ newUrl += `?sort=${encodeURIComponent(sort)}`;+ }const data = {newUrl,index,diff --git a/src/socket.io/posts.js b/src/socket.io/posts.jsindex b1130faca1..c613adb237 100644--- a/src/socket.io/posts.js+++ b/src/socket.io/posts.js@@ -19,17 +19,21 @@ SocketPosts.getRawPost = async function (socket, pid) {return await api.posts.getRaw(socket, { pid });};+async function getPidByIndex(tid, index, sort) {+ if (index === 0) {+ return await topics.getTopicField(tid, 'mainPid');+ }+ const set = sort === 'most_votes' ? `tid:${tid}:posts:votes` : `tid:${tid}:posts`;+ const reverse = sort === 'newest_to_oldest' || sort === 'most_votes';+ const pids = await db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, index - 1, index - 1);+ return pids.length ? pids[0] : null;+}+SocketPosts.getPostSummaryByIndex = async function (socket, data) {if (data.index < 0) {data.index = 0;}- let pid;- if (data.index === 0) {- pid = await topics.getTopicField(data.tid, 'mainPid');- } else {- pid = await db.getSortedSetRange(`tid:${data.tid}:posts`, data.index - 1, data.index - 1);- }- pid = Array.isArray(pid) ? pid[0] : pid;+ const pid = await getPidByIndex(data.tid, data.index, data.sort);if (!pid) {return 0;}@@ -41,13 +45,7 @@ SocketPosts.getPostTimestampByIndex = async function (socket, data) {if (data.index < 0) {data.index = 0;}- let pid;- if (data.index === 0) {- pid = await topics.getTopicField(data.tid, 'mainPid');- } else {- pid = await db.getSortedSetRange(`tid:${data.tid}:posts`, data.index - 1, data.index - 1);- }- pid = Array.isArray(pid) ? pid[0] : pid;+ const pid = await getPidByIndex(data.tid, data.index, data.sort);const [postPrivileges] = await privileges.posts.get([pid], socket.uid);if (!postPrivileges.read || !postPrivileges['topics:read'] || postPrivileges.disabled) {throw new Error('[[error:no-privileges]]');diff --git a/test/posts.js b/test/posts.jsindex 32e1ede9ab..b947c34e49 100644--- a/test/posts.js+++ b/test/posts.js@@ -918,6 +918,32 @@ describe('Post\'s', () => {assert(utils.isNumber(timestamp));});+ it('should get post summary and timestamp by index with sort', async () => {+ const result = await topics.post({+ uid: voterUid,+ cid: cid,+ title: 'topic for sorted navigation',+ content: 'main post',+ });+ await apiTopics.reply({ uid: voterUid }, { tid: result.topicData.tid, content: 'first reply' });+ await sleep(5);+ const lastReply = await apiTopics.reply({ uid: voterUid }, { tid: result.topicData.tid, content: 'second reply' });++ const timestamp = await socketPosts.getPostTimestampByIndex({ uid: voterUid }, {+ index: 1,+ tid: result.topicData.tid,+ sort: 'newest_to_oldest',+ });+ assert.strictEqual(timestamp, lastReply.timestamp);++ const summary = await socketPosts.getPostSummaryByIndex({ uid: voterUid }, {+ index: 1,+ tid: result.topicData.tid,+ sort: 'newest_to_oldest',+ });+ assert.strictEqual(summary.pid, lastReply.pid);+ });+it('should get post timestamp by index', async () => {const summary = await socketPosts.getPostSummaryByPid({ uid: voterUid }, {pid: pid,