Notifications refactor (#5151
* Ongoing * refactor * test * Add cleanup task * refactor
This commit is contained in:
57
server/queues/tasks/CommentUpdatedNotificationsTask.ts
Normal file
57
server/queues/tasks/CommentUpdatedNotificationsTask.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user