diff --git a/server/queues/tasks/CommentCreatedNotificationsTask.ts b/server/queues/tasks/CommentCreatedNotificationsTask.ts index 50f92429b1..2bed7340e8 100644 --- a/server/queues/tasks/CommentCreatedNotificationsTask.ts +++ b/server/queues/tasks/CommentCreatedNotificationsTask.ts @@ -5,6 +5,7 @@ import NotificationHelper from "@server/models/helpers/NotificationHelper"; import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper"; import { sequelize } from "@server/storage/database"; import { CommentEvent } from "@server/types"; +import { canUserAccessDocument } from "@server/utils/policies"; import BaseTask, { TaskPriority } from "./BaseTask"; export default class CommentCreatedNotificationsTask extends BaseTask { @@ -52,7 +53,8 @@ export default class CommentCreatedNotificationsTask extends BaseTask { @@ -41,7 +42,8 @@ export default class CommentUpdatedNotificationsTask extends BaseTask { @@ -33,7 +34,8 @@ export default class DocumentPublishedNotificationsTask extends BaseTask { @@ -54,7 +54,7 @@ export default class RevisionCreatedNotificationsTask extends BaseTask { - try { - const document = await Document.findByPk(model.id, { - userId: user.id, - }); - authorize(user, "read", document); - return true; - } catch (err) { - return false; - } - }; - public get options() { return { priority: TaskPriority.Background, diff --git a/server/utils/policies.ts b/server/utils/policies.ts new file mode 100644 index 0000000000..3058b46934 --- /dev/null +++ b/server/utils/policies.ts @@ -0,0 +1,21 @@ +import { Document, User } from "@server/models"; +import { authorize } from "@server/policies"; + +/** + * Check if the given user can access a document + * + * @param user - The user to check + * @param documentId - The document to check + * @returns Boolean whether the user can access the document + */ +export const canUserAccessDocument = async (user: User, documentId: string) => { + try { + const document = await Document.findByPk(documentId, { + userId: user.id, + }); + authorize(user, "read", document); + return true; + } catch (err) { + return false; + } +};