Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Tom Moor <tom.moor@gmail.com>
56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
import { NotificationEventType } from "@shared/types";
|
|
import { Comment, Document, Notification, User } from "@server/models";
|
|
import ProsemirrorHelper from "@server/models/helpers/ProsemirrorHelper";
|
|
import { CommentEvent, CommentUpdateEvent } from "@server/types";
|
|
import BaseTask, { TaskPriority } from "./BaseTask";
|
|
|
|
export default class CommentUpdatedNotificationsTask extends BaseTask<CommentEvent> {
|
|
public async perform(event: CommentUpdateEvent) {
|
|
const [document, comment] = await Promise.all([
|
|
Document.scope("withCollection").findOne({
|
|
where: {
|
|
id: event.documentId,
|
|
},
|
|
}),
|
|
Comment.findByPk(event.modelId),
|
|
]);
|
|
if (!document || !comment) {
|
|
return;
|
|
}
|
|
|
|
const mentions = ProsemirrorHelper.parseMentions(
|
|
ProsemirrorHelper.toProsemirror(comment.data)
|
|
).filter((mention) => event.data.newMentionIds.includes(mention.id));
|
|
if (mentions.length === 0) {
|
|
return;
|
|
}
|
|
|
|
for (const mention of mentions) {
|
|
const recipient = await User.findByPk(mention.modelId);
|
|
|
|
if (
|
|
recipient &&
|
|
recipient.id !== mention.actorId &&
|
|
recipient.subscribedToEventType(
|
|
NotificationEventType.MentionedInComment
|
|
)
|
|
) {
|
|
await Notification.create({
|
|
event: NotificationEventType.MentionedInComment,
|
|
userId: recipient.id,
|
|
actorId: mention.actorId,
|
|
teamId: document.teamId,
|
|
documentId: document.id,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
public get options() {
|
|
return {
|
|
attempts: 1,
|
|
priority: TaskPriority.Background,
|
|
};
|
|
}
|
|
}
|