mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor: 🏷️ Move all types that represent ORM abstractions to ORM class static properties
This commit is contained in:
parent
ca31830fb3
commit
02c3c9d0bf
17 changed files with 143 additions and 136 deletions
|
|
@ -12,7 +12,7 @@ import {
|
|||
import { z } from "zod";
|
||||
import { BaseInterface } from "./base.ts";
|
||||
|
||||
export type ApplicationType = InferSelectModel<typeof Applications>;
|
||||
type ApplicationType = InferSelectModel<typeof Applications>;
|
||||
|
||||
export class Application extends BaseInterface<typeof Applications> {
|
||||
public static schema: z.ZodType<APIApplication> = z.object({
|
||||
|
|
@ -23,6 +23,8 @@ export class Application extends BaseInterface<typeof Applications> {
|
|||
scopes: z.string().optional(),
|
||||
});
|
||||
|
||||
public static $type: ApplicationType;
|
||||
|
||||
public async reload(): Promise<void> {
|
||||
const reloaded = await Application.fromId(this.data.id);
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ import { MediaBackendType } from "~/packages/config-manager/config.type";
|
|||
import { config } from "~/packages/config-manager/index.ts";
|
||||
import { BaseInterface } from "./base.ts";
|
||||
|
||||
export type AttachmentType = InferSelectModel<typeof Attachments>;
|
||||
type AttachmentType = InferSelectModel<typeof Attachments>;
|
||||
|
||||
export class Attachment extends BaseInterface<typeof Attachments> {
|
||||
public static schema: z.ZodType<ApiAttachment> = z.object({
|
||||
|
|
@ -47,6 +47,8 @@ export class Attachment extends BaseInterface<typeof Attachments> {
|
|||
blurhash: z.string().nullable(),
|
||||
});
|
||||
|
||||
public static $type: AttachmentType;
|
||||
|
||||
public async reload(): Promise<void> {
|
||||
const reloaded = await Attachment.fromId(this.data.id);
|
||||
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ import { z } from "zod";
|
|||
import { BaseInterface } from "./base.ts";
|
||||
import { Instance } from "./instance.ts";
|
||||
|
||||
export type EmojiWithInstance = InferSelectModel<typeof Emojis> & {
|
||||
type EmojiWithInstance = InferSelectModel<typeof Emojis> & {
|
||||
instance: InferSelectModel<typeof Instances> | null;
|
||||
};
|
||||
|
||||
|
|
@ -30,6 +30,8 @@ export class Emoji extends BaseInterface<typeof Emojis, EmojiWithInstance> {
|
|||
static_url: z.string(),
|
||||
});
|
||||
|
||||
public static $type: EmojiWithInstance;
|
||||
|
||||
public async reload(): Promise<void> {
|
||||
const reloaded = await Emoji.fromId(this.data.id);
|
||||
|
||||
|
|
|
|||
|
|
@ -20,9 +20,11 @@ import { config } from "~/packages/config-manager/index.ts";
|
|||
import { BaseInterface } from "./base.ts";
|
||||
import { User } from "./user.ts";
|
||||
|
||||
export type InstanceType = InferSelectModel<typeof Instances>;
|
||||
type InstanceType = InferSelectModel<typeof Instances>;
|
||||
|
||||
export class Instance extends BaseInterface<typeof Instances> {
|
||||
public static $type: InstanceType;
|
||||
|
||||
public async reload(): Promise<void> {
|
||||
const reloaded = await Instance.fromId(this.data.id);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,12 @@
|
|||
import { RolePermission } from "@versia/client/types";
|
||||
import type { Delete, LikeExtension } from "@versia/federation/types";
|
||||
import { db } from "@versia/kit/db";
|
||||
import { Likes, Notifications } from "@versia/kit/tables";
|
||||
import {
|
||||
Likes,
|
||||
type Notes,
|
||||
Notifications,
|
||||
type Users,
|
||||
} from "@versia/kit/tables";
|
||||
import {
|
||||
type InferInsertModel,
|
||||
type InferSelectModel,
|
||||
|
|
@ -13,15 +18,13 @@ import {
|
|||
} from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { config } from "~/packages/config-manager/index.ts";
|
||||
import type { Status } from "../functions/status.ts";
|
||||
import type { UserType } from "../functions/user.ts";
|
||||
import { BaseInterface } from "./base.ts";
|
||||
import { Note } from "./note.ts";
|
||||
import { User } from "./user.ts";
|
||||
|
||||
export type LikeType = InferSelectModel<typeof Likes> & {
|
||||
liker: UserType;
|
||||
liked: Status;
|
||||
type LikeType = InferSelectModel<typeof Likes> & {
|
||||
liker: InferSelectModel<typeof Users>;
|
||||
liked: InferSelectModel<typeof Notes>;
|
||||
};
|
||||
|
||||
export class Like extends BaseInterface<typeof Likes, LikeType> {
|
||||
|
|
@ -35,6 +38,8 @@ export class Like extends BaseInterface<typeof Likes, LikeType> {
|
|||
icon: z.string().nullable(),
|
||||
});
|
||||
|
||||
public static $type: LikeType;
|
||||
|
||||
public async reload(): Promise<void> {
|
||||
const reloaded = await Like.fromId(this.data.id);
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ import type {
|
|||
Delete as VersiaDelete,
|
||||
Note as VersiaNote,
|
||||
} from "@versia/federation/types";
|
||||
import { Notification, db } from "@versia/kit/db";
|
||||
import { type Instance, Notification, db } from "@versia/kit/db";
|
||||
import {
|
||||
Attachments,
|
||||
EmojiToNote,
|
||||
|
|
@ -24,6 +24,7 @@ import {
|
|||
} from "@versia/kit/tables";
|
||||
import {
|
||||
type InferInsertModel,
|
||||
type InferSelectModel,
|
||||
type SQL,
|
||||
and,
|
||||
desc,
|
||||
|
|
@ -36,7 +37,6 @@ import { htmlToText } from "html-to-text";
|
|||
import { createRegExp, exactly, global } from "magic-regexp";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
type StatusWithRelations,
|
||||
contentToHtml,
|
||||
findManyNotes,
|
||||
parseTextMentions,
|
||||
|
|
@ -48,10 +48,37 @@ import { BaseInterface } from "./base.ts";
|
|||
import { Emoji } from "./emoji.ts";
|
||||
import { User } from "./user.ts";
|
||||
|
||||
type NoteType = InferSelectModel<typeof Notes>;
|
||||
|
||||
type NoteTypeWithRelations = NoteType & {
|
||||
author: typeof User.$type;
|
||||
mentions: (InferSelectModel<typeof Users> & {
|
||||
instance: typeof Instance.$type | null;
|
||||
})[];
|
||||
attachments: (typeof Attachment.$type)[];
|
||||
reblog: NoteTypeWithoutRecursiveRelations | null;
|
||||
emojis: (typeof Emoji.$type)[];
|
||||
reply: NoteType | null;
|
||||
quote: NoteType | null;
|
||||
application: typeof Application.$type | null;
|
||||
reblogCount: number;
|
||||
likeCount: number;
|
||||
replyCount: number;
|
||||
pinned: boolean;
|
||||
reblogged: boolean;
|
||||
muted: boolean;
|
||||
liked: boolean;
|
||||
};
|
||||
|
||||
export type NoteTypeWithoutRecursiveRelations = Omit<
|
||||
NoteTypeWithRelations,
|
||||
"reply" | "quote" | "reblog"
|
||||
>;
|
||||
|
||||
/**
|
||||
* Gives helpers to fetch notes from database in a nice format
|
||||
*/
|
||||
export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
||||
export class Note extends BaseInterface<typeof Notes, NoteTypeWithRelations> {
|
||||
public static schema: z.ZodType<ApiStatus> = z.object({
|
||||
id: z.string().uuid(),
|
||||
uri: z.string().url(),
|
||||
|
|
@ -142,7 +169,9 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
bookmarked: z.boolean(),
|
||||
});
|
||||
|
||||
public save(): Promise<StatusWithRelations> {
|
||||
public static $type: NoteTypeWithRelations;
|
||||
|
||||
public save(): Promise<NoteTypeWithRelations> {
|
||||
return this.update(this.data);
|
||||
}
|
||||
|
||||
|
|
@ -814,8 +843,8 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
}
|
||||
|
||||
public async update(
|
||||
newStatus: Partial<StatusWithRelations>,
|
||||
): Promise<StatusWithRelations> {
|
||||
newStatus: Partial<NoteTypeWithRelations>,
|
||||
): Promise<NoteTypeWithRelations> {
|
||||
await db.update(Notes).set(newStatus).where(eq(Notes.id, this.data.id));
|
||||
|
||||
const updated = await Note.fromId(this.data.id);
|
||||
|
|
@ -932,7 +961,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
|||
// TODO: Add polls
|
||||
poll: null,
|
||||
reblog: data.reblog
|
||||
? await new Note(data.reblog as StatusWithRelations).toApi(
|
||||
? await new Note(data.reblog as NoteTypeWithRelations).toApi(
|
||||
userFetching,
|
||||
)
|
||||
: null,
|
||||
|
|
|
|||
|
|
@ -12,9 +12,7 @@ import {
|
|||
import { z } from "zod";
|
||||
import { MediaBackendType } from "~/packages/config-manager/config.type";
|
||||
import { config } from "~/packages/config-manager/index.ts";
|
||||
import type { StatusWithRelations } from "../functions/status.ts";
|
||||
import {
|
||||
type UserWithRelations,
|
||||
transformOutputToUserWithRelations,
|
||||
userExtrasTemplate,
|
||||
userRelations,
|
||||
|
|
@ -22,8 +20,8 @@ import {
|
|||
import { BaseInterface } from "./base.ts";
|
||||
|
||||
export type NotificationType = InferSelectModel<typeof Notifications> & {
|
||||
status: StatusWithRelations | null;
|
||||
account: UserWithRelations;
|
||||
status: typeof Note.$type | null;
|
||||
account: typeof User.$type;
|
||||
};
|
||||
|
||||
export class Notification extends BaseInterface<
|
||||
|
|
|
|||
|
|
@ -14,9 +14,9 @@ import { z } from "zod";
|
|||
import { BaseInterface } from "./base.ts";
|
||||
import type { User } from "./user.ts";
|
||||
|
||||
export type RelationshipType = InferSelectModel<typeof Relationships>;
|
||||
type RelationshipType = InferSelectModel<typeof Relationships>;
|
||||
|
||||
export type RelationshipWithOpposite = RelationshipType & {
|
||||
type RelationshipWithOpposite = RelationshipType & {
|
||||
followedBy: boolean;
|
||||
blockedBy: boolean;
|
||||
requestedBy: boolean;
|
||||
|
|
@ -43,6 +43,8 @@ export class Relationship extends BaseInterface<
|
|||
showing_reblogs: z.boolean(),
|
||||
});
|
||||
|
||||
public static $type: RelationshipWithOpposite;
|
||||
|
||||
public async reload(): Promise<void> {
|
||||
const reloaded = await Relationship.fromId(this.data.id);
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ import { z } from "zod";
|
|||
import { config } from "~/packages/config-manager/index.ts";
|
||||
import { BaseInterface } from "./base.ts";
|
||||
|
||||
export type RoleType = InferSelectModel<typeof Roles>;
|
||||
type RoleType = InferSelectModel<typeof Roles>;
|
||||
|
||||
export class Role extends BaseInterface<typeof Roles> {
|
||||
public static schema = z.object({
|
||||
|
|
@ -31,6 +31,8 @@ export class Role extends BaseInterface<typeof Roles> {
|
|||
icon: z.string().nullable(),
|
||||
});
|
||||
|
||||
public static $type: RoleType;
|
||||
|
||||
public async reload(): Promise<void> {
|
||||
const reloaded = await Role.fromId(this.data.id);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import type { Token as ApiToken } from "@versia/client/types";
|
||||
import { User, db } from "@versia/kit/db";
|
||||
import { type Applications, Tokens } from "@versia/kit/tables";
|
||||
import { type Application, User, db } from "@versia/kit/db";
|
||||
import { Tokens } from "@versia/kit/tables";
|
||||
import {
|
||||
type InferInsertModel,
|
||||
type InferSelectModel,
|
||||
|
|
@ -12,8 +12,8 @@ import {
|
|||
import { z } from "zod";
|
||||
import { BaseInterface } from "./base.ts";
|
||||
|
||||
export type TokenType = InferSelectModel<typeof Tokens> & {
|
||||
application: InferSelectModel<typeof Applications> | null;
|
||||
type TokenType = InferSelectModel<typeof Tokens> & {
|
||||
application: typeof Application.$type | null;
|
||||
};
|
||||
|
||||
export class Token extends BaseInterface<typeof Tokens, TokenType> {
|
||||
|
|
@ -24,6 +24,8 @@ export class Token extends BaseInterface<typeof Tokens, TokenType> {
|
|||
created_at: z.number(),
|
||||
});
|
||||
|
||||
public static $type: TokenType;
|
||||
|
||||
public async reload(): Promise<void> {
|
||||
const reloaded = await Token.fromId(this.data.id);
|
||||
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ import {
|
|||
import chalk from "chalk";
|
||||
import {
|
||||
type InferInsertModel,
|
||||
type InferSelectModel,
|
||||
type SQL,
|
||||
and,
|
||||
countDistinct,
|
||||
|
|
@ -47,7 +48,6 @@ import {
|
|||
import { htmlToText } from "html-to-text";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
type UserWithRelations,
|
||||
findManyUsers,
|
||||
followAcceptToVersia,
|
||||
followRejectToVersia,
|
||||
|
|
@ -64,6 +64,18 @@ import type { Note } from "./note.ts";
|
|||
import { Relationship } from "./relationship.ts";
|
||||
import { Role } from "./role.ts";
|
||||
|
||||
type UserWithInstance = InferSelectModel<typeof Users> & {
|
||||
instance: typeof Instance.$type | null;
|
||||
};
|
||||
|
||||
type UserWithRelations = UserWithInstance & {
|
||||
emojis: (typeof Emoji.$type)[];
|
||||
followerCount: number;
|
||||
followingCount: number;
|
||||
statusCount: number;
|
||||
roles: (typeof Role.$type)[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Gives helpers to fetch users from database in a nice format
|
||||
*/
|
||||
|
|
@ -125,6 +137,8 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
|||
mute_expires_at: z.string().optional(),
|
||||
});
|
||||
|
||||
public static $type: UserWithRelations;
|
||||
|
||||
public async reload(): Promise<void> {
|
||||
const reloaded = await User.fromId(this.data.id);
|
||||
|
||||
|
|
|
|||
|
|
@ -2,22 +2,9 @@ import { mentionValidator } from "@/api";
|
|||
import { sanitizeHtml, sanitizeHtmlInline } from "@/sanitization";
|
||||
import markdownItTaskLists from "@hackmd/markdown-it-task-lists";
|
||||
import type { ContentFormat } from "@versia/federation/types";
|
||||
import { User, db } from "@versia/kit/db";
|
||||
import {
|
||||
type Attachments,
|
||||
Instances,
|
||||
type Notes,
|
||||
Users,
|
||||
} from "@versia/kit/tables";
|
||||
import {
|
||||
type InferSelectModel,
|
||||
and,
|
||||
eq,
|
||||
inArray,
|
||||
isNull,
|
||||
or,
|
||||
sql,
|
||||
} from "drizzle-orm";
|
||||
import { type Note, User, db } from "@versia/kit/db";
|
||||
import { Instances, Users } from "@versia/kit/tables";
|
||||
import { and, eq, inArray, isNull, or, sql } from "drizzle-orm";
|
||||
import linkifyHtml from "linkify-html";
|
||||
import {
|
||||
anyOf,
|
||||
|
|
@ -31,42 +18,13 @@ import {
|
|||
import MarkdownIt from "markdown-it";
|
||||
import markdownItContainer from "markdown-it-container";
|
||||
import markdownItTocDoneRight from "markdown-it-toc-done-right";
|
||||
import type { ApplicationType } from "~/classes/database/application.ts";
|
||||
import type { EmojiWithInstance } from "~/classes/database/emoji.ts";
|
||||
import { config } from "~/packages/config-manager/index.ts";
|
||||
import {
|
||||
type UserWithInstance,
|
||||
type UserWithRelations,
|
||||
transformOutputToUserWithRelations,
|
||||
userExtrasTemplate,
|
||||
userRelations,
|
||||
} from "./user.ts";
|
||||
|
||||
export type Status = InferSelectModel<typeof Notes>;
|
||||
|
||||
export type StatusWithRelations = Status & {
|
||||
author: UserWithRelations;
|
||||
mentions: UserWithInstance[];
|
||||
attachments: InferSelectModel<typeof Attachments>[];
|
||||
reblog: StatusWithoutRecursiveRelations | null;
|
||||
emojis: EmojiWithInstance[];
|
||||
reply: Status | null;
|
||||
quote: Status | null;
|
||||
application: ApplicationType | null;
|
||||
reblogCount: number;
|
||||
likeCount: number;
|
||||
replyCount: number;
|
||||
pinned: boolean;
|
||||
reblogged: boolean;
|
||||
muted: boolean;
|
||||
liked: boolean;
|
||||
};
|
||||
|
||||
export type StatusWithoutRecursiveRelations = Omit<
|
||||
StatusWithRelations,
|
||||
"reply" | "quote" | "reblog"
|
||||
>;
|
||||
|
||||
/**
|
||||
* Wrapper against the Status object to make it easier to work with
|
||||
* @param query
|
||||
|
|
@ -75,7 +33,7 @@ export type StatusWithoutRecursiveRelations = Omit<
|
|||
export const findManyNotes = async (
|
||||
query: Parameters<typeof db.query.Notes.findMany>[0],
|
||||
userId?: string,
|
||||
): Promise<StatusWithRelations[]> => {
|
||||
): Promise<(typeof Note.$type)[]> => {
|
||||
const output = await db.query.Notes.findMany({
|
||||
...query,
|
||||
with: {
|
||||
|
|
|
|||
|
|
@ -3,25 +3,17 @@ import type {
|
|||
FollowAccept,
|
||||
FollowReject,
|
||||
} from "@versia/federation/types";
|
||||
import { type Application, type Token, type User, db } from "@versia/kit/db";
|
||||
import type { Instances, Roles, Users } from "@versia/kit/tables";
|
||||
import {
|
||||
type Application,
|
||||
type Emoji,
|
||||
type Instance,
|
||||
type Role,
|
||||
type Token,
|
||||
type User,
|
||||
db,
|
||||
} from "@versia/kit/db";
|
||||
import type { Users } from "@versia/kit/tables";
|
||||
import { type InferSelectModel, type SQL, sql } from "drizzle-orm";
|
||||
import type { EmojiWithInstance } from "~/classes/database/emoji.ts";
|
||||
|
||||
export type UserType = InferSelectModel<typeof Users>;
|
||||
|
||||
export type UserWithInstance = UserType & {
|
||||
instance: InferSelectModel<typeof Instances> | null;
|
||||
};
|
||||
|
||||
export type UserWithRelations = UserType & {
|
||||
instance: InferSelectModel<typeof Instances> | null;
|
||||
emojis: EmojiWithInstance[];
|
||||
followerCount: number;
|
||||
followingCount: number;
|
||||
statusCount: number;
|
||||
roles: InferSelectModel<typeof Roles>[];
|
||||
};
|
||||
|
||||
export const userRelations = {
|
||||
instance: true,
|
||||
|
|
@ -84,24 +76,24 @@ export interface AuthData {
|
|||
}
|
||||
|
||||
export const transformOutputToUserWithRelations = (
|
||||
user: Omit<UserType, "endpoints"> & {
|
||||
user: Omit<InferSelectModel<typeof Users>, "endpoints"> & {
|
||||
followerCount: unknown;
|
||||
followingCount: unknown;
|
||||
statusCount: unknown;
|
||||
emojis: {
|
||||
userId: string;
|
||||
emojiId: string;
|
||||
emoji?: EmojiWithInstance;
|
||||
emoji?: typeof Emoji.$type;
|
||||
}[];
|
||||
instance: InferSelectModel<typeof Instances> | null;
|
||||
instance: typeof Instance.$type | null;
|
||||
roles: {
|
||||
userId: string;
|
||||
roleId: string;
|
||||
role?: InferSelectModel<typeof Roles>;
|
||||
role?: typeof Role.$type;
|
||||
}[];
|
||||
endpoints: unknown;
|
||||
},
|
||||
): UserWithRelations => {
|
||||
): typeof User.$type => {
|
||||
return {
|
||||
...user,
|
||||
followerCount: Number(user.followerCount),
|
||||
|
|
@ -121,17 +113,17 @@ export const transformOutputToUserWithRelations = (
|
|||
emojis: user.emojis.map(
|
||||
(emoji) =>
|
||||
(emoji as unknown as Record<string, object>)
|
||||
.emoji as EmojiWithInstance,
|
||||
.emoji as typeof Emoji.$type,
|
||||
),
|
||||
roles: user.roles
|
||||
.map((role) => role.role)
|
||||
.filter(Boolean) as InferSelectModel<typeof Roles>[],
|
||||
.filter(Boolean) as (typeof Role.$type)[],
|
||||
};
|
||||
};
|
||||
|
||||
export const findManyUsers = async (
|
||||
query: Parameters<typeof db.query.Users.findMany>[0],
|
||||
): Promise<UserWithRelations[]> => {
|
||||
): Promise<(typeof User.$type)[]> => {
|
||||
const output = await db.query.Users.findMany({
|
||||
...query,
|
||||
with: {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue