mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28: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
102 lines
2.5 KiB
TypeScript
102 lines
2.5 KiB
TypeScript
import {
|
|
type Application,
|
|
db,
|
|
type Emoji,
|
|
type Instance,
|
|
type Media,
|
|
type Role,
|
|
type Token,
|
|
type User,
|
|
} from "@versia/kit/db";
|
|
import type { Users } from "@versia/kit/tables";
|
|
import type { InferSelectModel } from "drizzle-orm";
|
|
|
|
export const userRelations = {
|
|
instance: true,
|
|
emojis: {
|
|
with: {
|
|
emoji: {
|
|
with: {
|
|
instance: true,
|
|
media: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
avatar: true,
|
|
header: true,
|
|
roles: {
|
|
with: {
|
|
role: true,
|
|
},
|
|
},
|
|
} as const;
|
|
|
|
export interface AuthData {
|
|
user: User | null;
|
|
token: Token | null;
|
|
application: Application | null;
|
|
}
|
|
|
|
export const transformOutputToUserWithRelations = (
|
|
user: Omit<InferSelectModel<typeof Users>, "endpoints"> & {
|
|
followerCount: unknown;
|
|
followingCount: unknown;
|
|
statusCount: unknown;
|
|
avatar: typeof Media.$type | null;
|
|
header: typeof Media.$type | null;
|
|
emojis: {
|
|
userId: string;
|
|
emojiId: string;
|
|
emoji?: typeof Emoji.$type;
|
|
}[];
|
|
instance: typeof Instance.$type | null;
|
|
roles: {
|
|
userId: string;
|
|
roleId: string;
|
|
role?: typeof Role.$type;
|
|
}[];
|
|
endpoints: unknown;
|
|
},
|
|
): typeof User.$type => {
|
|
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>)
|
|
.emoji as typeof Emoji.$type,
|
|
),
|
|
roles: user.roles
|
|
.map((role) => role.role)
|
|
.filter(Boolean) as (typeof Role.$type)[],
|
|
};
|
|
};
|
|
|
|
export const findManyUsers = async (
|
|
query: Parameters<typeof db.query.Users.findMany>[0],
|
|
): Promise<(typeof User.$type)[]> => {
|
|
const output = await db.query.Users.findMany({
|
|
...query,
|
|
with: {
|
|
...userRelations,
|
|
...query?.with,
|
|
},
|
|
});
|
|
|
|
return output.map((user) => transformOutputToUserWithRelations(user));
|
|
};
|