mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-29 10:10:30 +08:00
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:
@@ -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
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user