feat(api): 🏷️ Port Role and CustomEmoji OpenAPI schemas
Some checks failed
Mirror to Codeberg / Mirror (push) Failing after 1s

This commit is contained in:
Jesse Wierzbinski 2025-02-11 18:22:39 +01:00
parent 7c622730dc
commit 264e2fe8ac
No known key found for this signature in database
17 changed files with 319 additions and 177 deletions

View file

@ -1,7 +1,6 @@
import { emojiValidatorWithColons, emojiValidatorWithIdentifiers } from "@/api";
import { proxyUrl } from "@/response";
import { z } from "@hono/zod-openapi";
import type { Emoji as APIEmoji } from "@versia/client/types";
import type { z } from "@hono/zod-openapi";
import type { CustomEmojiExtension } from "@versia/federation/types";
import { type Instance, Media, db } from "@versia/kit/db";
import { Emojis, type Instances, type Medias } from "@versia/kit/tables";
@ -15,6 +14,7 @@ import {
inArray,
isNull,
} from "drizzle-orm";
import type { CustomEmoji } from "../schemas/emoji.ts";
import { BaseInterface } from "./base.ts";
type EmojiType = InferSelectModel<typeof Emojis> & {
@ -23,16 +23,6 @@ type EmojiType = InferSelectModel<typeof Emojis> & {
};
export class Emoji extends BaseInterface<typeof Emojis, EmojiType> {
public static schema = z.object({
id: z.string(),
shortcode: z.string(),
url: z.string(),
visible_in_picker: z.boolean(),
category: z.string().optional(),
static_url: z.string(),
global: z.boolean(),
});
public static $type: EmojiType;
public media: Media;
@ -184,18 +174,18 @@ export class Emoji extends BaseInterface<typeof Emojis, EmojiType> {
);
}
public toApi(): APIEmoji {
public toApi(): z.infer<typeof CustomEmoji> {
return {
id: this.id,
shortcode: this.data.shortcode,
static_url: proxyUrl(this.media.getUrl()).toString(),
url: proxyUrl(this.media.getUrl()).toString(),
visible_in_picker: this.data.visibleInPicker,
category: this.data.category ?? undefined,
category: this.data.category,
global: this.data.ownerId === null,
description:
this.media.data.content[this.media.getPreferredMimeType()]
.description ?? undefined,
.description ?? null,
};
}

View file

@ -12,6 +12,7 @@ import {
inArray,
} from "drizzle-orm";
import { config } from "~/packages/config-manager/index.ts";
import { CustomEmoji } from "../schemas/emoji.ts";
import { BaseInterface } from "./base.ts";
type ReactionType = InferSelectModel<typeof Reactions> & {
@ -30,7 +31,7 @@ export class Reaction extends BaseInterface<typeof Reactions, ReactionType> {
public static schema: z.ZodType<APIReaction> = z.object({
id: z.string().uuid(),
author_id: z.string().uuid(),
emoji: z.lazy(() => Emoji.schema),
emoji: CustomEmoji,
});
public static $type: ReactionType;
@ -169,16 +170,6 @@ export class Reaction extends BaseInterface<typeof Reactions, ReactionType> {
return this.data.id;
}
public toApi(): APIReaction {
return {
id: this.data.id,
author_id: this.data.authorId,
emoji: this.hasCustomEmoji()
? new Emoji(this.data.emoji as typeof Emoji.$type).toApi()
: this.data.emojiText || "",
};
}
public getUri(baseUrl: URL): URL {
return this.data.uri
? new URL(this.data.uri)

View file

@ -1,7 +1,6 @@
import { proxyUrl } from "@/response";
import { z } from "@hono/zod-openapi";
import {
type VersiaRole as APIRole,
import type {
VersiaRole as APIRole,
RolePermission,
} from "@versia/client/types";
import { db } from "@versia/kit/db";
@ -21,16 +20,6 @@ import { BaseInterface } from "./base.ts";
type RoleType = InferSelectModel<typeof Roles>;
export class Role extends BaseInterface<typeof Roles> {
public static schema = z.object({
id: z.string().uuid(),
name: z.string().min(1).max(128),
permissions: z.array(z.nativeEnum(RolePermission)).default([]),
priority: z.number().int().default(0),
description: z.string().min(0).max(1024).optional(),
visible: z.boolean().default(true),
icon: z.string().url().optional(),
});
public static $type: RoleType;
public async reload(): Promise<void> {

View file

@ -1,5 +1,5 @@
import { idValidator } from "@/api";
import { getBestContentType, urlToContentFormat } from "@/content_types";
import { getBestContentType } from "@/content_types";
import { randomString } from "@/math";
import { proxyUrl } from "@/response";
import { sentry } from "@/sentry";
@ -52,7 +52,7 @@ import { type Config, config } from "~/packages/config-manager";
import type { KnownEntity } from "~/types/api.ts";
import { DeliveryJobType, deliveryQueue } from "../queues/delivery.ts";
import { PushJobType, pushQueue } from "../queues/push.ts";
import type { Account } from "../schemas/account.ts";
import type { Account, Source } from "../schemas/account.ts";
import { BaseInterface } from "./base.ts";
import { Emoji } from "./emoji.ts";
import { Instance } from "./instance.ts";
@ -686,12 +686,12 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
note: getBestContentType(user.bio).content,
publicKey: user.public_key.key,
source: {
language: null,
language: "en",
note: "",
privacy: "public",
sensitive: false,
fields: [],
},
} as z.infer<typeof Source>,
};
const userEmojis =
@ -888,12 +888,12 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
privateKey: keys.private_key,
updatedAt: new Date().toISOString(),
source: {
language: null,
language: "en",
note: "",
privacy: "public",
sensitive: false,
fields: [],
},
} as z.infer<typeof Source>,
})
.returning()
)[0];
@ -1107,6 +1107,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
public toApi(isOwnAccount = false): z.infer<typeof Account> {
const user = this.data;
return {
id: user.id,
username: user.username,
@ -1177,6 +1178,8 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
)
.map((r) => r.toApi()),
group: false,
// TODO
last_status_at: null,
};
}
@ -1235,17 +1238,8 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
indexable: false,
username: user.username,
manually_approves_followers: this.data.isLocked,
avatar:
urlToContentFormat(
this.getAvatarUrl(config),
this.data.source.avatar?.content_type,
) ?? undefined,
header: this.getHeaderUrl(config)
? (urlToContentFormat(
this.getHeaderUrl(config) as URL,
this.data.source.header?.content_type,
) ?? undefined)
: undefined,
avatar: this.avatar?.toVersia(),
header: this.header?.toVersia(),
display_name: user.displayName,
fields: user.fields,
public_key: {