refactor(cli): ♻️ Rewrite instance fetch command to refetch instances instead

This commit is contained in:
Jesse Wierzbinski 2024-11-24 22:45:41 +01:00
parent 50ebc12783
commit 8b23eb888d
No known key found for this signature in database
4 changed files with 88 additions and 50 deletions

View file

@ -362,6 +362,36 @@ export class Instance extends BaseInterface<typeof Instances> {
});
}
public static async updateFromRemote(url: string): Promise<Instance> {
const logger = getLogger("federation");
const host = new URL(url).host;
const instance = await Instance.fromSql(eq(Instances.baseUrl, host));
if (!instance) {
throw new Error("Instance not found");
}
const output = await Instance.fetchMetadata(url);
if (!output) {
logger.error`Failed to update instance ${chalk.bold(host)}`;
throw new Error("Failed to update instance");
}
const { metadata, protocol } = output;
await instance.update({
name: metadata.name,
version: metadata.software.version,
logo: metadata.logo,
protocol,
publicKey: metadata.public_key,
});
return instance;
}
public static getCount(): Promise<number> {
return db.$count(Instances);
}