mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
Fix not working routes
This commit is contained in:
parent
f7abe06a60
commit
df939a6a7a
8 changed files with 146 additions and 69 deletions
|
|
@ -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> => {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue