mirror of
https://github.com/Drop-OSS/drop.git
synced 2026-07-21 07:22:54 +10:00
feat: separate library and metadata pages, notification acls
This commit is contained in:
@@ -8,25 +8,32 @@ Design goals:
|
||||
|
||||
import type { Notification } from "~/prisma/client";
|
||||
import prisma from "../db/database";
|
||||
import type { GlobalACL } from "../acls";
|
||||
|
||||
export type NotificationCreateArgs = Pick<
|
||||
Notification,
|
||||
"title" | "description" | "actions" | "nonce"
|
||||
>;
|
||||
> & { acls: Array<GlobalACL> };
|
||||
|
||||
class NotificationSystem {
|
||||
// userId to acl to listenerId
|
||||
private listeners = new Map<
|
||||
string,
|
||||
Map<string, (notification: Notification) => void>
|
||||
Map<
|
||||
string,
|
||||
{ callback: (notification: Notification) => void; acls: GlobalACL[] }
|
||||
>
|
||||
>();
|
||||
|
||||
listen(
|
||||
userId: string,
|
||||
acls: Array<GlobalACL>,
|
||||
id: string,
|
||||
callback: (notification: Notification) => void,
|
||||
) {
|
||||
this.listeners.set(userId, new Map());
|
||||
this.listeners.get(userId)?.set(id, callback);
|
||||
if (!this.listeners.has(userId)) this.listeners.set(userId, new Map());
|
||||
// eslint-disable-next-line @typescript-eslint/no-extra-non-null-assertion
|
||||
this.listeners.get(userId)!!.set(id, { callback, acls });
|
||||
|
||||
this.catchupListener(userId, id);
|
||||
}
|
||||
@@ -36,23 +43,23 @@ class NotificationSystem {
|
||||
}
|
||||
|
||||
private async catchupListener(userId: string, id: string) {
|
||||
const callback = this.listeners.get(userId)?.get(id);
|
||||
if (!callback)
|
||||
const listener = this.listeners.get(userId)?.get(id);
|
||||
if (!listener)
|
||||
throw new Error("Failed to catch-up listener: callback does not exist");
|
||||
const notifications = await prisma.notification.findMany({
|
||||
where: { userId: userId },
|
||||
where: { userId: userId, acls: { hasSome: listener.acls } },
|
||||
orderBy: {
|
||||
created: "asc", // Oldest first, because they arrive in reverse order
|
||||
},
|
||||
});
|
||||
for (const notification of notifications) {
|
||||
await callback(notification);
|
||||
await listener.callback(notification);
|
||||
}
|
||||
}
|
||||
|
||||
private async pushNotification(userId: string, notification: Notification) {
|
||||
for (const listener of this.listeners.get(userId) ?? []) {
|
||||
await listener[1](notification);
|
||||
await listener[1].callback(notification);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +97,7 @@ class NotificationSystem {
|
||||
}
|
||||
|
||||
async systemPush(notificationCreateArgs: NotificationCreateArgs) {
|
||||
return await this.push("system", notificationCreateArgs);
|
||||
return await this.pushAll(notificationCreateArgs);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user