From 89399c0ed511cc2ab60b2ccd0344b0e6741212bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sun, 31 Oct 2021 15:09:33 -0400 Subject: [PATCH] fix: #9954, get next post timestamp fixes topic events being inserted in after first page but at the wrong spot --- public/src/modules/helpers.js | 2 +- src/topics/index.js | 17 +++++++++++++++++ src/topics/posts.js | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/public/src/modules/helpers.js b/public/src/modules/helpers.js index b1778c92b1..34781ba92d 100644 --- a/public/src/modules/helpers.js +++ b/public/src/modules/helpers.js @@ -212,7 +212,7 @@ function renderTopicEvents(index) { const start = this.posts[index].timestamp; - const end = this.posts[index + 1] ? this.posts[index + 1].timestamp : Date.now(); + const end = this.posts[index].nextPostTimestamp; const events = this.events.filter(event => event.timestamp >= start && event.timestamp < end); if (!events.length) { return ''; diff --git a/src/topics/index.js b/src/topics/index.js index 85aa31aa2a..ddc432bfd4 100644 --- a/src/topics/index.js +++ b/src/topics/index.js @@ -240,9 +240,26 @@ async function getMainPostAndReplies(topic, set, uid, start, stop, reverse) { Topics.calculatePostIndices(replies, repliesStart); + await Topics.addNextPostTimestamp(postData, set, reverse); return await Topics.addPostData(postData, uid); } +Topics.addNextPostTimestamp = async function (postData, set, reverse) { + if (!postData.length) { + return; + } + postData.forEach((p, index) => { + if (p && postData[index + 1]) { + p.nextPostTimestamp = postData[index + 1].timestamp; + } + }); + const lastPost = postData[postData.length - 1]; + if (lastPost && lastPost.index) { + const data = await db[reverse ? 'getSortedSetRevRangeWithScores' : 'getSortedSetRangeWithScores'](set, lastPost.index, lastPost.index); + lastPost.nextPostTimestamp = data.length ? data[0].score : Date.now(); + } +}; + async function getDeleter(topicData) { if (!parseInt(topicData.deleterUid, 10)) { return null; diff --git a/src/topics/posts.js b/src/topics/posts.js index 2384dead48..dc947c6e67 100644 --- a/src/topics/posts.js +++ b/src/topics/posts.js @@ -24,6 +24,7 @@ module.exports = function (Topics) { const postData = await posts.getPostsFromSet(set, start, stop, uid, reverse); Topics.calculatePostIndices(postData, start); + await Topics.addNextPostTimestamp(postData, set, reverse); return await Topics.addPostData(postData, uid); };