2023-01-13 04:40:33 +00:00
|
|
|
import { URL } from "node:url";
|
|
|
|
import chalk from "chalk";
|
|
|
|
import { IsNull } from "typeorm";
|
|
|
|
import config from "@/config/index.js";
|
|
|
|
import type { User, IRemoteUser } from "@/models/entities/user.js";
|
|
|
|
import { Users } from "@/models/index.js";
|
|
|
|
import { toPuny } from "@/misc/convert-host.js";
|
|
|
|
import webFinger from "./webfinger.js";
|
|
|
|
import { createPerson, updatePerson } from "./activitypub/models/person.js";
|
|
|
|
import { remoteLogger } from "./logger.js";
|
|
|
|
|
|
|
|
const logger = remoteLogger.createSubLogger("resolve-user");
|
|
|
|
|
|
|
|
export async function resolveUser(
|
|
|
|
username: string,
|
|
|
|
host: string | null,
|
|
|
|
): Promise<User> {
|
2018-03-31 10:55:00 +00:00
|
|
|
const usernameLower = username.toLowerCase();
|
2018-05-06 19:26:45 +00:00
|
|
|
|
2019-04-09 15:59:41 +00:00
|
|
|
if (host == null) {
|
2019-02-02 19:04:57 +00:00
|
|
|
logger.info(`return local user: ${usernameLower}`);
|
2023-01-13 04:40:33 +00:00
|
|
|
return await Users.findOneBy({ usernameLower, host: IsNull() }).then(
|
|
|
|
(u) => {
|
|
|
|
if (u == null) {
|
|
|
|
throw new Error("user not found");
|
|
|
|
} else {
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2018-05-06 19:26:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
host = toPuny(host);
|
|
|
|
|
2022-02-28 16:24:50 +00:00
|
|
|
if (config.host === host) {
|
2019-02-02 19:04:57 +00:00
|
|
|
logger.info(`return local user: ${usernameLower}`);
|
2023-01-13 04:40:33 +00:00
|
|
|
return await Users.findOneBy({ usernameLower, host: IsNull() }).then(
|
|
|
|
(u) => {
|
|
|
|
if (u == null) {
|
|
|
|
throw new Error("user not found");
|
|
|
|
} else {
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2018-04-08 06:25:17 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
const user = (await Users.findOneBy({
|
|
|
|
usernameLower,
|
|
|
|
host,
|
|
|
|
})) as IRemoteUser | null;
|
2018-10-30 17:16:13 +00:00
|
|
|
|
2019-04-09 15:59:41 +00:00
|
|
|
const acctLower = `${usernameLower}@${host}`;
|
2018-03-31 10:55:00 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (user == null) {
|
2018-10-30 17:16:13 +00:00
|
|
|
const self = await resolveSelf(acctLower);
|
|
|
|
|
2019-02-02 19:43:43 +00:00
|
|
|
logger.succ(`return new remote user: ${chalk.magenta(acctLower)}`);
|
2018-10-30 17:16:13 +00:00
|
|
|
return await createPerson(self.href);
|
|
|
|
}
|
|
|
|
|
2022-12-06 16:46:08 +00:00
|
|
|
// If user information is out of date, return it by starting over from WebFilger
|
2023-01-13 04:40:33 +00:00
|
|
|
if (
|
|
|
|
user.lastFetchedAt == null ||
|
|
|
|
Date.now() - user.lastFetchedAt.getTime() > 1000 * 60 * 60 * 24
|
|
|
|
) {
|
2022-12-06 16:46:08 +00:00
|
|
|
// Prevent multiple attempts to connect to unconnected instances, update before each attempt to prevent subsequent similar attempts
|
2019-04-21 14:57:44 +00:00
|
|
|
await Users.update(user.id, {
|
|
|
|
lastFetchedAt: new Date(),
|
|
|
|
});
|
|
|
|
|
2019-02-02 19:04:57 +00:00
|
|
|
logger.info(`try resync: ${acctLower}`);
|
2018-10-30 17:16:13 +00:00
|
|
|
const self = await resolveSelf(acctLower);
|
|
|
|
|
2019-04-21 14:57:44 +00:00
|
|
|
if (user.uri !== self.href) {
|
2018-10-30 17:16:13 +00:00
|
|
|
// if uri mismatch, Fix (user@host <=> AP's Person id(IRemoteUser.uri)) mapping.
|
2019-02-02 19:04:57 +00:00
|
|
|
logger.info(`uri missmatch: ${acctLower}`);
|
2023-01-13 04:40:33 +00:00
|
|
|
logger.info(
|
|
|
|
`recovery missmatch uri for (username=${username}, host=${host}) from ${user.uri} to ${self.href}`,
|
|
|
|
);
|
2018-03-31 10:55:00 +00:00
|
|
|
|
2018-10-30 17:16:13 +00:00
|
|
|
// validate uri
|
|
|
|
const uri = new URL(self.href);
|
2019-04-09 15:59:41 +00:00
|
|
|
if (uri.hostname !== host) {
|
2023-01-13 04:40:33 +00:00
|
|
|
throw new Error("Invalid uri");
|
2018-10-30 17:16:13 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
await Users.update(
|
|
|
|
{
|
|
|
|
usernameLower,
|
|
|
|
host: host,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
uri: self.href,
|
|
|
|
},
|
|
|
|
);
|
2018-10-30 17:16:13 +00:00
|
|
|
} else {
|
2019-02-02 19:04:57 +00:00
|
|
|
logger.info(`uri is fine: ${acctLower}`);
|
2018-03-31 10:55:00 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 17:16:13 +00:00
|
|
|
await updatePerson(self.href);
|
|
|
|
|
2019-02-02 19:04:57 +00:00
|
|
|
logger.info(`return resynced remote user: ${acctLower}`);
|
2023-01-13 04:40:33 +00:00
|
|
|
return await Users.findOneBy({ uri: self.href }).then((u) => {
|
2019-04-12 16:43:22 +00:00
|
|
|
if (u == null) {
|
2023-01-13 04:40:33 +00:00
|
|
|
throw new Error("user not found");
|
2019-04-12 16:43:22 +00:00
|
|
|
} else {
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
});
|
2019-02-02 19:43:43 +00:00
|
|
|
}
|
2018-03-31 10:55:00 +00:00
|
|
|
|
2019-02-02 19:43:43 +00:00
|
|
|
logger.info(`return existing remote user: ${acctLower}`);
|
2018-03-31 10:55:00 +00:00
|
|
|
return user;
|
2019-04-07 14:06:07 +00:00
|
|
|
}
|
2018-10-30 17:16:13 +00:00
|
|
|
|
|
|
|
async function resolveSelf(acctLower: string) {
|
2019-02-02 19:43:43 +00:00
|
|
|
logger.info(`WebFinger for ${chalk.yellow(acctLower)}`);
|
2023-01-13 04:40:33 +00:00
|
|
|
const finger = await webFinger(acctLower).catch((e) => {
|
|
|
|
logger.error(
|
|
|
|
`Failed to WebFinger for ${chalk.yellow(acctLower)}: ${
|
|
|
|
e.statusCode || e.message
|
|
|
|
}`,
|
|
|
|
);
|
|
|
|
throw new Error(
|
|
|
|
`Failed to WebFinger for ${acctLower}: ${e.statusCode || e.message}`,
|
|
|
|
);
|
2019-02-04 00:02:23 +00:00
|
|
|
});
|
2023-01-13 04:40:33 +00:00
|
|
|
const self = finger.links.find(
|
|
|
|
(link) => link.rel != null && link.rel.toLowerCase() === "self",
|
|
|
|
);
|
2018-10-30 17:16:13 +00:00
|
|
|
if (!self) {
|
2023-01-13 04:40:33 +00:00
|
|
|
logger.error(
|
|
|
|
`Failed to WebFinger for ${chalk.yellow(acctLower)}: self link not found`,
|
|
|
|
);
|
|
|
|
throw new Error("self link not found");
|
2018-10-30 17:16:13 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|