Files
wekan/imports/lib/commentThreading.js
T
Lauri Ojansivu ab1f60d806 Cap comment replies at one level of threading, grouped under their parent.
A comment's Reply link (added earlier for #5907) let a reply be posted
against ANY comment, including another reply, so clicking Reply enough
times nested comments arbitrarily deep. For #3011, a reply now attaches
to a reply's own parent instead of the reply itself - resolveParentId()
(models/cardComments.js) makes that decision once, is unit tested, and
is applied both when the client opens the composer (so the "In reply
to ..." banner already names the right comment) and again in a
CardComments.before.insert hook server-side, so the cap holds no
matter how a comment is inserted.

Replies also now render directly under their top-level parent instead
of interleaved by date with unrelated comments: groupCommentsByThread()
(imports/lib/commentThreading.js) reorders the already-sorted flat list
the comments template already used, with no schema or query change.

The "Replying to <author>" indicator on the composer reuses the
existing, already fully translated comment-in-reply-to string plus the
parent comment's author name, rather than adding a new i18n key that
would need translating across every locale file for a small wording
difference.

Added tests/commentReplyFlattening.test.cjs (the one-level cap, both
the client and server wiring) and tests/commentThreadGrouping.test.cjs
(grouping, including the regression cases: a flat list of only
top-level comments is unchanged, and a comment whose parent was
deleted still renders as top-level rather than being dropped).

Thanks to FuXXz and xet7 !

Closes #3011,
2026-09-10 20:28:46 +03:00

52 lines
1.7 KiB
JavaScript

// Pure helper for issue #3011: group a flat, date-sorted list of card
// comments so each reply renders directly under its (flattened, one-level)
// parent instead of interleaved by date with unrelated top-level comments.
//
// No Meteor imports on purpose - it is plain array logic, unit-testable with
// plain node (see tests/commentThreading.test.cjs), and reused as-is by the
// `comments` Blaze template (client/components/activities/comments.js).
//
// Threading is already capped at one level before a comment ever reaches
// here (models/cardComments.js `resolveParentId`, enforced both client-side
// on reply-click and server-side on insert), so a comment's parentId - when
// set - always points at a genuine top-level comment. This function does not
// re-flatten; it only groups. A parentId that does not match any comment in
// the list (e.g. the parent was deleted) falls back to rendering as
// top-level, rather than being silently dropped.
export function groupCommentsByThread(comments) {
if (!Array.isArray(comments)) {
return [];
}
const byId = new Set(comments.map(c => c._id));
const repliesByParent = new Map();
const topLevel = [];
comments.forEach(comment => {
const parentId = comment.parentId && byId.has(comment.parentId)
? comment.parentId
: '';
if (parentId) {
if (!repliesByParent.has(parentId)) {
repliesByParent.set(parentId, []);
}
repliesByParent.get(parentId).push(comment);
} else {
topLevel.push(comment);
}
});
const grouped = [];
topLevel.forEach(comment => {
grouped.push(comment);
const replies = repliesByParent.get(comment._id);
if (replies) {
grouped.push(...replies);
}
});
return grouped;
}
export default groupCommentsByThread;