feat(client-portal): let a reviewer edit and delete their own notes
Notes left through a review link were write-once. A client who mistyped a timecode or changed their mind had no way to correct it and had to add a second note contradicting the first, which the studio then had to reconcile by hand. PATCH and DELETE on comment/[commentId] cover both. Only the reviewer's own notes are touchable: the studio's comments and those of every other review session are read-only through a link, whatever the holder sends. Ownership is re-checked server-side on every write, so the canEdit flag now returned by the versions route only decides whether to offer the controls and grants nothing. Refusals are 404 rather than 403 throughout, since a link holder has no business learning which comment ids exist outside their own notes. Only the text is editable. The frame a note is pinned to is what the studio works from, so moving it would silently repoint feedback someone may already have actioned; a note about a different frame is a new note. Deleting a drawing note also removes its strokes, mirroring deleteComment in actions/comments.ts, and the page reloads the player afterwards so the frame does not keep showing marks that no longer exist. Delete goes through a confirmation that shows the note and says plainly that it disappears for the studio too. Worth knowing before this reaches a show: everyone holding the same review link is the same author. A ReviewSession with no email resolves to a token-derived guest user, so one link sent to three people gives all three one identity and any of them can now edit or delete the others' notes. That model is not new, which is why their comments already showed as one name, but edit and delete is the first feature where it grants real power rather than muddling attribution. Per-reviewer links with email set are the fix if that is not wanted, and that is a change to how links are issued rather than to this code. The scope is documented on reviewClientEmail so the next reader meets it before it surprises them. getOrCreateClientUser was copy-pasted into the comment and approve routes with the fallback-email expression spelled out in three places. It is one set of helpers in review-auth.ts now, including findReviewUser, a read-only variant so that merely opening a review link does not conjure a User row just to answer "is this theirs?", and ownsComment, the single rule shared by the route that offers the controls and the route that enforces them. Nine tests cover that rule directly: internal users' notes stay read-only, a session that has never posted owns nothing, and an authorless comment — one whose User was deleted, since Comment.authorId is SetNull — cannot be claimed by anyone. The routes themselves are not covered, so exercise edit and delete against a real review link before handing one to a client. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { db } from "@/lib/db";
|
||||
import { validateReviewToken, findReviewUser, ownsComment } from "@/lib/review-auth";
|
||||
|
||||
/**
|
||||
* Edit and delete for notes left through a client review link.
|
||||
*
|
||||
* A reviewer may only touch their own notes: the studio's comments and those of
|
||||
* any other review session are read-only here, whatever the link holder sends.
|
||||
* Ownership is the review session's guest User (lib/review-auth.ts) — the link
|
||||
* is the credential, so everyone holding it shares that identity.
|
||||
*/
|
||||
|
||||
/** Matches the internal addComment/editComment limits (actions/comments.ts). */
|
||||
const MAX_LENGTH = 5000;
|
||||
|
||||
/** The annotation companion comments written by the drawing tools. */
|
||||
const ANNOTATION_PREFIX = "✏️ Annotation at frame";
|
||||
|
||||
type Ctx = { params: Promise<{ token: string; commentId: string }> };
|
||||
|
||||
/**
|
||||
* Resolves the request to a comment the caller is allowed to change, or the
|
||||
* response explaining why not.
|
||||
*
|
||||
* "Not yours" is deliberately answered with 404 rather than 403: a link holder
|
||||
* has no business learning which comment ids exist outside their own notes.
|
||||
*/
|
||||
async function authorizeCommentEdit(req: NextRequest, ctx: Ctx) {
|
||||
const { token, commentId } = await ctx.params;
|
||||
|
||||
const result = await validateReviewToken(token, req);
|
||||
if (result.type === "requiresPassword") {
|
||||
return { error: NextResponse.json({ requiresPassword: true }, { status: 401 }) };
|
||||
}
|
||||
if (result.type === "invalid") {
|
||||
return {
|
||||
error: NextResponse.json({ error: "Invalid or expired review link" }, { status: 403 }),
|
||||
};
|
||||
}
|
||||
const session = result.session;
|
||||
|
||||
const notFound = NextResponse.json({ error: "Comment not found" }, { status: 404 });
|
||||
|
||||
const comment = await db.comment.findUnique({
|
||||
where: { id: commentId },
|
||||
select: {
|
||||
id: true,
|
||||
authorId: true,
|
||||
versionId: true,
|
||||
frameNumber: true,
|
||||
text: true,
|
||||
version: {
|
||||
select: {
|
||||
shot: { select: { projectId: true } },
|
||||
task: { select: { projectId: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!comment) return { error: notFound };
|
||||
|
||||
// Same project scoping the rest of the portal applies.
|
||||
const projectId = comment.version.shot?.projectId ?? comment.version.task?.projectId;
|
||||
if (projectId !== session.projectId) return { error: notFound };
|
||||
|
||||
const user = await findReviewUser(session, token);
|
||||
if (!ownsComment(comment, user?.id)) return { error: notFound };
|
||||
|
||||
return { comment };
|
||||
}
|
||||
|
||||
/** PATCH — change the text of one's own note. */
|
||||
export async function PATCH(req: NextRequest, ctx: Ctx) {
|
||||
const authorized = await authorizeCommentEdit(req, ctx);
|
||||
if (authorized.error) return authorized.error;
|
||||
const { comment } = authorized;
|
||||
|
||||
const body = await req.json().catch(() => null);
|
||||
const text = typeof body?.text === "string" ? body.text.trim() : "";
|
||||
if (!text) {
|
||||
return NextResponse.json({ error: "Note text is required" }, { status: 400 });
|
||||
}
|
||||
if (text.length > MAX_LENGTH) {
|
||||
return NextResponse.json(
|
||||
{ error: `Note must be ${MAX_LENGTH} characters or fewer` },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Only the text is editable. The frame a note is pinned to is what the studio
|
||||
// works from, so moving it would silently repoint feedback someone may have
|
||||
// already actioned; a note about a different frame is a new note.
|
||||
const updated = await db.comment.update({
|
||||
where: { id: comment.id },
|
||||
data: { text },
|
||||
include: {
|
||||
author: { select: { id: true, name: true, image: true, email: true } },
|
||||
replies: {
|
||||
orderBy: { createdAt: "asc" },
|
||||
include: { author: { select: { id: true, name: true, image: true, email: true } } },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ comment: { ...updated, canEdit: true } });
|
||||
}
|
||||
|
||||
/** DELETE — remove one's own note. */
|
||||
export async function DELETE(req: NextRequest, ctx: Ctx) {
|
||||
const authorized = await authorizeCommentEdit(req, ctx);
|
||||
if (authorized.error) return authorized.error;
|
||||
const { comment } = authorized;
|
||||
|
||||
await db.comment.delete({ where: { id: comment.id } });
|
||||
|
||||
// A drawing leaves a companion comment; deleting the note without the strokes
|
||||
// would leave marks on the frame that nothing explains. Mirrors deleteComment
|
||||
// in actions/comments.ts.
|
||||
if (comment.text.startsWith(ANNOTATION_PREFIX)) {
|
||||
await db.annotation.deleteMany({
|
||||
where: { versionId: comment.versionId, frameNumber: comment.frameNumber },
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
}
|
||||
Reference in New Issue
Block a user