2024-11-04 14:58:17 +01:00
|
|
|
import {
|
|
|
|
|
type Application,
|
2025-04-10 19:15:31 +02:00
|
|
|
db,
|
2024-11-04 14:58:17 +01:00
|
|
|
type Emoji,
|
|
|
|
|
type Instance,
|
2025-01-28 19:07:55 +01:00
|
|
|
type Media,
|
2024-11-04 14:58:17 +01:00
|
|
|
type Role,
|
|
|
|
|
type Token,
|
|
|
|
|
type User,
|
|
|
|
|
} from "@versia/kit/db";
|
|
|
|
|
import type { Users } from "@versia/kit/tables";
|
2025-05-04 16:38:37 +02:00
|
|
|
import type { InferSelectModel } from "drizzle-orm";
|
2023-10-23 07:39:42 +02:00
|
|
|
|
2024-11-03 17:45:21 +01:00
|
|
|
export const userRelations = {
|
2024-04-11 13:39:07 +02:00
|
|
|
instance: true,
|
|
|
|
|
emojis: {
|
|
|
|
|
with: {
|
|
|
|
|
emoji: {
|
|
|
|
|
with: {
|
|
|
|
|
instance: true,
|
2025-01-28 17:43:43 +01:00
|
|
|
media: true,
|
2024-04-11 13:39:07 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-01-28 19:07:55 +01:00
|
|
|
avatar: true,
|
|
|
|
|
header: true,
|
2024-06-08 06:57:29 +02:00
|
|
|
roles: {
|
|
|
|
|
with: {
|
|
|
|
|
role: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-11-03 17:45:21 +01:00
|
|
|
} as const;
|
2024-04-11 13:39:07 +02:00
|
|
|
|
|
|
|
|
export interface AuthData {
|
2024-04-25 05:40:27 +02:00
|
|
|
user: User | null;
|
2024-11-03 17:45:21 +01:00
|
|
|
token: Token | null;
|
|
|
|
|
application: Application | null;
|
2024-04-11 13:39:07 +02:00
|
|
|
}
|
2023-10-08 22:20:42 +02:00
|
|
|
|
2024-04-11 13:39:07 +02:00
|
|
|
export const transformOutputToUserWithRelations = (
|
2024-11-04 14:58:17 +01:00
|
|
|
user: Omit<InferSelectModel<typeof Users>, "endpoints"> & {
|
2024-04-11 13:39:07 +02:00
|
|
|
followerCount: unknown;
|
|
|
|
|
followingCount: unknown;
|
|
|
|
|
statusCount: unknown;
|
2025-01-28 19:07:55 +01:00
|
|
|
avatar: typeof Media.$type | null;
|
|
|
|
|
header: typeof Media.$type | null;
|
2024-04-11 13:39:07 +02:00
|
|
|
emojis: {
|
2024-04-14 03:21:38 +02:00
|
|
|
userId: string;
|
|
|
|
|
emojiId: string;
|
2024-11-04 14:58:17 +01:00
|
|
|
emoji?: typeof Emoji.$type;
|
2024-04-11 13:39:07 +02:00
|
|
|
}[];
|
2024-11-04 14:58:17 +01:00
|
|
|
instance: typeof Instance.$type | null;
|
2024-06-08 06:57:29 +02:00
|
|
|
roles: {
|
|
|
|
|
userId: string;
|
|
|
|
|
roleId: string;
|
2024-11-04 14:58:17 +01:00
|
|
|
role?: typeof Role.$type;
|
2024-06-08 06:57:29 +02:00
|
|
|
}[];
|
2024-04-11 13:39:07 +02:00
|
|
|
endpoints: unknown;
|
|
|
|
|
},
|
2024-11-04 14:58:17 +01:00
|
|
|
): typeof User.$type => {
|
2024-04-11 13:39:07 +02:00
|
|
|
return {
|
|
|
|
|
...user,
|
|
|
|
|
followerCount: Number(user.followerCount),
|
|
|
|
|
followingCount: Number(user.followingCount),
|
|
|
|
|
statusCount: Number(user.statusCount),
|
|
|
|
|
endpoints:
|
|
|
|
|
user.endpoints ??
|
|
|
|
|
({} as Partial<{
|
|
|
|
|
dislikes: string;
|
|
|
|
|
featured: string;
|
|
|
|
|
likes: string;
|
|
|
|
|
followers: string;
|
|
|
|
|
following: string;
|
|
|
|
|
inbox: string;
|
|
|
|
|
outbox: string;
|
|
|
|
|
}>),
|
|
|
|
|
emojis: user.emojis.map(
|
|
|
|
|
(emoji) =>
|
|
|
|
|
(emoji as unknown as Record<string, object>)
|
2024-11-04 14:58:17 +01:00
|
|
|
.emoji as typeof Emoji.$type,
|
2024-04-11 13:39:07 +02:00
|
|
|
),
|
2024-06-08 06:57:29 +02:00
|
|
|
roles: user.roles
|
|
|
|
|
.map((role) => role.role)
|
2024-11-04 14:58:17 +01:00
|
|
|
.filter(Boolean) as (typeof Role.$type)[],
|
2024-04-11 13:39:07 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const findManyUsers = async (
|
2024-04-17 08:36:01 +02:00
|
|
|
query: Parameters<typeof db.query.Users.findMany>[0],
|
2024-11-04 14:58:17 +01:00
|
|
|
): Promise<(typeof User.$type)[]> => {
|
2024-04-17 08:36:01 +02:00
|
|
|
const output = await db.query.Users.findMany({
|
2024-04-11 13:39:07 +02:00
|
|
|
...query,
|
|
|
|
|
with: {
|
|
|
|
|
...userRelations,
|
|
|
|
|
...query?.with,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return output.map((user) => transformOutputToUserWithRelations(user));
|
|
|
|
|
};
|