Share comment thread building between patch views

Revision and ReviewPage built threads and checked who may resolve a
comment with their own near-identical copies.
This commit is contained in:
Rūdolfs Ošiņš
2026-10-01 18:20:24 +02:00
parent a43031ad70
commit 974235b1fe
6 changed files with 461 additions and 202 deletions
+34 -59
View File
@@ -39,13 +39,18 @@
import { anchorOf } from "@app/lib/pierreComments";
import * as roles from "@app/lib/roles";
import { push } from "@app/lib/router";
import {
buildThreads,
groupThreadsByFile,
isCodeRoot,
reviewDiscussionThreads,
} from "@app/lib/threads";
import {
absoluteTimestamp,
authorForNodeId,
didFromPublicKey,
formatTimestamp,
pluralize,
publicKeyFromDid,
revisionPosition,
verdictAction,
verdictBadge,
@@ -247,20 +252,9 @@
}
}
// Comments on the review itself rather than on a line. The summary is a
// field, not the root of this thread, so a comment replying to the review id
// starts a root thread of its own.
const commentThreads: Thread[] = $derived.by(() => {
const comments = (review.comments ?? []) as Comment<CodeLocation>[];
return comments
.filter(c => (!c.location && !c.replyTo) || c.replyTo === review.id)
.map(root => ({
root,
replies: comments
.filter(c => c.replyTo === root.id)
.sort((a, b) => a.edits[0].timestamp - b.edits[0].timestamp),
})) as unknown as Thread[];
});
const commentThreads = $derived(
reviewDiscussionThreads(review) as unknown as Thread[],
);
async function createDiscussionComment(
body: string,
@@ -328,21 +322,15 @@
// revision comments have no resolve action at all.
function canResolveComment(commentId: string): boolean {
if (ownerOf(commentId)?.kind !== "review") return false;
if (
roles.isDelegate(
config.publicKey,
repoDelegates.map(d => d.did),
)
) {
return true;
}
const comment = (review.comments ?? []).find(c => c.id === commentId);
return [
comment?.author.did,
review.author.did,
reviewedRevision?.author.did,
].some(
did => did !== undefined && publicKeyFromDid(did) === config.publicKey,
return roles.canResolveReviewComment(
config.publicKey,
repoDelegates.map(d => d.did),
{
comment: (review.comments ?? []).find(c => c.id === commentId)?.author
.did,
review: review.author.did,
revision: reviewedRevision?.author.did,
},
);
}
@@ -357,35 +345,22 @@
}),
);
const fileGroups: FileGroup[] = $derived.by(() => {
// eslint-disable-next-line svelte/prefer-svelte-reactivity
const groups = new Map<string, Thread<CodeLocation>[]>();
const addThreads = (
comments: Comment<CodeLocation>[],
excludeId: string,
) => {
const roots = comments.filter(
c => c.location && !c.replyTo && c.id !== excludeId,
);
for (const root of roots) {
const replies = comments
.filter(c => c.replyTo === root.id)
.sort((a, b) => a.edits[0].timestamp - b.edits[0].timestamp);
const thread = { root, replies } as Thread<CodeLocation>;
const path = root.location!.path;
const list = groups.get(path) ?? [];
list.push(thread);
groups.set(path, list);
}
};
addThreads(review.comments as Comment<CodeLocation>[], review.id);
// Standalone code comments left directly on the reviewed revision (not part
// of the review itself) also belong on this page.
if (reviewedRevision?.discussion) {
addThreads(reviewedRevision.discussion, reviewedRevision.id);
}
return [...groups.entries()].map(([path, threads]) => ({ path, threads }));
});
// Standalone code comments left directly on the reviewed revision (not part
// of the review itself) also belong on this page.
const fileGroups: FileGroup[] = $derived(
groupThreadsByFile([
...buildThreads(
review.comments as Comment<CodeLocation>[],
c => isCodeRoot(c) && c.id !== review.id,
),
...(reviewedRevision
? buildThreads(
reviewedRevision.discussion ?? [],
c => isCodeRoot(c) && c.id !== reviewedRevision.id,
)
: []),
]),
);
// Standalone comments render next to the review's own and look identical,
// but nothing can resolve them and their edits target the revision, so they
+29 -143
View File
@@ -34,6 +34,16 @@
import { patchTimeline } from "@app/lib/patchTimeline";
import * as roles from "@app/lib/roles";
import { push } from "@app/lib/router";
import {
buildThreads,
codeThreadsByReview,
discussionThreadsByReview as discussionThreadsByReviewOf,
groupThreadsByFile,
isCodeRoot,
reviewIdByComment,
revisionDiscussionThreads,
revisionIdByComment,
} from "@app/lib/threads";
import {
authorForNodeId,
didFromPublicKey,
@@ -255,14 +265,7 @@
const hasPublishedReview = $derived(Boolean(ownPublishedReview));
const codeCommentThreads: Thread<CodeLocation>[] = $derived(
draftReview
? (draftReview.comments
.filter(c => c.location && !c.replyTo)
.map(root => ({
root,
replies: draftReview.comments.filter(c => c.replyTo === root.id),
})) as Thread<CodeLocation>[])
: [],
draftReview ? buildThreads(draftReview.comments, isCodeRoot) : [],
);
async function createCodeComment(
@@ -379,72 +382,18 @@
}
}
// One header per file with its hunks stacked beneath, rather than a
// duplicate file block per comment.
function groupThreadsByFile(
list: Thread<CodeLocation>[],
): { path: string; threads: Thread<CodeLocation>[] }[] {
// eslint-disable-next-line svelte/prefer-svelte-reactivity
const groups = new Map<string, Thread<CodeLocation>[]>();
for (const thread of list) {
const path = thread.root.location?.path;
if (!path) continue;
const existing = groups.get(path) ?? [];
existing.push(thread);
groups.set(path, existing);
}
return [...groups.entries()].map(([path, threads]) => ({ path, threads }));
}
// Comments on a review itself rather than on a line. Kept apart from
// `threadsByReview`, which feeds the diff and must only carry threads that
// can be anchored to a line.
const discussionThreadsByReview = $derived.by(() => {
// eslint-disable-next-line svelte/prefer-svelte-reactivity
const map = new Map<string, Thread<CodeLocation>[]>();
(revision.reviews ?? []).forEach(review => {
const comments = review.comments ?? [];
const threads = comments
.filter(c => (!c.location && !c.replyTo) || c.replyTo === review.id)
.map(root => ({
root,
replies: comments
.filter(c => c.replyTo === root.id)
.sort((a, b) => a.edits[0].timestamp - b.edits[0].timestamp),
})) as Thread<CodeLocation>[];
if (threads.length > 0) {
map.set(review.id, threads);
}
});
return map;
});
const discussionThreadsByReview = $derived(
discussionThreadsByReviewOf(revision.reviews ?? []),
);
const commentToReviewId = $derived.by(() => {
// eslint-disable-next-line svelte/prefer-svelte-reactivity
const map = new Map<string, string>();
for (const source of [threadsByReview, discussionThreadsByReview]) {
for (const [reviewId, threads] of source.entries()) {
for (const thread of threads) {
map.set(thread.root.id, reviewId);
for (const reply of thread.replies) {
map.set(reply.id, reviewId);
}
}
}
}
return map;
});
const commentToReviewId = $derived.by(() =>
reviewIdByComment(threadsByReview, discussionThreadsByReview),
);
const commentToRevisionId = $derived.by(() => {
// eslint-disable-next-line svelte/prefer-svelte-reactivity
const map = new Map<string, string>();
for (const rev of revisions) {
for (const comment of rev.discussion ?? []) {
map.set(comment.id, rev.id);
}
}
return map;
});
const commentToRevisionId = $derived(revisionIdByComment(revisions));
// Every code-comment mutation dispatches on the owning object; resolving it
// once here keeps the actions shared.
@@ -473,18 +422,15 @@
function canResolveComment(commentId: string): boolean {
const owner = ownerOf(commentId);
if (owner?.kind !== "review") return false;
if (
roles.isDelegate(
config.publicKey,
repoDelegates.map(d => d.did),
)
) {
return true;
}
const review = (revision.reviews ?? []).find(r => r.id === owner.reviewId);
const comment = review?.comments?.find(c => c.id === commentId);
return [comment?.author.did, review?.author.did, revision.author.did].some(
did => did !== undefined && publicKeyFromDid(did) === config.publicKey,
return roles.canResolveReviewComment(
config.publicKey,
repoDelegates.map(d => d.did),
{
comment: review?.comments?.find(c => c.id === commentId)?.author.did,
review: review?.author.did,
revision: revision.author.did,
},
);
}
@@ -538,15 +484,7 @@
// render on the diff alongside review comments instead of vanishing.
const revisionCodeCommentThreads: Thread<CodeLocation>[] = $derived.by(() => {
if (hiddenCommentSources.includes(STANDALONE_COMMENTS)) return [];
const discussion = revision.discussion ?? [];
return discussion
.filter(c => c.location && !c.replyTo)
.map(root => ({
root,
replies: discussion
.filter(c => c.replyTo === root.id)
.sort((a, b) => a.edits[0].timestamp - b.edits[0].timestamp),
})) as Thread<CodeLocation>[];
return buildThreads(revision.discussion ?? [], isCodeRoot);
});
const codeComments: CodeComments | undefined = $derived.by(() => {
@@ -643,25 +581,7 @@
new Set(revisions.filter(r => r.id !== latestRevisionId).map(r => r.id)),
);
const threadsByReview = $derived.by(() => {
// eslint-disable-next-line svelte/prefer-svelte-reactivity
const map = new Map<string, Thread<CodeLocation>[]>();
(revision.reviews ?? []).forEach(review => {
const reviewComments = review.comments ?? [];
const threads = reviewComments
.filter(c => c.location && !c.replyTo)
.map(root => {
const replies = reviewComments
.filter(c => c.replyTo === root.id)
.sort((a, b) => a.edits[0].timestamp - b.edits[0].timestamp);
return { root, replies } as Thread<CodeLocation>;
});
if (threads.length > 0) {
map.set(review.id, threads);
}
});
return map;
});
const threadsByReview = $derived(codeThreadsByReview(revision.reviews ?? []));
const activityItems = $derived(
patchTimeline({
@@ -673,45 +593,11 @@
expandedRevisionRuns,
}),
);
const reviewSummaryFingerprints = $derived(
new Set(
revisions
.flatMap(r => r.reviews ?? [])
.filter(r => r.summary && r.summary.trim() !== "")
.map(r => `${r.author.did} ${r.summary}`),
),
);
// Gather discussion comments from every revision, not just the selected one,
// so comments left on a revision stay in the timeline after a newer revision
// is pushed. Discussion.svelte orders the merged timeline by timestamp.
const commentThreads = $derived(
revisions.flatMap(rev => {
const discussion = rev.discussion;
if (!discussion) return [];
return (
discussion
.filter(
comment =>
(comment.id !== rev.id && !comment.replyTo) ||
comment.replyTo === rev.id,
)
// Code comments (those with a location) render on the diff, not as
// plain entries in the activity timeline.
.filter(comment => !comment.location)
.filter(comment => {
const body = comment.edits[comment.edits.length - 1]?.body ?? "";
return !reviewSummaryFingerprints.has(
`${comment.author.did} ${body}`,
);
})
.map(thread => ({
root: thread,
replies: discussion
.filter(comment => comment.replyTo === thread.id)
.sort((a, b) => a.edits[0].timestamp - b.edits[0].timestamp),
}))
);
}) as Thread[],
revisionDiscussionThreads(revisions) as unknown as Thread[],
);
async function editRevision(
+17
View File
@@ -24,3 +24,20 @@ export function isDelegateOrAuthor(
undefined
);
}
/// The protocol lets the comment author, the review author or the revision
/// author resolve a review comment; delegates may do anything.
export function canResolveReviewComment(
publicKey: string,
delegates: string[],
authors: {
comment: string | undefined;
review: string | undefined;
revision: string | undefined;
},
): boolean {
if (isDelegate(publicKey, delegates)) return true;
return [authors.comment, authors.review, authors.revision].some(
did => did !== undefined && publicKeyFromDid(did) === publicKey,
);
}
+132
View File
@@ -0,0 +1,132 @@
import type { Review } from "@bindings/cob/patch/Review";
import type { Revision } from "@bindings/cob/patch/Revision";
import type { CodeLocation } from "@bindings/cob/thread/CodeLocation";
import type { Comment } from "@bindings/cob/thread/Comment";
import type { Thread } from "@bindings/cob/thread/Thread";
/// Threads rooted at the comments `isRoot` picks, each with its replies oldest
/// first.
export function buildThreads<T>(
comments: Comment<T>[],
isRoot: (comment: Comment<T>) => boolean,
): Thread<T>[] {
return comments.filter(isRoot).map(root => ({
root,
replies: comments
.filter(c => c.replyTo === root.id)
.sort((a, b) => a.edits[0].timestamp - b.edits[0].timestamp),
}));
}
/// A code comment that starts a thread.
export function isCodeRoot(comment: Comment<CodeLocation>): boolean {
return Boolean(comment.location) && !comment.replyTo;
}
/// Each review's code threads, for reviews that have any.
export function codeThreadsByReview(
reviews: Review[],
): Map<string, Thread<CodeLocation>[]> {
const map = new Map<string, Thread<CodeLocation>[]>();
for (const review of reviews) {
const threads = buildThreads(review.comments ?? [], isCodeRoot);
if (threads.length > 0) map.set(review.id, threads);
}
return map;
}
/// Comments on a review itself rather than on a line. The summary is a field,
/// not the root of this thread, so a comment replying to the review id starts
/// a thread of its own.
export function reviewDiscussionThreads(
review: Review,
): Thread<CodeLocation>[] {
return buildThreads(
review.comments ?? [],
c => (!c.location && !c.replyTo) || c.replyTo === review.id,
);
}
/// Each review's discussion threads, for reviews that have any.
export function discussionThreadsByReview(
reviews: Review[],
): Map<string, Thread<CodeLocation>[]> {
const map = new Map<string, Thread<CodeLocation>[]>();
for (const review of reviews) {
const threads = reviewDiscussionThreads(review);
if (threads.length > 0) map.set(review.id, threads);
}
return map;
}
/// Which review each comment in these threads belongs to.
export function reviewIdByComment(
...sources: Map<string, Thread<CodeLocation>[]>[]
): Map<string, string> {
const map = new Map<string, string>();
for (const source of sources) {
for (const [reviewId, threads] of source) {
for (const thread of threads) {
map.set(thread.root.id, reviewId);
for (const reply of thread.replies) map.set(reply.id, reviewId);
}
}
}
return map;
}
/// Which revision each discussion comment was left on.
export function revisionIdByComment(
revisions: Revision[],
): Map<string, string> {
const map = new Map<string, string>();
for (const revision of revisions) {
for (const comment of revision.discussion ?? []) {
map.set(comment.id, revision.id);
}
}
return map;
}
/// One group per file, in order of first appearance, so a file's threads
/// render under a single header.
export function groupThreadsByFile(
threads: Thread<CodeLocation>[],
): { path: string; threads: Thread<CodeLocation>[] }[] {
const groups = new Map<string, Thread<CodeLocation>[]>();
for (const thread of threads) {
const path = thread.root.location?.path;
if (!path) continue;
const group = groups.get(path) ?? [];
group.push(thread);
groups.set(path, group);
}
return [...groups.entries()].map(([path, threads]) => ({ path, threads }));
}
/// The discussion threads of every revision, for the activity timeline.
///
/// Code comments render on the diff instead. A comment whose body repeats one
/// of its author's review summaries is left out too, since the review already
/// shows that text.
export function revisionDiscussionThreads(
revisions: Revision[],
): Thread<CodeLocation>[] {
const summaries = new Set(
revisions
.flatMap(r => r.reviews ?? [])
.filter(r => r.summary && r.summary.trim() !== "")
.map(r => `${r.author.did} ${r.summary}`),
);
return revisions.flatMap(revision =>
buildThreads(revision.discussion ?? [], comment => {
const body = comment.edits[comment.edits.length - 1]?.body ?? "";
return (
((comment.id !== revision.id && !comment.replyTo) ||
comment.replyTo === revision.id) &&
!comment.location &&
!summaries.has(`${comment.author.did} ${body}`)
);
}),
);
}