diff --git a/api/api/v1/markers/index.ts b/api/api/v1/markers/index.ts index 864095bb..162dadf1 100644 --- a/api/api/v1/markers/index.ts +++ b/api/api/v1/markers/index.ts @@ -1,30 +1,16 @@ import { apiRoute, auth } from "@/api"; import { createRoute, z } from "@hono/zod-openapi"; -import type { Marker as ApiMarker } from "@versia/client/types"; import { db } from "@versia/kit/db"; import { Markers, RolePermissions } from "@versia/kit/tables"; import { type SQL, and, eq } from "drizzle-orm"; +import { Marker as MarkerSchema } from "~/classes/schemas/marker"; +import { Notification as NotificationSchema } from "~/classes/schemas/notification"; +import { Status as StatusSchema } from "~/classes/schemas/status"; -const schemas = { - markers: z.object({ - home: z - .object({ - last_read_id: z.string().uuid(), - version: z.number(), - updated_at: z.string(), - }) - .nullable() - .optional(), - notifications: z - .object({ - last_read_id: z.string().uuid(), - version: z.number(), - updated_at: z.string(), - }) - .nullable() - .optional(), - }), -}; +const MarkerResponseSchema = z.object({ + notifications: MarkerSchema.optional(), + home: MarkerSchema.optional(), +}); const routeGet = createRoute({ method: "get", @@ -50,7 +36,7 @@ const routeGet = createRoute({ description: "Markers", content: { "application/json": { - schema: schemas.markers, + schema: MarkerResponseSchema, }, }, }, @@ -69,8 +55,9 @@ const routePost = createRoute({ ] as const, request: { query: z.object({ - "home[last_read_id]": z.string().uuid().optional(), - "notifications[last_read_id]": z.string().uuid().optional(), + "home[last_read_id]": StatusSchema.shape.id.optional(), + "notifications[last_read_id]": + NotificationSchema.shape.id.optional(), }), }, responses: { @@ -78,7 +65,7 @@ const routePost = createRoute({ description: "Markers", content: { "application/json": { - schema: schemas.markers, + schema: MarkerResponseSchema, }, }, }, @@ -96,7 +83,7 @@ export default apiRoute((app) => { return context.json({}, 200); } - const markers: ApiMarker = { + const markers: z.infer = { home: undefined, notifications: undefined, }; @@ -160,7 +147,7 @@ export default apiRoute((app) => { } = context.req.valid("query"); const { user } = context.get("auth"); - const markers: ApiMarker = { + const markers: z.infer = { home: undefined, notifications: undefined, }; diff --git a/classes/database/note.ts b/classes/database/note.ts index 1edabc7d..929df1ae 100644 --- a/classes/database/note.ts +++ b/classes/database/note.ts @@ -829,6 +829,7 @@ export class Note extends BaseInterface { pinned: data.pinned, // TODO: Add polls poll: null, + // @ts-expect-error broken recursive types reblog: data.reblog ? await new Note(data.reblog as NoteTypeWithRelations).toApi( userFetching, @@ -844,6 +845,7 @@ export class Note extends BaseInterface { visibility: data.visibility, url: data.uri || this.getMastoUri().toString(), bookmarked: false, + // @ts-expect-error broken recursive types quote: data.quotingId ? ((await Note.fromId(data.quotingId, userFetching?.id).then( (n) => n?.toApi(userFetching), diff --git a/classes/database/pushsubscription.ts b/classes/database/pushsubscription.ts index b8cf4833..218a1bc4 100644 --- a/classes/database/pushsubscription.ts +++ b/classes/database/pushsubscription.ts @@ -1,7 +1,4 @@ -import type { - Alerts, - PushSubscription as ApiPushSubscription, -} from "@versia/client/types"; +import type { z } from "@hono/zod-openapi"; import { type Token, type User, db } from "@versia/kit/db"; import { PushSubscriptions, Tokens } from "@versia/kit/tables"; import { @@ -12,6 +9,7 @@ import { eq, inArray, } from "drizzle-orm"; +import type { WebPushSubscription as WebPushSubscriptionSchema } from "../schemas/pushsubscription.ts"; import { BaseInterface } from "./base.ts"; type PushSubscriptionType = InferSelectModel; @@ -165,7 +163,7 @@ export class PushSubscription extends BaseInterface< return this.data.id; } - public getAlerts(): Alerts { + public getAlerts(): z.infer { return { mention: this.data.alerts.mention ?? false, favourite: this.data.alerts.favourite ?? false, @@ -180,7 +178,7 @@ export class PushSubscription extends BaseInterface< }; } - public toApi(): ApiPushSubscription { + public toApi(): z.infer { return { id: this.data.id, alerts: this.getAlerts(), diff --git a/classes/database/token.ts b/classes/database/token.ts index a4b433af..c1fdadab 100644 --- a/classes/database/token.ts +++ b/classes/database/token.ts @@ -1,5 +1,4 @@ -import { z } from "@hono/zod-openapi"; -import type { Token as ApiToken } from "@versia/client/types"; +import type { z } from "@hono/zod-openapi"; import { type Application, User, db } from "@versia/kit/db"; import { Tokens } from "@versia/kit/tables"; import { @@ -10,6 +9,7 @@ import { eq, inArray, } from "drizzle-orm"; +import type { Token as TokenSchema } from "../schemas/token.ts"; import { BaseInterface } from "./base.ts"; type TokenType = InferSelectModel & { @@ -17,13 +17,6 @@ type TokenType = InferSelectModel & { }; export class Token extends BaseInterface { - public static schema: z.ZodType = z.object({ - access_token: z.string(), - token_type: z.enum(["bearer"]), - scope: z.string(), - created_at: z.number(), - }); - public static $type: TokenType; public async reload(): Promise { @@ -160,7 +153,7 @@ export class Token extends BaseInterface { return await User.fromId(this.data.userId); } - public toApi(): ApiToken { + public toApi(): z.infer { return { access_token: this.data.accessToken, token_type: "Bearer", diff --git a/classes/database/user.ts b/classes/database/user.ts index 36709e6a..6d403179 100644 --- a/classes/database/user.ts +++ b/classes/database/user.ts @@ -5,7 +5,6 @@ import { proxyUrl } from "@/response"; import { sentry } from "@/sentry"; import type { z } from "@hono/zod-openapi"; import { getLogger } from "@logtape/logtape"; -import type { Mention as ApiMention } from "@versia/client/types"; import { EntityValidator, FederationRequester, @@ -53,6 +52,7 @@ import type { KnownEntity } from "~/types/api.ts"; import { DeliveryJobType, deliveryQueue } from "../queues/delivery.ts"; import { PushJobType, pushQueue } from "../queues/push.ts"; import type { Account, Source } from "../schemas/account.ts"; +import type { Mention as MentionSchema } from "../schemas/status.ts"; import { BaseInterface } from "./base.ts"; import { Emoji } from "./emoji.ts"; import { Instance } from "./instance.ts"; @@ -1260,7 +1260,7 @@ export class User extends BaseInterface { }; } - public toMention(): ApiMention { + public toMention(): z.infer { return { url: this.getUri().toString(), username: this.data.username, diff --git a/classes/schemas/status.ts b/classes/schemas/status.ts index 9b3275ed..a8b32971 100644 --- a/classes/schemas/status.ts +++ b/classes/schemas/status.ts @@ -102,6 +102,7 @@ export const Status = z.object({ }, }), reblog: z + // @ts-expect-error broken recursive types .lazy((): z.ZodType => Status as z.ZodType) .nullable() .openapi({ @@ -308,6 +309,7 @@ export const Status = z.object({ }), reactions: z.array(NoteReaction).openapi({}), quote: z + // @ts-expect-error broken recursive types .lazy((): z.ZodType => Status as z.ZodType) .nullable(), bookmarked: zBoolean.optional().openapi({