Make deleted-comment cleanup atomic
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -270,4 +270,48 @@ describeEmbeddedPostgres("documentAnnotationService", () => {
|
|||||||
|
|
||||||
await expect(db.select().from(documentAnnotationComments)).resolves.toHaveLength(0);
|
await expect(db.select().from(documentAnnotationComments)).resolves.toHaveLength(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("does not report already-resolved empty threads as newly resolved during linked comment cleanup", async () => {
|
||||||
|
const { companyId, issueId, document } = await createIssueWithDocument();
|
||||||
|
const [issueComment] = await db
|
||||||
|
.insert(issueComments)
|
||||||
|
.values({
|
||||||
|
companyId,
|
||||||
|
issueId,
|
||||||
|
authorType: "user",
|
||||||
|
authorUserId: "board-user",
|
||||||
|
body: "Delete this linked comment from a resolved thread",
|
||||||
|
})
|
||||||
|
.returning();
|
||||||
|
|
||||||
|
const thread = await annotations.createThread(
|
||||||
|
issueId,
|
||||||
|
"plan",
|
||||||
|
{
|
||||||
|
baseRevisionId: document.latestRevisionId!,
|
||||||
|
baseRevisionNumber: document.latestRevisionNumber,
|
||||||
|
selector: {
|
||||||
|
quote: { exact: "selected text", prefix: "Alpha ", suffix: " omega" },
|
||||||
|
position: { normalizedStart: 6, normalizedEnd: 19, markdownStart: 6, markdownEnd: 19 },
|
||||||
|
},
|
||||||
|
body: "Linked annotation body",
|
||||||
|
issueCommentId: issueComment.id,
|
||||||
|
},
|
||||||
|
{ actorType: "user", actorId: "board-user", userId: "board-user" },
|
||||||
|
);
|
||||||
|
|
||||||
|
await db
|
||||||
|
.update(documentAnnotationThreads)
|
||||||
|
.set({ status: "resolved", resolvedByUserId: "board-user", resolvedAt: new Date("2026-06-05T03:05:00.000Z") })
|
||||||
|
.where(eq(documentAnnotationThreads.id, thread.id));
|
||||||
|
|
||||||
|
const cleanup = await annotations.cleanupForIssueCommentDeletion(
|
||||||
|
issueId,
|
||||||
|
issueComment.id,
|
||||||
|
{ actorType: "user", actorId: "board-user", userId: "board-user" },
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(cleanup.deletedCommentIds).toEqual([thread.comments[0]!.id]);
|
||||||
|
expect(cleanup.resolvedThreadIds).toEqual([]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -195,15 +195,19 @@ describe.sequential("issue comment cancel routes", () => {
|
|||||||
mockIssueService.assertCheckoutOwner.mockResolvedValue({ adoptedFromRunId: null });
|
mockIssueService.assertCheckoutOwner.mockResolvedValue({ adoptedFromRunId: null });
|
||||||
mockIssueService.getComment.mockResolvedValue(makeComment());
|
mockIssueService.getComment.mockResolvedValue(makeComment());
|
||||||
mockIssueService.removeComment.mockResolvedValue(makeComment());
|
mockIssueService.removeComment.mockResolvedValue(makeComment());
|
||||||
mockIssueService.tombstoneComment.mockResolvedValue(makeComment({
|
mockIssueService.tombstoneComment.mockImplementation(async (_commentId, _actor, options) => {
|
||||||
body: "",
|
const deleted = makeComment({
|
||||||
metadata: null,
|
body: "",
|
||||||
deletedAt: new Date("2026-04-11T15:05:00.000Z"),
|
metadata: null,
|
||||||
deletedByType: "user",
|
deletedAt: new Date("2026-04-11T15:05:00.000Z"),
|
||||||
deletedByAgentId: null,
|
deletedByType: "user",
|
||||||
deletedByUserId: "local-board",
|
deletedByAgentId: null,
|
||||||
deletedByRunId: null,
|
deletedByUserId: "local-board",
|
||||||
}));
|
deletedByRunId: null,
|
||||||
|
});
|
||||||
|
await options?.afterTombstone?.(deleted, "tx");
|
||||||
|
return deleted;
|
||||||
|
});
|
||||||
mockAccessService.canUser.mockResolvedValue(false);
|
mockAccessService.canUser.mockResolvedValue(false);
|
||||||
mockAccessService.hasPermission.mockResolvedValue(false);
|
mockAccessService.hasPermission.mockResolvedValue(false);
|
||||||
mockFeedbackService.listIssueVotesForUser.mockResolvedValue([]);
|
mockFeedbackService.listIssueVotesForUser.mockResolvedValue([]);
|
||||||
@@ -337,13 +341,17 @@ describe.sequential("issue comment cancel routes", () => {
|
|||||||
expect(JSON.stringify(res.body)).not.toContain("Sensitive original comment body");
|
expect(JSON.stringify(res.body)).not.toContain("Sensitive original comment body");
|
||||||
expect(JSON.stringify(res.body)).not.toContain("Sensitive metadata copy");
|
expect(JSON.stringify(res.body)).not.toContain("Sensitive metadata copy");
|
||||||
expect(mockIssueService.removeComment).not.toHaveBeenCalled();
|
expect(mockIssueService.removeComment).not.toHaveBeenCalled();
|
||||||
expect(mockIssueService.tombstoneComment).toHaveBeenCalledWith("comment-1", {
|
expect(mockIssueService.tombstoneComment).toHaveBeenCalledWith(
|
||||||
actorType: "user",
|
"comment-1",
|
||||||
agentId: null,
|
{
|
||||||
userId: "local-board",
|
actorType: "user",
|
||||||
runId: null,
|
agentId: null,
|
||||||
});
|
userId: "local-board",
|
||||||
expect(mockIssueReferenceService.syncComment).toHaveBeenCalledWith("comment-1");
|
runId: null,
|
||||||
|
},
|
||||||
|
expect.objectContaining({ afterTombstone: expect.any(Function) }),
|
||||||
|
);
|
||||||
|
expect(mockIssueReferenceService.syncComment).toHaveBeenCalledWith("comment-1", "tx");
|
||||||
expect(mockDocumentAnnotationService.cleanupForIssueCommentDeletion).toHaveBeenCalledWith(
|
expect(mockDocumentAnnotationService.cleanupForIssueCommentDeletion).toHaveBeenCalledWith(
|
||||||
"11111111-1111-4111-8111-111111111111",
|
"11111111-1111-4111-8111-111111111111",
|
||||||
"comment-1",
|
"comment-1",
|
||||||
@@ -351,8 +359,9 @@ describe.sequential("issue comment cancel routes", () => {
|
|||||||
actorType: "user",
|
actorType: "user",
|
||||||
userId: "local-board",
|
userId: "local-board",
|
||||||
}),
|
}),
|
||||||
|
"tx",
|
||||||
);
|
);
|
||||||
expect(mockIssueReferenceService.deleteCommentSource).toHaveBeenCalledWith("annotation-comment-1");
|
expect(mockIssueReferenceService.deleteCommentSource).toHaveBeenCalledWith("annotation-comment-1", "tx");
|
||||||
const deletedActivity = mockLogActivity.mock.calls.find((call) => call[1]?.action === "issue.comment_deleted")?.[1];
|
const deletedActivity = mockLogActivity.mock.calls.find((call) => call[1]?.action === "issue.comment_deleted")?.[1];
|
||||||
expect(deletedActivity).toEqual(expect.objectContaining({
|
expect(deletedActivity).toEqual(expect.objectContaining({
|
||||||
action: "issue.comment_deleted",
|
action: "issue.comment_deleted",
|
||||||
|
|||||||
+27
-19
@@ -5674,29 +5674,37 @@ export function issueRoutes(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const deleted = await svc.tombstoneComment(commentId, {
|
let annotationCleanup = { deletedCommentIds: [] as string[], resolvedThreadIds: [] as string[] };
|
||||||
actorType: actor.actorType,
|
const deleted = await svc.tombstoneComment(
|
||||||
agentId: actor.agentId,
|
commentId,
|
||||||
userId: actor.actorType === "user" ? actor.actorId : null,
|
{
|
||||||
runId: actor.runId,
|
actorType: actor.actorType,
|
||||||
});
|
agentId: actor.agentId,
|
||||||
|
userId: actor.actorType === "user" ? actor.actorId : null,
|
||||||
|
runId: actor.runId,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
afterTombstone: async (deletedComment, tx) => {
|
||||||
|
await issueReferencesSvc.syncComment(deletedComment.id, tx);
|
||||||
|
annotationCleanup = await documentAnnotationsSvc.cleanupForIssueCommentDeletion(issue.id, deletedComment.id, {
|
||||||
|
actorType: actor.actorType,
|
||||||
|
actorId: actor.actorId,
|
||||||
|
agentId: actor.agentId,
|
||||||
|
userId: actor.actorType === "user" ? actor.actorId : null,
|
||||||
|
runId: actor.runId,
|
||||||
|
}, tx);
|
||||||
|
await Promise.all(
|
||||||
|
annotationCleanup.deletedCommentIds.map((annotationCommentId) =>
|
||||||
|
issueReferencesSvc.deleteCommentSource(annotationCommentId, tx)
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
if (!deleted) {
|
if (!deleted) {
|
||||||
res.status(404).json({ error: "Comment not found" });
|
res.status(404).json({ error: "Comment not found" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await issueReferencesSvc.syncComment(deleted.id);
|
|
||||||
const annotationCleanup = await documentAnnotationsSvc.cleanupForIssueCommentDeletion(issue.id, deleted.id, {
|
|
||||||
actorType: actor.actorType,
|
|
||||||
actorId: actor.actorId,
|
|
||||||
agentId: actor.agentId,
|
|
||||||
userId: actor.actorType === "user" ? actor.actorId : null,
|
|
||||||
runId: actor.runId,
|
|
||||||
});
|
|
||||||
await Promise.all(
|
|
||||||
annotationCleanup.deletedCommentIds.map((annotationCommentId) =>
|
|
||||||
issueReferencesSvc.deleteCommentSource(annotationCommentId)
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
await logActivity(db, {
|
await logActivity(db, {
|
||||||
companyId: issue.companyId,
|
companyId: issue.companyId,
|
||||||
|
|||||||
@@ -330,58 +330,66 @@ export function documentAnnotationService(db: Db) {
|
|||||||
issueId: string,
|
issueId: string,
|
||||||
issueCommentId: string,
|
issueCommentId: string,
|
||||||
actor: ActorInput,
|
actor: ActorInput,
|
||||||
) => db.transaction(async (tx) => {
|
dbOrTx: any = db,
|
||||||
const linkedComments: Array<Pick<DocumentAnnotationComment, "id" | "threadId">> = await tx
|
) => {
|
||||||
.select({
|
const runCleanup = async (tx: any) => {
|
||||||
id: documentAnnotationComments.id,
|
const linkedComments: Array<Pick<DocumentAnnotationComment, "id" | "threadId">> = await tx
|
||||||
threadId: documentAnnotationComments.threadId,
|
.select({
|
||||||
})
|
id: documentAnnotationComments.id,
|
||||||
.from(documentAnnotationComments)
|
threadId: documentAnnotationComments.threadId,
|
||||||
.where(and(
|
|
||||||
eq(documentAnnotationComments.issueId, issueId),
|
|
||||||
eq(documentAnnotationComments.issueCommentId, issueCommentId),
|
|
||||||
));
|
|
||||||
if (linkedComments.length === 0) {
|
|
||||||
return { deletedCommentIds: [], resolvedThreadIds: [] };
|
|
||||||
}
|
|
||||||
|
|
||||||
const deletedCommentIds = linkedComments.map((comment) => comment.id);
|
|
||||||
const threadIds = [...new Set(linkedComments.map((comment) => comment.threadId))];
|
|
||||||
const now = new Date();
|
|
||||||
|
|
||||||
await tx
|
|
||||||
.delete(documentAnnotationComments)
|
|
||||||
.where(inArray(documentAnnotationComments.id, deletedCommentIds));
|
|
||||||
|
|
||||||
const remainingRows: Array<{ threadId: string; count: number }> = await tx
|
|
||||||
.select({
|
|
||||||
threadId: documentAnnotationComments.threadId,
|
|
||||||
count: sql<number>`count(*)::int`,
|
|
||||||
})
|
|
||||||
.from(documentAnnotationComments)
|
|
||||||
.where(inArray(documentAnnotationComments.threadId, threadIds))
|
|
||||||
.groupBy(documentAnnotationComments.threadId);
|
|
||||||
const remainingByThreadId = new Map(remainingRows.map((row) => [row.threadId, Number(row.count)]));
|
|
||||||
const emptyThreadIds = threadIds.filter((threadId) => (remainingByThreadId.get(threadId) ?? 0) === 0);
|
|
||||||
|
|
||||||
if (emptyThreadIds.length > 0) {
|
|
||||||
await tx
|
|
||||||
.update(documentAnnotationThreads)
|
|
||||||
.set({
|
|
||||||
status: "resolved",
|
|
||||||
resolvedByAgentId: actor.actorType === "agent" ? actor.agentId ?? null : null,
|
|
||||||
resolvedByUserId: actor.actorType === "user" ? actor.userId ?? null : null,
|
|
||||||
resolvedAt: now,
|
|
||||||
updatedAt: now,
|
|
||||||
})
|
})
|
||||||
|
.from(documentAnnotationComments)
|
||||||
.where(and(
|
.where(and(
|
||||||
inArray(documentAnnotationThreads.id, emptyThreadIds),
|
eq(documentAnnotationComments.issueId, issueId),
|
||||||
eq(documentAnnotationThreads.status, "open"),
|
eq(documentAnnotationComments.issueCommentId, issueCommentId),
|
||||||
));
|
));
|
||||||
}
|
if (linkedComments.length === 0) {
|
||||||
|
return { deletedCommentIds: [], resolvedThreadIds: [] };
|
||||||
|
}
|
||||||
|
|
||||||
return { deletedCommentIds, resolvedThreadIds: emptyThreadIds };
|
const deletedCommentIds = linkedComments.map((comment) => comment.id);
|
||||||
}),
|
const threadIds = [...new Set(linkedComments.map((comment) => comment.threadId))];
|
||||||
|
const now = new Date();
|
||||||
|
|
||||||
|
await tx
|
||||||
|
.delete(documentAnnotationComments)
|
||||||
|
.where(inArray(documentAnnotationComments.id, deletedCommentIds));
|
||||||
|
|
||||||
|
const remainingRows: Array<{ threadId: string; count: number }> = await tx
|
||||||
|
.select({
|
||||||
|
threadId: documentAnnotationComments.threadId,
|
||||||
|
count: sql<number>`count(*)::int`,
|
||||||
|
})
|
||||||
|
.from(documentAnnotationComments)
|
||||||
|
.where(inArray(documentAnnotationComments.threadId, threadIds))
|
||||||
|
.groupBy(documentAnnotationComments.threadId);
|
||||||
|
const remainingByThreadId = new Map(remainingRows.map((row) => [row.threadId, Number(row.count)]));
|
||||||
|
const emptyThreadIds = threadIds.filter((threadId) => (remainingByThreadId.get(threadId) ?? 0) === 0);
|
||||||
|
|
||||||
|
if (emptyThreadIds.length > 0) {
|
||||||
|
const resolvedRows: Array<{ id: string }> = await tx
|
||||||
|
.update(documentAnnotationThreads)
|
||||||
|
.set({
|
||||||
|
status: "resolved",
|
||||||
|
resolvedByAgentId: actor.actorType === "agent" ? actor.agentId ?? null : null,
|
||||||
|
resolvedByUserId: actor.actorType === "user" ? actor.userId ?? null : null,
|
||||||
|
resolvedAt: now,
|
||||||
|
updatedAt: now,
|
||||||
|
})
|
||||||
|
.where(and(
|
||||||
|
inArray(documentAnnotationThreads.id, emptyThreadIds),
|
||||||
|
eq(documentAnnotationThreads.status, "open"),
|
||||||
|
))
|
||||||
|
.returning({ id: documentAnnotationThreads.id });
|
||||||
|
|
||||||
|
return { deletedCommentIds, resolvedThreadIds: resolvedRows.map((row) => row.id) };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { deletedCommentIds, resolvedThreadIds: [] };
|
||||||
|
};
|
||||||
|
|
||||||
|
return dbOrTx === db ? db.transaction(runCleanup) : runCleanup(dbOrTx);
|
||||||
|
},
|
||||||
|
|
||||||
updateThread: async (
|
updateThread: async (
|
||||||
issueId: string,
|
issueId: string,
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ import {
|
|||||||
} from "@paperclipai/db";
|
} from "@paperclipai/db";
|
||||||
import type {
|
import type {
|
||||||
AcceptedPlanDecomposition,
|
AcceptedPlanDecomposition,
|
||||||
|
IssueComment,
|
||||||
IssueCommentAuthorType,
|
IssueCommentAuthorType,
|
||||||
IssueCommentMetadata,
|
IssueCommentMetadata,
|
||||||
IssueCommentPresentation,
|
IssueCommentPresentation,
|
||||||
@@ -5733,6 +5734,9 @@ export function issueService(db: Db) {
|
|||||||
userId?: string | null;
|
userId?: string | null;
|
||||||
runId?: string | null;
|
runId?: string | null;
|
||||||
},
|
},
|
||||||
|
options?: {
|
||||||
|
afterTombstone?: (comment: IssueComment, tx: any) => Promise<void>;
|
||||||
|
},
|
||||||
) => {
|
) => {
|
||||||
const currentUserRedactionOptions = {
|
const currentUserRedactionOptions = {
|
||||||
enabled: (await instanceSettings.getGeneral()).censorUsernameInLogs,
|
enabled: (await instanceSettings.getGeneral()).censorUsernameInLogs,
|
||||||
@@ -5763,7 +5767,10 @@ export function issueService(db: Db) {
|
|||||||
.set({ updatedAt: now })
|
.set({ updatedAt: now })
|
||||||
.where(eq(issues.id, comment.issueId));
|
.where(eq(issues.id, comment.issueId));
|
||||||
|
|
||||||
return redactIssueComment(comment, currentUserRedactionOptions.enabled);
|
const redacted = redactIssueComment(comment, currentUserRedactionOptions.enabled);
|
||||||
|
await options?.afterTombstone?.(redacted, tx);
|
||||||
|
|
||||||
|
return redacted;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user