Files
Tom Moor ac48c91a7e feat: Accept text param in comments.update (#13854)
* feat: Accept text param in comments.update

* fix: Authorize before processing text in comments.update
2026-09-22 22:39:16 -04:00

152 lines
4.7 KiB
TypeScript

import { isEmpty } from "es-toolkit/compat";
import { z } from "zod";
import { CommentStatusFilter } from "@shared/types";
import { commentSchema } from "@server/editor";
import { BaseSchema, ProsemirrorSchema } from "@server/routes/api/schema";
import { zodEmojiType } from "@server/utils/zod";
const BaseIdSchema = z.object({
/** Comment Id */
id: z.uuid(),
});
const CommentsSortParamsSchema = z.object({
/** Specifies the attributes by which comments will be sorted in the list */
sort: z
.string()
.refine((val) => ["createdAt", "updatedAt"].includes(val))
.prefault("createdAt"),
/** Specifies the sort order with respect to sort field */
direction: z
.string()
.optional()
.transform((val) => (val !== "ASC" ? "DESC" : val)),
});
export const CommentsCreateSchema = BaseSchema.extend({
body: z
.object({
/** Allow creation with a specific ID */
id: z.uuid().optional(),
/** Create comment for this document */
documentId: z.uuid(),
/** Create comment under this parent */
parentCommentId: z.uuid().optional(),
/** Create comment with this data */
data: ProsemirrorSchema({ schema: commentSchema }).optional(),
/** Create comment with this text */
text: z.string().optional(),
/**
* Plain text substring to anchor the comment to as an inline comment.
* The first occurrence in the document's plain text is used.
*/
anchorText: z.string().optional(),
/**
* Plain text immediately preceding `anchorText` in the document, used
* to select a specific occurrence when `anchorText` is ambiguous.
*/
anchorPrefix: z.string().optional(),
/**
* Plain text immediately following `anchorText` in the document, used
* to select a specific occurrence when `anchorText` is ambiguous.
*/
anchorSuffix: z.string().optional(),
/**
* Hash identifying a node in the document to anchor the comment to,
* e.g. an image, as computed by `ProsemirrorHelper.getNodeHash`. The
* first matching node in document order is used.
*/
anchorNodeId: z.string().optional(),
})
.refine((obj) => !(isEmpty(obj.data) && isEmpty(obj.text)), {
error: "One of data or text is required",
})
.refine(
(obj) => !((obj.anchorPrefix || obj.anchorSuffix) && !obj.anchorText),
{
error: "anchorPrefix and anchorSuffix require anchorText",
}
)
.refine((obj) => !(obj.anchorText && obj.anchorNodeId), {
error: "anchorText and anchorNodeId are mutually exclusive",
}),
});
export type CommentsCreateReq = z.infer<typeof CommentsCreateSchema>;
export const CommentsUpdateSchema = BaseSchema.extend({
body: BaseIdSchema.extend({
/** Update comment with this data */
data: ProsemirrorSchema({ schema: commentSchema }).optional(),
/** Update comment with this text */
text: z.string().optional(),
}).refine((obj) => !(isEmpty(obj.data) && isEmpty(obj.text)), {
error: "One of data or text is required",
}),
});
export type CommentsUpdateReq = z.infer<typeof CommentsUpdateSchema>;
export const CommentsDeleteSchema = BaseSchema.extend({
body: BaseIdSchema,
});
export type CommentsDeleteReq = z.infer<typeof CommentsDeleteSchema>;
export const CommentsListSchema = BaseSchema.extend({
body: CommentsSortParamsSchema.extend({
/** Id of a document to list comments for */
documentId: z.string().optional(),
/** Id of a collection to list comments for */
collectionId: z.string().optional(),
/** Id of a parent comment to list comments for */
parentCommentId: z.uuid().optional(),
/** Comment statuses to include in results */
statusFilter: z.enum(CommentStatusFilter).array().optional(),
/** Whether to include anchor text, if it exists */
includeAnchorText: z.boolean().optional(),
}),
});
export type CommentsListReq = z.infer<typeof CommentsListSchema>;
export const CommentsInfoSchema = z.object({
body: BaseIdSchema.extend({
/** Whether to include anchor text, if it exists */
includeAnchorText: z.boolean().optional(),
}),
});
export type CommentsInfoReq = z.infer<typeof CommentsInfoSchema>;
export const CommentsResolveSchema = z.object({
body: BaseIdSchema,
});
export type CommentsResolveReq = z.infer<typeof CommentsResolveSchema>;
export const CommentsUnresolveSchema = z.object({
body: BaseIdSchema,
});
export type CommentsUnresolveReq = z.infer<typeof CommentsUnresolveSchema>;
export const CommentsReactionSchema = z.object({
body: BaseIdSchema.extend({
/** Emoji that's added to (or) removed from a comment as a reaction. */
emoji: zodEmojiType(),
}),
});
export type CommentsReactionReq = z.infer<typeof CommentsReactionSchema>;