mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 16:38:19 +01:00
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import type { EntityValidator } from "@lysand-org/federation";
|
|
import { db } from "~drizzle/db";
|
|
import { Instances } from "~drizzle/schema";
|
|
|
|
/**
|
|
* Represents an instance in the database.
|
|
*/
|
|
|
|
/**
|
|
* Adds an instance to the database if it doesn't already exist.
|
|
* @param url
|
|
* @returns Either the database instance if it already exists, or a newly created instance.
|
|
*/
|
|
export const addInstanceIfNotExists = async (url: string) => {
|
|
const origin = new URL(url).origin;
|
|
const host = new URL(url).host;
|
|
|
|
const found = await db.query.Instances.findFirst({
|
|
where: (instance, { eq }) => eq(instance.baseUrl, host),
|
|
});
|
|
|
|
if (found) return found;
|
|
|
|
console.log(`Fetching instance metadata for ${origin}`);
|
|
|
|
// Fetch the instance configuration
|
|
const metadata = (await fetch(new URL("/.well-known/lysand", origin)).then(
|
|
(res) => res.json(),
|
|
)) as typeof EntityValidator.$ServerMetadata;
|
|
|
|
if (metadata.type !== "ServerMetadata") {
|
|
throw new Error("Invalid instance metadata (wrong type)");
|
|
}
|
|
|
|
if (!(metadata.name && metadata.version)) {
|
|
throw new Error("Invalid instance metadata (missing name or version)");
|
|
}
|
|
|
|
return (
|
|
await db
|
|
.insert(Instances)
|
|
.values({
|
|
baseUrl: host,
|
|
name: metadata.name,
|
|
version: metadata.version,
|
|
logo: metadata.logo,
|
|
})
|
|
.returning()
|
|
)[0];
|
|
};
|