2022-02-27 03:07:39 +01:00
|
|
|
import { URL } from 'node:url';
|
|
|
|
import webFinger from './webfinger.js';
|
|
|
|
import config from '@/config/index.js';
|
|
|
|
import { createPerson, updatePerson } from './activitypub/models/person.js';
|
|
|
|
import { remoteLogger } from './logger.js';
|
|
|
|
import chalk from 'chalk';
|
|
|
|
import { User, IRemoteUser } from '@/models/entities/user.js';
|
|
|
|
import { Users } from '@/models/index.js';
|
|
|
|
import { toPuny } from '@/misc/convert-host.js';
|
2022-03-26 07:34:00 +01:00
|
|
|
import { IsNull } from 'typeorm';
|
2018-03-31 12:55:00 +02:00
|
|
|
|
2019-02-02 20:04:57 +01:00
|
|
|
const logger = remoteLogger.createSubLogger('resolve-user');
|
2018-10-30 18:16:13 +01:00
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
export async function resolveUser(username: string, host: string | null): Promise<User> {
|
2018-03-31 12:55:00 +02:00
|
|
|
const usernameLower = username.toLowerCase();
|
2018-05-06 21:26:45 +02:00
|
|
|
|
2019-04-09 17:59:41 +02:00
|
|
|
if (host == null) {
|
2019-02-02 20:04:57 +01:00
|
|
|
logger.info(`return local user: ${usernameLower}`);
|
2022-03-26 07:34:00 +01:00
|
|
|
return await Users.findOneBy({ usernameLower, host: IsNull() }).then(u => {
|
2019-04-12 18:43:22 +02:00
|
|
|
if (u == null) {
|
2019-04-13 21:17:24 +02:00
|
|
|
throw new Error('user not found');
|
2019-04-12 18:43:22 +02:00
|
|
|
} else {
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
});
|
2018-05-06 21:26:45 +02:00
|
|
|
}
|
|
|
|
|
2019-04-12 18:43:22 +02:00
|
|
|
host = toPuny(host);
|
|
|
|
|
2022-02-28 17:24:50 +01:00
|
|
|
if (config.host === host) {
|
2019-02-02 20:04:57 +01:00
|
|
|
logger.info(`return local user: ${usernameLower}`);
|
2022-03-26 07:34:00 +01:00
|
|
|
return await Users.findOneBy({ usernameLower, host: IsNull() }).then(u => {
|
2019-04-12 18:43:22 +02:00
|
|
|
if (u == null) {
|
2019-04-13 21:17:24 +02:00
|
|
|
throw new Error('user not found');
|
2019-04-12 18:43:22 +02:00
|
|
|
} else {
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
});
|
2018-04-08 08:25:17 +02:00
|
|
|
}
|
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
const user = await Users.findOneBy({ usernameLower, host }) as IRemoteUser | null;
|
2018-10-30 18:16:13 +01:00
|
|
|
|
2019-04-09 17:59:41 +02:00
|
|
|
const acctLower = `${usernameLower}@${host}`;
|
2018-03-31 12:55:00 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (user == null) {
|
2018-10-30 18:16:13 +01:00
|
|
|
const self = await resolveSelf(acctLower);
|
|
|
|
|
2019-02-02 20:43:43 +01:00
|
|
|
logger.succ(`return new remote user: ${chalk.magenta(acctLower)}`);
|
2018-10-30 18:16:13 +01:00
|
|
|
return await createPerson(self.href);
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
// ユーザー情報が古い場合は、WebFilgerからやりなおして返す
|
|
|
|
if (user.lastFetchedAt == null || Date.now() - user.lastFetchedAt.getTime() > 1000 * 60 * 60 * 24) {
|
2019-04-21 16:57:44 +02:00
|
|
|
// 繋がらないインスタンスに何回も試行するのを防ぐ, 後続の同様処理の連続試行を防ぐ ため 試行前にも更新する
|
|
|
|
await Users.update(user.id, {
|
|
|
|
lastFetchedAt: new Date(),
|
|
|
|
});
|
|
|
|
|
2019-02-02 20:04:57 +01:00
|
|
|
logger.info(`try resync: ${acctLower}`);
|
2018-10-30 18:16:13 +01:00
|
|
|
const self = await resolveSelf(acctLower);
|
|
|
|
|
2019-04-21 16:57:44 +02:00
|
|
|
if (user.uri !== self.href) {
|
2018-10-30 18:16:13 +01:00
|
|
|
// if uri mismatch, Fix (user@host <=> AP's Person id(IRemoteUser.uri)) mapping.
|
2019-02-02 20:04:57 +01:00
|
|
|
logger.info(`uri missmatch: ${acctLower}`);
|
2019-04-21 16:57:44 +02:00
|
|
|
logger.info(`recovery missmatch uri for (username=${username}, host=${host}) from ${user.uri} to ${self.href}`);
|
2018-03-31 12:55:00 +02:00
|
|
|
|
2018-10-30 18:16:13 +01:00
|
|
|
// validate uri
|
|
|
|
const uri = new URL(self.href);
|
2019-04-09 17:59:41 +02:00
|
|
|
if (uri.hostname !== host) {
|
2019-09-15 16:27:33 +02:00
|
|
|
throw new Error(`Invalid uri`);
|
2018-10-30 18:16:13 +01:00
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
await Users.update({
|
2018-10-30 18:16:13 +01:00
|
|
|
usernameLower,
|
2021-12-09 15:58:30 +01:00
|
|
|
host: host,
|
2019-02-02 05:57:26 +01:00
|
|
|
}, {
|
2021-12-09 15:58:30 +01:00
|
|
|
uri: self.href,
|
2018-10-30 18:16:13 +01:00
|
|
|
});
|
|
|
|
} else {
|
2019-02-02 20:04:57 +01:00
|
|
|
logger.info(`uri is fine: ${acctLower}`);
|
2018-03-31 12:55:00 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 18:16:13 +01:00
|
|
|
await updatePerson(self.href);
|
|
|
|
|
2019-02-02 20:04:57 +01:00
|
|
|
logger.info(`return resynced remote user: ${acctLower}`);
|
2022-03-26 07:34:00 +01:00
|
|
|
return await Users.findOneBy({ uri: self.href }).then(u => {
|
2019-04-12 18:43:22 +02:00
|
|
|
if (u == null) {
|
2019-04-13 21:17:24 +02:00
|
|
|
throw new Error('user not found');
|
2019-04-12 18:43:22 +02:00
|
|
|
} else {
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
});
|
2019-02-02 20:43:43 +01:00
|
|
|
}
|
2018-03-31 12:55:00 +02:00
|
|
|
|
2019-02-02 20:43:43 +01:00
|
|
|
logger.info(`return existing remote user: ${acctLower}`);
|
2018-03-31 12:55:00 +02:00
|
|
|
return user;
|
2019-04-07 16:06:07 +02:00
|
|
|
}
|
2018-10-30 18:16:13 +01:00
|
|
|
|
|
|
|
async function resolveSelf(acctLower: string) {
|
2019-02-02 20:43:43 +01:00
|
|
|
logger.info(`WebFinger for ${chalk.yellow(acctLower)}`);
|
2019-02-04 01:02:23 +01:00
|
|
|
const finger = await webFinger(acctLower).catch(e => {
|
2019-04-10 08:07:21 +02:00
|
|
|
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 01:02:23 +01:00
|
|
|
});
|
2019-04-12 18:43:22 +02:00
|
|
|
const self = finger.links.find(link => link.rel != null && link.rel.toLowerCase() === 'self');
|
2018-10-30 18:16:13 +01:00
|
|
|
if (!self) {
|
2019-02-04 01:06:13 +01:00
|
|
|
logger.error(`Failed to WebFinger for ${chalk.yellow(acctLower)}: self link not found`);
|
2018-10-30 18:16:13 +01:00
|
|
|
throw new Error('self link not found');
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|