Skip to content
20 linesCodeBlameRaw
1import type { Comment } from "@g1t/contracts";
2
3/**
4 * Whether the viewer may edit or delete a comment: its author may, and so
5 * may someone with the Maintain role or higher (`canModerate`). Notes of
6 * what happened are nobody's to change, and a review's verdict stays, so a
7 * review can be edited but not deleted. The work service decides; this
8 * only says which buttons to show.
9 */
10export function mayChangeComment(
11 comment: Pick<Comment, "kind" | "verdict"> &
12 Partial<Pick<Comment, "advisory">> & { author: { id: string }; actingFor?: { id: string } | null },
13 viewerId: string | null | undefined,
14 canModerate: boolean,
15): { edit: boolean; delete: boolean } {
16 // What an agent wrote as itself is answered for by whoever it acted for.
17 const answerable = comment.actingFor?.id ?? comment.author.id;
18 const allowed = comment.kind === "comment" && viewerId != null && (answerable === viewerId || canModerate);
19 return { edit: allowed, delete: allowed && comment.verdict == null && !comment.advisory };
20}