2024-04-13 14:20:12 +02:00
|
|
|
import type { InferSelectModel } from "drizzle-orm";
|
|
|
|
|
import { db } from "~drizzle/db";
|
|
|
|
|
import type { notification } from "~drizzle/schema";
|
2024-04-14 12:53:21 +02:00
|
|
|
import type { Notification as APINotification } from "~types/mastodon/notification";
|
2024-04-11 14:12:16 +02:00
|
|
|
import {
|
|
|
|
|
type StatusWithRelations,
|
|
|
|
|
findFirstStatuses,
|
2024-04-13 14:20:12 +02:00
|
|
|
statusToAPI,
|
2024-04-11 14:12:16 +02:00
|
|
|
} from "./Status";
|
|
|
|
|
import {
|
|
|
|
|
type UserWithRelations,
|
|
|
|
|
transformOutputToUserWithRelations,
|
2024-04-13 14:20:12 +02:00
|
|
|
userExtrasTemplate,
|
|
|
|
|
userRelations,
|
|
|
|
|
userToAPI,
|
2024-04-11 14:12:16 +02:00
|
|
|
} from "./User";
|
2024-04-11 13:39:07 +02:00
|
|
|
|
|
|
|
|
export type Notification = InferSelectModel<typeof notification>;
|
2023-11-23 05:10:37 +01:00
|
|
|
|
|
|
|
|
export type NotificationWithRelations = Notification & {
|
2024-04-07 07:30:49 +02:00
|
|
|
status: StatusWithRelations | null;
|
|
|
|
|
account: UserWithRelations;
|
2023-11-23 05:10:37 +01:00
|
|
|
};
|
|
|
|
|
|
2024-04-11 14:12:16 +02:00
|
|
|
export const findManyNotifications = async (
|
|
|
|
|
query: Parameters<typeof db.query.notification.findMany>[0],
|
|
|
|
|
): Promise<NotificationWithRelations[]> => {
|
|
|
|
|
const output = await db.query.notification.findMany({
|
|
|
|
|
...query,
|
|
|
|
|
with: {
|
|
|
|
|
...query?.with,
|
|
|
|
|
account: {
|
|
|
|
|
with: {
|
|
|
|
|
...userRelations,
|
|
|
|
|
},
|
|
|
|
|
extras: userExtrasTemplate("notification_account"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
extras: {
|
|
|
|
|
...query?.extras,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return await Promise.all(
|
|
|
|
|
output.map(async (notif) => ({
|
|
|
|
|
...notif,
|
|
|
|
|
account: transformOutputToUserWithRelations(notif.account),
|
|
|
|
|
status: notif.statusId
|
|
|
|
|
? await findFirstStatuses({
|
|
|
|
|
where: (status, { eq }) =>
|
|
|
|
|
eq(status.id, notif.statusId ?? ""),
|
|
|
|
|
})
|
|
|
|
|
: null,
|
|
|
|
|
})),
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-23 05:10:37 +01:00
|
|
|
export const notificationToAPI = async (
|
2024-04-07 07:30:49 +02:00
|
|
|
notification: NotificationWithRelations,
|
2023-11-23 05:10:37 +01:00
|
|
|
): Promise<APINotification> => {
|
2024-04-07 07:30:49 +02:00
|
|
|
return {
|
|
|
|
|
account: userToAPI(notification.account),
|
|
|
|
|
created_at: new Date(notification.createdAt).toISOString(),
|
|
|
|
|
id: notification.id,
|
|
|
|
|
type: notification.type,
|
|
|
|
|
status: notification.status
|
|
|
|
|
? await statusToAPI(notification.status, notification.account)
|
|
|
|
|
: undefined,
|
|
|
|
|
};
|
2023-11-23 05:10:37 +01:00
|
|
|
};
|