From 8b23eb888d2a91b578514ff53f3f2ab04d3197d7 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Sun, 24 Nov 2024 22:45:41 +0100 Subject: [PATCH] refactor(cli): :recycle: Rewrite instance fetch command to refetch instances instead --- classes/database/instance.ts | 30 +++++++++++ cli/commands/federation/instance/fetch.ts | 48 ------------------ cli/commands/federation/instance/refetch.ts | 56 +++++++++++++++++++++ cli/index.ts | 4 +- 4 files changed, 88 insertions(+), 50 deletions(-) delete mode 100644 cli/commands/federation/instance/fetch.ts create mode 100644 cli/commands/federation/instance/refetch.ts diff --git a/classes/database/instance.ts b/classes/database/instance.ts index 5e3ba333..0f963f46 100644 --- a/classes/database/instance.ts +++ b/classes/database/instance.ts @@ -362,6 +362,36 @@ export class Instance extends BaseInterface { }); } + public static async updateFromRemote(url: string): Promise { + 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 { return db.$count(Instances); } diff --git a/cli/commands/federation/instance/fetch.ts b/cli/commands/federation/instance/fetch.ts deleted file mode 100644 index 5687e37b..00000000 --- a/cli/commands/federation/instance/fetch.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { Args } from "@oclif/core"; -import { Instance } from "@versia/kit/db"; -import ora from "ora"; -import { BaseCommand } from "~/cli/base"; -import { formatArray } from "~/cli/utils/format"; - -export default class FederationInstanceFetch extends BaseCommand< - typeof FederationInstanceFetch -> { - public static override args = { - url: Args.string({ - description: "URL of the remote instance", - required: true, - }), - }; - - public static override description = "Fetch metadata from remote instances"; - - public static override examples = ["<%= config.bin %> <%= command.id %>"]; - - public static override flags = {}; - - public async run(): Promise { - const { args } = await this.parse(FederationInstanceFetch); - - const spinner = ora("Fetching instance metadata").start(); - - const data = await Instance.fetchMetadata(args.url); - - if (!data) { - spinner.fail("Failed to fetch instance metadata"); - this.exit(1); - } - - spinner.succeed("Fetched instance metadata"); - - const { metadata, protocol } = data; - - this.log( - formatArray( - [{ ...metadata, protocol }], - ["name", "description", "version", "protocol"], - ), - ); - - this.exit(0); - } -} diff --git a/cli/commands/federation/instance/refetch.ts b/cli/commands/federation/instance/refetch.ts new file mode 100644 index 00000000..ecea7f7c --- /dev/null +++ b/cli/commands/federation/instance/refetch.ts @@ -0,0 +1,56 @@ +import { Args } from "@oclif/core"; +import { Instance } from "@versia/kit/db"; +import ora from "ora"; +import { BaseCommand } from "~/cli/base"; +import { formatArray } from "~/cli/utils/format"; + +export default class FederationInstanceRefetch extends BaseCommand< + typeof FederationInstanceRefetch +> { + public static override args = { + url: Args.string({ + description: "URL of the remote instance", + required: true, + }), + }; + + public static override description = + "Refetches metadata from remote instances"; + + public static override examples = ["<%= config.bin %> <%= command.id %>"]; + + public static override flags = {}; + + public async run(): Promise { + const { args } = await this.parse(FederationInstanceRefetch); + + const spinner = ora("Refetching instance metadata").start(); + + const data = await Instance.updateFromRemote(args.url); + + if (!data) { + spinner.fail("Failed to refetch instance metadata"); + this.exit(1); + } + + spinner.succeed("Refetched instance metadata"); + + const { name, baseUrl, protocol, version } = data.data; + + this.log( + formatArray( + [ + { + Name: name, + "Base URL": baseUrl, + Version: version, + Protocol: protocol, + }, + ], + ["Name", "Base URL", "Version", "Protocol"], + ), + ); + + this.exit(0); + } +} diff --git a/cli/index.ts b/cli/index.ts index e93c4a32..c547921b 100644 --- a/cli/index.ts +++ b/cli/index.ts @@ -16,8 +16,8 @@ export const commands = { "emoji:list": (await import("./commands/emoji/list.ts")).default, "emoji:import": (await import("./commands/emoji/import.ts")).default, "index:rebuild": (await import("./commands/index/rebuild.ts")).default, - "federation:instance:fetch": ( - await import("./commands/federation/instance/fetch.ts") + "federation:instance:refetch": ( + await import("./commands/federation/instance/refetch.ts") ).default, "federation:user:finger": ( await import("./commands/federation/user/finger.ts")