Node.js based forum software built for the modern web
feat: do not show the replies container in a post's footer if the only reply present is the next post
Julian Lamda02361b13d0
diff --git a/public/src/client/topic/replies.js b/public/src/client/topic/replies.jsindex 8e0573ab04..92c2d36e39 100644--- a/public/src/client/topic/replies.js+++ b/public/src/client/topic/replies.js@@ -1,7 +1,7 @@'use strict';-define('forum/topic/replies', ['forum/topic/posts', 'hooks', 'alerts'], function (posts, hooks, alerts) {+define('forum/topic/replies', ['forum/topic/posts', 'hooks', 'alerts', 'components'], function (posts, hooks, alerts, components) {const Replies = {};Replies.init = function (button) {@@ -85,14 +85,22 @@ define('forum/topic/replies', ['forum/topic/posts', 'hooks', 'alerts'], function};function incrementCount(post, inc) {+ const postEl = document.querySelector(`[component="post"][data-pid="${post.toPid}"]`);+ if (!postEl) {+ return;+ }+const replyCount = $('[component="post"][data-pid="' + post.toPid + '"]').find('[component="post/reply-count"]').first();const countEl = replyCount.find('[component="post/reply-count/text"]');const avatars = replyCount.find('[component="post/reply-count/avatars"]');const count = Math.max(0, (parseInt(countEl.attr('data-replies'), 10) || 0) + inc);const timestamp = replyCount.find('.timeago').attr('title', post.timestampISO);+ const index = postEl.getAttribute('data-index');+ const hasSingleImmediateReply = count === 1 && Math.abs(post.index - index) === 1;+countEl.attr('data-replies', count);- replyCount.toggleClass('hidden', count <= 0);+ replyCount.toggleClass('hidden', count <= 0 || hasSingleImmediateReply);if (count > 1) {countEl.translateText('[[topic:replies_to_this_post, ' + count + ']]');} else {diff --git a/public/src/modules/helpers.common.js b/public/src/modules/helpers.common.jsindex 32a8665f4e..d607d7e20f 100644--- a/public/src/modules/helpers.common.js+++ b/public/src/modules/helpers.common.js@@ -27,8 +27,10 @@ module.exports = function (utils, Benchpress, relative_path) {increment,generateRepliedTo,generateWrote,- register,isoTimeToLocaleString,+ shouldHideReplyContainer,++ register,__escape: identity,};@@ -342,6 +344,14 @@ module.exports = function (utils, Benchpress, relative_path) {return new Date(isoTime).toLocaleString().replace(/,/g, ',');}+ function shouldHideReplyContainer(post) {+ if (post.replies.count <= 0 || post.replies.hasSingleImmediateReply) {+ return true;+ }++ return false;+ }+function register() {Object.keys(helpers).forEach(function (helperName) {Benchpress.registerHelper(helperName, helpers[helperName]);diff --git a/src/topics/posts.js b/src/topics/posts.jsindex ce56931481..fd1dc94889 100644--- a/src/topics/posts.js+++ b/src/topics/posts.js@@ -319,6 +319,9 @@ module.exports = function (Topics) {const arrayOfReplyPids = await db.getSortedSetsMembers(keys);const uniquePids = _.uniq(_.flatten(arrayOfReplyPids));+ const someTid = await posts.getPostField(pids[0], 'tid'); // the particular tid doesn't matter; used in getPostIndices but does not affect output+ const pidIndices = await posts.getPostIndices(pids.map(pid => ({ pid, tid: someTid })));+ const replyIndices = await posts.getPostIndices(uniquePids.map(pid => ({ pid, tid: someTid })));let replyData = await posts.getPostsFields(uniquePids, ['pid', 'uid', 'timestamp']);const result = await plugins.hooks.fire('filter:topics.getPostReplies', {@@ -335,12 +338,15 @@ module.exports = function (Topics) {const uidMap = _.zipObject(uniqueUids, userData);const pidMap = _.zipObject(replyData.map(r => r.pid), replyData);+ const indicesMap = _.zipObject(replyData.map(r => r.pid), replyIndices);- const returnData = arrayOfReplyPids.map((replyPids) => {+ const returnData = arrayOfReplyPids.map((replyPids, idx) => {replyPids = replyPids.filter(pid => pidMap[pid]);+ const currentIndex = pidIndices[idx];const uidsUsed = {};const currentData = {hasMore: false,+ hasSingleImmediateReply: replyPids.length === 1 && Math.abs(currentIndex - indicesMap[replyPids[0]]) === 1,users: [],text: replyPids.length > 1 ? `[[topic:replies_to_this_post, ${replyPids.length}]]` : '[[topic:one_reply_to_this_post]]',count: replyPids.length,