From 12032695318fd919d730ec3ef7d2c82ba726ef23 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Mon, 26 Aug 2024 19:30:56 +0200 Subject: [PATCH] feat(federation): :sparkles: Add Collections --- federation/schemas.ts | 2 ++ federation/schemas/base.ts | 14 ++++++++++++++ federation/validator.ts | 11 +++++++++++ 3 files changed, 27 insertions(+) diff --git a/federation/schemas.ts b/federation/schemas.ts index ec70413..68b621e 100644 --- a/federation/schemas.ts +++ b/federation/schemas.ts @@ -5,6 +5,7 @@ import type { z } from "zod"; import type { + CollectionSchema, DeleteSchema, EntitySchema, FollowAcceptSchema, @@ -31,6 +32,7 @@ type AnyZod = z.ZodType; type InferType = z.infer; export type Note = InferType; +export type Collection = InferType; export type EntityExtensionProperty = InferType; export type VanityExtension = InferType; export type User = InferType; diff --git a/federation/schemas/base.ts b/federation/schemas/base.ts index 76ce0d4..76e8a8b 100644 --- a/federation/schemas/base.ts +++ b/federation/schemas/base.ts @@ -105,6 +105,20 @@ export const NoteSchema = EntitySchema.extend({ .nullable(), }); +export const CollectionSchema = z.object({ + author: z.string().url().nullable(), + first: z.string().url(), + last: z.string().url(), + total: z + .number() + .int() + .nonnegative() + .max(2 ** 64 - 1), + next: z.string().url().nullable(), + previous: z.string().url().nullable(), + items: z.array(z.any()), +}); + export const PublicKeyDataSchema = z .object({ key: z.string().min(1), diff --git a/federation/validator.ts b/federation/validator.ts index 3df9412..e1ae058 100644 --- a/federation/validator.ts +++ b/federation/validator.ts @@ -1,6 +1,7 @@ import type { z } from "zod"; import { fromError } from "zod-validation-error"; import type { + Collection, ContentFormat, CustomEmojiExtension, Delete, @@ -22,6 +23,7 @@ import type { VanityExtension, } from "./schemas"; import { + CollectionSchema, DeleteSchema, EntitySchema, FollowAcceptSchema, @@ -99,6 +101,15 @@ export class EntityValidator { return this.validate(NoteSchema, data); } + /** + * Validates a Collection entity. + * @param data - The data to validate + * @returns A promise that resolves to the validated data. + */ + public Collection(data: unknown): Promise { + return this.validate(CollectionSchema, data); + } + /** * Validates a VanityExtension entity. * @param data - The data to validate