2024-07-16 23:29:20 +02:00
|
|
|
import { parseUserAddress, userAddressValidator } from "@/api";
|
2024-06-30 10:24:10 +02:00
|
|
|
import { Args } from "@oclif/core";
|
2024-11-01 20:57:16 +01:00
|
|
|
import { Instance, User } from "@versia/kit/db";
|
2024-06-30 10:24:10 +02:00
|
|
|
import chalk from "chalk";
|
|
|
|
|
import ora from "ora";
|
|
|
|
|
import { BaseCommand } from "~/cli/base";
|
|
|
|
|
|
|
|
|
|
export default class FederationUserFetch extends BaseCommand<
|
|
|
|
|
typeof FederationUserFetch
|
|
|
|
|
> {
|
2024-11-01 21:20:12 +01:00
|
|
|
public static override args = {
|
2024-06-30 10:24:10 +02:00
|
|
|
address: Args.string({
|
|
|
|
|
description: "Address of remote user (name@host.com)",
|
|
|
|
|
required: true,
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-01 21:20:12 +01:00
|
|
|
public static override description = "Fetch remote users";
|
2024-06-30 10:24:10 +02:00
|
|
|
|
2024-11-01 21:20:12 +01:00
|
|
|
public static override examples = ["<%= config.bin %> <%= command.id %>"];
|
2024-06-30 10:24:10 +02:00
|
|
|
|
2024-11-01 21:20:12 +01:00
|
|
|
public static override flags = {};
|
2024-06-30 10:24:10 +02:00
|
|
|
|
|
|
|
|
public async run(): Promise<void> {
|
|
|
|
|
const { args } = await this.parse(FederationUserFetch);
|
|
|
|
|
|
2024-07-16 23:29:20 +02:00
|
|
|
// Check if the address is valid
|
|
|
|
|
if (!args.address.match(userAddressValidator)) {
|
|
|
|
|
this.log(
|
|
|
|
|
"Invalid address. Please check the address format and try again. For example: name@host.com",
|
|
|
|
|
);
|
2024-06-30 10:24:10 +02:00
|
|
|
|
2024-07-16 23:29:20 +02:00
|
|
|
this.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const spinner = ora("Fetching user").start();
|
|
|
|
|
|
|
|
|
|
const { username, domain: host } = parseUserAddress(args.address);
|
|
|
|
|
|
2024-12-02 15:43:56 +01:00
|
|
|
if (!host) {
|
|
|
|
|
this.log("Address must contain a domain.");
|
|
|
|
|
this.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-16 23:29:20 +02:00
|
|
|
// Check instance exists, if not, create it
|
2025-02-01 16:32:18 +01:00
|
|
|
await Instance.resolve(new URL(`https://${host}`));
|
2024-06-30 10:24:10 +02:00
|
|
|
|
2024-08-26 19:27:40 +02:00
|
|
|
const manager = await User.getFederationRequester();
|
2024-06-30 10:24:10 +02:00
|
|
|
|
2024-07-26 18:07:11 +02:00
|
|
|
const uri = await User.webFinger(manager, username, host);
|
2024-06-30 10:24:10 +02:00
|
|
|
|
2024-12-09 15:01:19 +01:00
|
|
|
if (!uri) {
|
|
|
|
|
spinner.fail();
|
|
|
|
|
this.log(chalk.red("User not found"));
|
|
|
|
|
this.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-16 23:29:20 +02:00
|
|
|
const newUser = await User.resolve(uri);
|
2024-06-30 10:24:10 +02:00
|
|
|
|
2024-07-16 23:29:20 +02:00
|
|
|
if (newUser) {
|
|
|
|
|
spinner.succeed();
|
|
|
|
|
this.log(chalk.green(`User found: ${newUser.getUri()}`));
|
|
|
|
|
} else {
|
|
|
|
|
spinner.fail();
|
|
|
|
|
this.log(chalk.red("User not found"));
|
|
|
|
|
}
|
2024-06-30 10:24:10 +02:00
|
|
|
|
|
|
|
|
this.exit(0);
|
|
|
|
|
}
|
|
|
|
|
}
|