Fix not working routes

This commit is contained in:
Jesse Wierzbinski 2024-04-11 02:12:16 -10:00
parent f7abe06a60
commit df939a6a7a
No known key found for this signature in database
8 changed files with 146 additions and 69 deletions

View file

@ -1,8 +1,19 @@
import type { APINotification } from "~types/entities/notification";
import { type StatusWithRelations, statusToAPI } from "./Status";
import { type UserWithRelations, userToAPI } from "./User";
import {
type StatusWithRelations,
statusToAPI,
findFirstStatuses,
} from "./Status";
import {
type UserWithRelations,
userToAPI,
userRelations,
userExtrasTemplate,
transformOutputToUserWithRelations,
} from "./User";
import type { InferSelectModel } from "drizzle-orm";
import type { notification } from "~drizzle/schema";
import { db } from "~drizzle/db";
export type Notification = InferSelectModel<typeof notification>;
@ -11,6 +22,39 @@ export type NotificationWithRelations = Notification & {
account: UserWithRelations;
};
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,
})),
);
};
export const notificationToAPI = async (
notification: NotificationWithRelations,
): Promise<APINotification> => {

View file

@ -100,6 +100,21 @@ export const statusExtras = {
),
};
export const statusExtrasTemplate = (name: string) => ({
// @ts-ignore
reblogCount: sql([
`(SELECT COUNT(*) FROM "Status" "status" WHERE "status"."reblogId" = ${name}.id)`,
]).as("reblog_count"),
// @ts-ignore
likeCount: sql([
`(SELECT COUNT(*) FROM "Like" "like" WHERE "like"."likedId" = ${name}.id)`,
]).as("like_count"),
// @ts-ignore
replyCount: sql([
`(SELECT COUNT(*) FROM "Status" "status" WHERE "status"."inReplyToPostId" = ${name}.id)`,
]).as("reply_count"),
});
/**
* Returns whether this status is viewable by a user.
* @param user The user to check.
@ -138,6 +153,15 @@ export const findManyStatuses = async (
where: (attachment, { eq }) =>
eq(attachment.statusId, sql`"status"."id"`),
},
emojis: {
with: {
emoji: {
with: {
instance: true,
},
},
},
},
author: {
with: {
...userRelations,