Notifications refactor (#5151

* Ongoing

* refactor

* test

* Add cleanup task

* refactor
This commit is contained in:
Tom Moor
2023-04-08 09:22:49 -04:00
committed by GitHub
parent c97110e72b
commit 9c9ceef8ee
28 changed files with 1122 additions and 901 deletions

View File

@@ -0,0 +1,44 @@
import { NotificationEventType } from "@shared/types";
import { Collection, Notification } from "@server/models";
import NotificationHelper from "@server/models/helpers/NotificationHelper";
import { CollectionEvent } from "@server/types";
import BaseTask, { TaskPriority } from "./BaseTask";
export default class CollectionCreatedNotificationsTask extends BaseTask<
CollectionEvent
> {
public async perform(event: CollectionEvent) {
const collection = await Collection.findByPk(event.collectionId);
// We only send notifications for collections visible to the entire team
if (!collection || !collection.permission) {
return;
}
const recipients = await NotificationHelper.getCollectionNotificationRecipients(
collection,
NotificationEventType.CreateCollection
);
for (const recipient of recipients) {
// Suppress notifications for suspended users
if (recipient.isSuspended || !recipient.email) {
continue;
}
await Notification.create({
event: NotificationEventType.CreateCollection,
userId: recipient.id,
collectionId: collection.id,
actorId: collection.createdById,
teamId: collection.teamId,
});
}
}
public get options() {
return {
priority: TaskPriority.Background,
};
}
}