feat: do not show the replies container in a post's footer if the only reply present is the next post

isekai-main
Julian Lam 2 years ago
parent 0f84f597df
commit da02361b13

@ -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 {

@ -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, '&#44;');
}
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]);

@ -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,

Loading…
Cancel
Save