feat(federation): Replace old types and federation validators with @lysand-org/federation

This commit is contained in:
Jesse Wierzbinski 2024-05-14 14:35:13 -10:00
parent 25d087a54b
commit 5fd6a4e43d
No known key found for this signature in database
16 changed files with 80 additions and 65 deletions

View file

@ -1,3 +1,4 @@
import type { EntityValidator } from "@lysand-org/federation";
import { proxyUrl } from "@response";
import { sanitizedHtmlStrip } from "@sanitization";
import {
@ -12,7 +13,6 @@ import {
sql,
} from "drizzle-orm";
import { htmlToText } from "html-to-text";
import type * as Lysand from "lysand-types";
import { createRegExp, exactly, global } from "magic-regexp";
import {
type Application,
@ -210,7 +210,7 @@ export class Note {
static async fromData(
author: User,
content: Lysand.ContentFormat,
content: typeof EntityValidator.$ContentFormat,
visibility: APIStatus["visibility"],
is_sensitive: boolean,
spoiler_text: string,
@ -303,7 +303,7 @@ export class Note {
}
async updateFromData(
content?: Lysand.ContentFormat,
content?: typeof EntityValidator.$ContentFormat,
visibility?: APIStatus["visibility"],
is_sensitive?: boolean,
spoiler_text?: string,
@ -539,7 +539,7 @@ export class Note {
return `/@${this.getAuthor().getUser().username}/${this.id}`;
}
toLysand(): Lysand.Note {
toLysand(): typeof EntityValidator.$Note {
const status = this.getStatus();
return {
type: "Note",
@ -563,7 +563,11 @@ export class Note {
quotes: Note.getURI(status.quotingId) ?? undefined,
replies_to: Note.getURI(status.replyId) ?? undefined,
subject: status.spoilerText,
visibility: status.visibility as Lysand.Visibility,
visibility: status.visibility as
| "public"
| "unlisted"
| "private"
| "direct",
extensions: {
"org.lysand:custom_emojis": {
emojis: status.emojis.map((emoji) => emojiToLysand(emoji)),

View file

@ -1,5 +1,6 @@
import { idValidator } from "@api";
import { getBestContentType, urlToContentFormat } from "@content_types";
import type { EntityValidator } from "@lysand-org/federation";
import { addUserToMeilisearch } from "@meilisearch";
import { proxyUrl } from "@response";
import {
@ -14,7 +15,6 @@ import {
isNull,
} from "drizzle-orm";
import { htmlToText } from "html-to-text";
import type * as Lysand from "lysand-types";
import {
emojiToAPI,
emojiToLysand,
@ -206,7 +206,9 @@ export class User {
},
});
const data = (await response.json()) as Partial<Lysand.User>;
const data = (await response.json()) as Partial<
typeof EntityValidator.$User
>;
if (
!(
@ -255,7 +257,11 @@ export class User {
inbox: data.inbox,
outbox: data.outbox,
},
fields: data.fields ?? [],
fields:
data.fields?.map((f) => ({
key: f.name,
value: f.value,
})) ?? [],
updatedAt: new Date(data.created_at).toISOString(),
instanceId: instance.id,
avatar: data.avatar
@ -467,7 +473,7 @@ export class User {
};
}
toLysand(): Lysand.User {
toLysand(): typeof EntityValidator.$User {
if (this.isRemote()) {
throw new Error("Cannot convert remote user to Lysand format");
}
@ -520,7 +526,10 @@ export class User {
avatar: urlToContentFormat(this.getAvatarUrl(config)) ?? undefined,
header: urlToContentFormat(this.getHeaderUrl(config)) ?? undefined,
display_name: user.displayName,
fields: user.fields,
fields: user.fields.map((f) => ({
name: f.key,
value: f.value,
})),
public_key: {
actor: new URL(
`/users/${user.id}`,