Files
Tom Moor b6231f36c9 perf: Reduce query cost and reparsing in comments API (#13887)
* perf: Reduce query cost and reparsing in comments API

* perf: Join documents without default associations when listing comments

* refactor: Move comment listing into Comment.findAllForUser
2026-09-25 20:08:59 -04:00

40 lines
1.1 KiB
TypeScript

import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper";
import type { Comment } from "@server/models";
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
import presentUser from "./user";
type Options = {
/** Whether to include anchor text, if it exists */
includeAnchorText?: boolean;
};
export default function present(
comment: Comment,
{ includeAnchorText }: Options = {}
) {
let anchorText: string | undefined;
if (includeAnchorText && comment.document) {
anchorText = ProsemirrorHelper.getAnchorTextForComment(
DocumentHelper.getCommentMarks(comment.document),
comment.id
);
}
return {
id: comment.id,
data: comment.data,
documentId: comment.documentId,
parentCommentId: comment.parentCommentId,
createdBy: presentUser(comment.createdBy),
createdById: comment.createdById,
resolvedAt: comment.resolvedAt,
resolvedBy: comment.resolvedBy ? presentUser(comment.resolvedBy) : null,
resolvedById: comment.resolvedById,
createdAt: comment.createdAt,
updatedAt: comment.updatedAt,
reactions: comment.reactions ?? [],
anchorText,
};
}