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,7 +1,7 @@
import type { EntityValidator } from "@lysand-org/federation";
import { proxyUrl } from "@response";
import type { Config } from "config-manager";
import type { InferSelectModel } from "drizzle-orm";
import type * as Lysand from "lysand-types";
import { MediaBackendType } from "media-manager";
import { db } from "~drizzle/db";
import { Attachments } from "~drizzle/schema";
@ -65,7 +65,7 @@ export const attachmentToAPI = (
export const attachmentToLysand = (
attachment: Attachment,
): Lysand.ContentFormat => {
): typeof EntityValidator.$ContentFormat => {
return {
[attachment.mimeType]: {
content: attachment.url,
@ -86,7 +86,7 @@ export const attachmentToLysand = (
};
export const attachmentFromLysand = async (
attachmentToConvert: Lysand.ContentFormat,
attachmentToConvert: typeof EntityValidator.$ContentFormat,
): Promise<InferSelectModel<typeof Attachments>> => {
const key = Object.keys(attachmentToConvert)[0];
const value = attachmentToConvert[key];

View file

@ -1,7 +1,7 @@
import { emojiValidator, emojiValidatorWithColons } from "@api";
import { emojiValidatorWithColons } from "@api";
import type { EntityValidator } from "@lysand-org/federation";
import { proxyUrl } from "@response";
import { type InferSelectModel, and, eq } from "drizzle-orm";
import type * as Lysand from "lysand-types";
import { db } from "~drizzle/db";
import { Emojis, Instances } from "~drizzle/schema";
import type { Emoji as APIEmoji } from "~types/mastodon/emoji";
@ -41,7 +41,7 @@ export const parseEmojis = async (text: string) => {
* @returns The emoji
*/
export const fetchEmoji = async (
emojiToFetch: Lysand.Emoji,
emojiToFetch: (typeof EntityValidator.$CustomEmojiExtension)["emojis"][0],
host?: string,
): Promise<EmojiWithInstance> => {
const existingEmoji = await db
@ -71,7 +71,6 @@ export const fetchEmoji = async (
shortcode: emojiToFetch.name,
url: Object.entries(emojiToFetch.url)[0][1].content,
alt:
emojiToFetch.alt ||
Object.entries(emojiToFetch.url)[0][1].description ||
undefined,
contentType: Object.keys(emojiToFetch.url)[0],
@ -103,7 +102,9 @@ export const emojiToAPI = (emoji: EmojiWithInstance): APIEmoji => {
};
};
export const emojiToLysand = (emoji: EmojiWithInstance): Lysand.Emoji => {
export const emojiToLysand = (
emoji: EmojiWithInstance,
): (typeof EntityValidator.$CustomEmojiExtension)["emojis"][0] => {
return {
name: emoji.shortcode,
url: {
@ -112,6 +113,5 @@ export const emojiToLysand = (emoji: EmojiWithInstance): Lysand.Emoji => {
description: emoji.alt || undefined,
},
},
alt: emoji.alt || undefined,
};
};

View file

@ -1,11 +1,11 @@
import type { EntityValidator } from "@lysand-org/federation";
import { config } from "config-manager";
import type * as Lysand from "lysand-types";
import type { User } from "~packages/database-interface/user";
export const localObjectURI = (id: string) => `/objects/${id}`;
export const objectToInboxRequest = async (
object: Lysand.Entity,
object: typeof EntityValidator.$Entity,
author: User,
userToSendTo: User,
): Promise<Request> => {

View file

@ -1,4 +1,4 @@
import type * as Lysand from "lysand-types";
import type { EntityValidator } from "@lysand-org/federation";
import { db } from "~drizzle/db";
import { Instances } from "~drizzle/schema";
@ -26,7 +26,7 @@ export const addInstanceIfNotExists = async (url: string) => {
// Fetch the instance configuration
const metadata = (await fetch(new URL("/.well-known/lysand", origin)).then(
(res) => res.json(),
)) as Lysand.ServerMetadata;
)) as typeof EntityValidator.$ServerMetadata;
if (metadata.type !== "ServerMetadata") {
throw new Error("Invalid instance metadata (wrong type)");

View file

@ -1,6 +1,6 @@
import type { EntityValidator } from "@lysand-org/federation";
import { config } from "config-manager";
import { type InferSelectModel, and, eq } from "drizzle-orm";
import type * as Lysand from "lysand-types";
import { db } from "~drizzle/db";
import { Likes, Notifications } from "~drizzle/schema";
import type { Note } from "~packages/database-interface/note";
@ -11,7 +11,7 @@ export type Like = InferSelectModel<typeof Likes>;
/**
* Represents a Like entity in the database.
*/
export const likeToLysand = (like: Like): Lysand.Like => {
export const likeToLysand = (like: Like): typeof EntityValidator.$Like => {
return {
id: like.id,
// biome-ignore lint/suspicious/noExplicitAny: to be rewritten

View file

@ -1,6 +1,7 @@
import { mentionValidator } from "@api";
import markdownItTaskLists from "@hackmd/markdown-it-task-lists";
import { dualLogger } from "@loggers";
import type { EntityValidator } from "@lysand-org/federation";
import { sanitizeHtml, sanitizeHtmlInline } from "@sanitization";
import { config } from "config-manager";
import {
@ -13,7 +14,6 @@ import {
sql,
} from "drizzle-orm";
import linkifyHtml from "linkify-html";
import type * as Lysand from "lysand-types";
import {
anyOf,
charIn,
@ -253,7 +253,7 @@ export const findManyNotes = async (
export const resolveNote = async (
uri?: string,
providedNote?: Lysand.Note,
providedNote?: typeof EntityValidator.$Note,
): Promise<Note> => {
if (!uri && !providedNote) {
throw new Error("No URI or note provided");
@ -265,7 +265,7 @@ export const resolveNote = async (
if (foundStatus) return foundStatus;
let note: Lysand.Note | null = providedNote ?? null;
let note = providedNote ?? null;
if (uri) {
if (!URL.canParse(uri)) {
@ -279,7 +279,7 @@ export const resolveNote = async (
},
});
note = (await response.json()) as Lysand.Note;
note = (await response.json()) as typeof EntityValidator.$Note;
}
if (!note) {
@ -484,7 +484,7 @@ export const replaceTextMentions = async (text: string, mentions: User[]) => {
};
export const contentToHtml = async (
content: Lysand.ContentFormat,
content: typeof EntityValidator.$ContentFormat,
mentions: User[] = [],
inline = false,
): Promise<string> => {

View file

@ -1,8 +1,7 @@
import { dualLogger } from "@loggers";
import { addUserToMeilisearch } from "@meilisearch";
import type { EntityValidator } from "@lysand-org/federation";
import { config } from "config-manager";
import { type InferSelectModel, and, eq, inArray, sql } from "drizzle-orm";
import type * as Lysand from "lysand-types";
import { type InferSelectModel, and, eq, sql } from "drizzle-orm";
import { db } from "~drizzle/db";
import {
Applications,
@ -462,7 +461,7 @@ export const getRelationshipToOtherUser = async (
export const followRequestToLysand = (
follower: User,
followee: User,
): Lysand.Follow => {
): typeof EntityValidator.$Follow => {
if (follower.isRemote()) {
throw new Error("Follower must be a local user");
}
@ -490,7 +489,7 @@ export const followRequestToLysand = (
export const followAcceptToLysand = (
follower: User,
followee: User,
): Lysand.FollowAccept => {
): typeof EntityValidator.$FollowAccept => {
if (!follower.isRemote()) {
throw new Error("Follower must be a remote user");
}
@ -518,7 +517,7 @@ export const followAcceptToLysand = (
export const followRejectToLysand = (
follower: User,
followee: User,
): Lysand.FollowReject => {
): typeof EntityValidator.$FollowReject => {
return {
...followAcceptToLysand(follower, followee),
type: "FollowReject",