Files
outline/server/models/Comment.ts
2024-03-26 06:09:41 -07:00

85 lines
1.9 KiB
TypeScript

import { Node } from "prosemirror-model";
import { InferAttributes, InferCreationAttributes } from "sequelize";
import {
DataType,
BelongsTo,
ForeignKey,
Column,
Table,
Length,
DefaultScope,
} from "sequelize-typescript";
import type { ProsemirrorData } from "@shared/types";
import ProsemirrorHelper from "@shared/utils/ProsemirrorHelper";
import { CommentValidation } from "@shared/validations";
import { schema } from "@server/editor";
import Document from "./Document";
import User from "./User";
import ParanoidModel from "./base/ParanoidModel";
import Fix from "./decorators/Fix";
import TextLength from "./validators/TextLength";
@DefaultScope(() => ({
include: [
{
model: User,
as: "createdBy",
paranoid: false,
},
],
}))
@Table({ tableName: "comments", modelName: "comment" })
@Fix
class Comment extends ParanoidModel<
InferAttributes<Comment>,
Partial<InferCreationAttributes<Comment>>
> {
@TextLength({
max: CommentValidation.maxLength,
msg: `Comment must be less than ${CommentValidation.maxLength} characters`,
})
@Length({
max: CommentValidation.maxLength * 10,
msg: `Comment data is too large`,
})
@Column(DataType.JSONB)
data: ProsemirrorData;
// associations
@BelongsTo(() => User, "createdById")
createdBy: User;
@ForeignKey(() => User)
@Column(DataType.UUID)
createdById: string;
@BelongsTo(() => User, "resolvedById")
resolvedBy: User;
@ForeignKey(() => User)
@Column(DataType.UUID)
resolvedById: string;
@BelongsTo(() => Document, "documentId")
document: Document;
@ForeignKey(() => Document)
@Column(DataType.UUID)
documentId: string;
@BelongsTo(() => Comment, "parentCommentId")
parentComment: Comment;
@ForeignKey(() => Comment)
@Column(DataType.UUID)
parentCommentId: string;
public toPlainText() {
const node = Node.fromJSON(schema, this.data);
return ProsemirrorHelper.toPlainText(node, schema);
}
}
export default Comment;