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