mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 16:38:19 +01:00
Some checks failed
CodeQL Scan / Analyze (javascript-typescript) (push) Failing after 0s
Build Docker Images / lint (push) Failing after 6s
Build Docker Images / check (push) Failing after 6s
Build Docker Images / tests (push) Failing after 6s
Deploy Docs to GitHub Pages / build (push) Failing after 1s
Build Docker Images / build (server, Dockerfile, ${{ github.repository_owner }}/server) (push) Has been skipped
Build Docker Images / build (worker, Worker.Dockerfile, ${{ github.repository_owner }}/worker) (push) Has been skipped
Deploy Docs to GitHub Pages / Deploy (push) Has been skipped
Mirror to Codeberg / Mirror (push) Failing after 1s
Nix Build / check (push) Failing after 1s
197 lines
5.3 KiB
TypeScript
197 lines
5.3 KiB
TypeScript
import type { Notification as NotificationSchema } from "@versia/client/schemas";
|
|
import { db, Note, User } from "@versia/kit/db";
|
|
import { Notifications } from "@versia/kit/tables";
|
|
import {
|
|
desc,
|
|
eq,
|
|
type InferInsertModel,
|
|
type InferSelectModel,
|
|
inArray,
|
|
type SQL,
|
|
} from "drizzle-orm";
|
|
import type { z } from "zod";
|
|
import {
|
|
transformOutputToUserWithRelations,
|
|
userRelations,
|
|
} from "../functions/user.ts";
|
|
import { BaseInterface } from "./base.ts";
|
|
|
|
export type NotificationType = InferSelectModel<typeof Notifications> & {
|
|
status: typeof Note.$type | null;
|
|
account: typeof User.$type;
|
|
};
|
|
|
|
export class Notification extends BaseInterface<
|
|
typeof Notifications,
|
|
NotificationType
|
|
> {
|
|
public async reload(): Promise<void> {
|
|
const reloaded = await Notification.fromId(this.data.id);
|
|
|
|
if (!reloaded) {
|
|
throw new Error("Failed to reload notification");
|
|
}
|
|
|
|
this.data = reloaded.data;
|
|
}
|
|
|
|
public static async fromId(
|
|
id: string | null,
|
|
userId?: string,
|
|
): Promise<Notification | null> {
|
|
if (!id) {
|
|
return null;
|
|
}
|
|
|
|
return await Notification.fromSql(
|
|
eq(Notifications.id, id),
|
|
undefined,
|
|
userId,
|
|
);
|
|
}
|
|
|
|
public static async fromIds(
|
|
ids: string[],
|
|
userId?: string,
|
|
): Promise<Notification[]> {
|
|
return await Notification.manyFromSql(
|
|
inArray(Notifications.id, ids),
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
userId,
|
|
);
|
|
}
|
|
|
|
public static async fromSql(
|
|
sql: SQL<unknown> | undefined,
|
|
orderBy: SQL<unknown> | undefined = desc(Notifications.id),
|
|
userId?: string,
|
|
): Promise<Notification | null> {
|
|
const found = await db.query.Notifications.findFirst({
|
|
where: sql,
|
|
orderBy,
|
|
with: {
|
|
account: {
|
|
with: {
|
|
...userRelations,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!found) {
|
|
return null;
|
|
}
|
|
return new Notification({
|
|
...found,
|
|
account: transformOutputToUserWithRelations(found.account),
|
|
status: (await Note.fromId(found.noteId, userId))?.data ?? null,
|
|
});
|
|
}
|
|
|
|
public static async manyFromSql(
|
|
sql: SQL<unknown> | undefined,
|
|
orderBy: SQL<unknown> | undefined = desc(Notifications.id),
|
|
limit?: number,
|
|
offset?: number,
|
|
extra?: Parameters<typeof db.query.Notifications.findMany>[0],
|
|
userId?: string,
|
|
): Promise<Notification[]> {
|
|
const found = await db.query.Notifications.findMany({
|
|
where: sql,
|
|
orderBy,
|
|
limit,
|
|
offset,
|
|
with: {
|
|
...extra?.with,
|
|
account: {
|
|
with: {
|
|
...userRelations,
|
|
},
|
|
},
|
|
},
|
|
extras: extra?.extras,
|
|
});
|
|
|
|
return (
|
|
await Promise.all(
|
|
found.map(async (notif) => ({
|
|
...notif,
|
|
account: transformOutputToUserWithRelations(notif.account),
|
|
status:
|
|
(await Note.fromId(notif.noteId, userId))?.data ?? null,
|
|
})),
|
|
)
|
|
).map((s) => new Notification(s));
|
|
}
|
|
|
|
public async update(
|
|
newAttachment: Partial<NotificationType>,
|
|
): Promise<NotificationType> {
|
|
await db
|
|
.update(Notifications)
|
|
.set(newAttachment)
|
|
.where(eq(Notifications.id, this.id));
|
|
|
|
const updated = await Notification.fromId(this.data.id);
|
|
|
|
if (!updated) {
|
|
throw new Error("Failed to update notification");
|
|
}
|
|
|
|
this.data = updated.data;
|
|
return updated.data;
|
|
}
|
|
|
|
public save(): Promise<NotificationType> {
|
|
return this.update(this.data);
|
|
}
|
|
|
|
public async delete(ids?: string[]): Promise<void> {
|
|
if (Array.isArray(ids)) {
|
|
await db
|
|
.delete(Notifications)
|
|
.where(inArray(Notifications.id, ids));
|
|
} else {
|
|
await db.delete(Notifications).where(eq(Notifications.id, this.id));
|
|
}
|
|
}
|
|
|
|
public static async insert(
|
|
data: InferInsertModel<typeof Notifications>,
|
|
): Promise<Notification> {
|
|
const inserted = (
|
|
await db.insert(Notifications).values(data).returning()
|
|
)[0];
|
|
|
|
const notification = await Notification.fromId(inserted.id);
|
|
|
|
if (!notification) {
|
|
throw new Error("Failed to insert notification");
|
|
}
|
|
|
|
return notification;
|
|
}
|
|
|
|
public get id(): string {
|
|
return this.data.id;
|
|
}
|
|
|
|
public async toApi(): Promise<z.infer<typeof NotificationSchema>> {
|
|
const account = new User(this.data.account);
|
|
|
|
return {
|
|
account: account.toApi(),
|
|
created_at: new Date(this.data.createdAt).toISOString(),
|
|
id: this.data.id,
|
|
type: this.data.type,
|
|
status: this.data.status
|
|
? await new Note(this.data.status).toApi(account)
|
|
: undefined,
|
|
group_key: `ungrouped-${this.data.id}`,
|
|
};
|
|
}
|
|
}
|