From 9382fc6dc5fc1179e193a88e662a63d58fef570e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 11 Mar 2021 19:17:42 -0500 Subject: [PATCH] fix: #9370, show correct teaser index if sorting is newest to oldest --- src/topics/teaser.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/topics/teaser.js b/src/topics/teaser.js index 24e6d5d775..11473f4d64 100644 --- a/src/topics/teaser.js +++ b/src/topics/teaser.js @@ -43,12 +43,15 @@ module.exports = function (Topics) { } }); - let postData = await posts.getPostsFields(teaserPids, ['pid', 'uid', 'timestamp', 'tid', 'content']); - postData = postData.filter(post => post && post.pid); + const [allPostData, callerSettings] = await Promise.all([ + posts.getPostsFields(teaserPids, ['pid', 'uid', 'timestamp', 'tid', 'content']), + user.getSettings(uid), + ]); + let postData = allPostData.filter(post => post && post.pid); postData = await handleBlocks(uid, postData); postData = postData.filter(Boolean); const uids = _.uniq(postData.map(post => post.uid)); - + const sortNewToOld = callerSettings.topicPostSort === 'newest_to_oldest'; const usersData = await user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture']); const users = {}; @@ -75,7 +78,7 @@ module.exports = function (Topics) { return null; } if (tidToPost[topic.tid]) { - tidToPost[topic.tid].index = meta.config.teaserPost === 'first' ? 1 : counts[index]; + tidToPost[topic.tid].index = calcTeaserIndex(teaserPost, counts[index], sortNewToOld); if (tidToPost[topic.tid].content) { tidToPost[topic.tid].content = utils.stripHTMLTags(replaceImgWithAltText(tidToPost[topic.tid].content), tags); } @@ -87,6 +90,17 @@ module.exports = function (Topics) { return result.teasers; }; + function calcTeaserIndex(teaserPost, postCountInTopic, sortNewToOld) { + if (teaserPost === 'first') { + return 1; + } + + if (sortNewToOld) { + return Math.min(2, postCountInTopic); + } + return postCountInTopic; + } + function replaceImgWithAltText(str) { return String(str).replace(/]*>/gi, '$1'); }