mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
refactor(cli): ♻️ Rewrite instance fetch command to refetch instances instead
This commit is contained in:
parent
50ebc12783
commit
8b23eb888d
|
|
@ -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> {
|
public static getCount(): Promise<number> {
|
||||||
return db.$count(Instances);
|
return db.$count(Instances);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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<void> {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
56
cli/commands/federation/instance/refetch.ts
Normal file
56
cli/commands/federation/instance/refetch.ts
Normal file
|
|
@ -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<void> {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -16,8 +16,8 @@ export const commands = {
|
||||||
"emoji:list": (await import("./commands/emoji/list.ts")).default,
|
"emoji:list": (await import("./commands/emoji/list.ts")).default,
|
||||||
"emoji:import": (await import("./commands/emoji/import.ts")).default,
|
"emoji:import": (await import("./commands/emoji/import.ts")).default,
|
||||||
"index:rebuild": (await import("./commands/index/rebuild.ts")).default,
|
"index:rebuild": (await import("./commands/index/rebuild.ts")).default,
|
||||||
"federation:instance:fetch": (
|
"federation:instance:refetch": (
|
||||||
await import("./commands/federation/instance/fetch.ts")
|
await import("./commands/federation/instance/refetch.ts")
|
||||||
).default,
|
).default,
|
||||||
"federation:user:finger": (
|
"federation:user:finger": (
|
||||||
await import("./commands/federation/user/finger.ts")
|
await import("./commands/federation/user/finger.ts")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue