2018-03-31 03:55:00 -07:00
|
|
|
import webFinger from './webfinger';
|
2018-04-07 23:25:17 -07:00
|
|
|
import config from '../config';
|
2018-10-30 10:16:13 -07:00
|
|
|
import { createPerson, updatePerson } from './activitypub/models/person';
|
|
|
|
import { URL } from 'url';
|
2019-02-02 11:04:57 -08:00
|
|
|
import { remoteLogger } from './logger';
|
2019-02-02 11:43:43 -08:00
|
|
|
import chalk from 'chalk';
|
2019-04-07 05:50:36 -07:00
|
|
|
import { User, IRemoteUser } from '../models/entities/user';
|
|
|
|
import { Users } from '../models';
|
2019-04-09 08:59:41 -07:00
|
|
|
import { toPuny } from '../misc/convert-host';
|
2018-03-31 03:55:00 -07:00
|
|
|
|
2019-02-02 11:04:57 -08:00
|
|
|
const logger = remoteLogger.createSubLogger('resolve-user');
|
2018-10-30 10:16:13 -07:00
|
|
|
|
2019-04-12 09:43:22 -07:00
|
|
|
export async function resolveUser(username: string, host: string | null, option?: any, resync = false): Promise<User> {
|
2018-03-31 03:55:00 -07:00
|
|
|
const usernameLower = username.toLowerCase();
|
2018-05-06 12:26:45 -07:00
|
|
|
|
2019-04-09 08:59:41 -07:00
|
|
|
if (host == null) {
|
2019-02-02 11:04:57 -08:00
|
|
|
logger.info(`return local user: ${usernameLower}`);
|
2019-04-12 09:43:22 -07:00
|
|
|
return await Users.findOne({ usernameLower, host: null }).then(u => {
|
|
|
|
if (u == null) {
|
2019-04-13 12:17:24 -07:00
|
|
|
throw new Error('user not found');
|
2019-04-12 09:43:22 -07:00
|
|
|
} else {
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
});
|
2018-05-06 12:26:45 -07:00
|
|
|
}
|
|
|
|
|
2019-04-12 09:43:22 -07:00
|
|
|
host = toPuny(host);
|
|
|
|
|
2019-04-09 08:59:41 -07:00
|
|
|
if (config.host == host) {
|
2019-02-02 11:04:57 -08:00
|
|
|
logger.info(`return local user: ${usernameLower}`);
|
2019-04-12 09:43:22 -07:00
|
|
|
return await Users.findOne({ usernameLower, host: null }).then(u => {
|
|
|
|
if (u == null) {
|
2019-04-13 12:17:24 -07:00
|
|
|
throw new Error('user not found');
|
2019-04-12 09:43:22 -07:00
|
|
|
} else {
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
});
|
2018-04-07 23:25:17 -07:00
|
|
|
}
|
|
|
|
|
2019-04-07 05:50:36 -07:00
|
|
|
const user = await Users.findOne({ usernameLower, host }, option);
|
2018-10-30 10:16:13 -07:00
|
|
|
|
2019-04-09 08:59:41 -07:00
|
|
|
const acctLower = `${usernameLower}@${host}`;
|
2018-03-31 03:55:00 -07:00
|
|
|
|
2019-04-07 05:50:36 -07:00
|
|
|
if (user == null) {
|
2018-10-30 10:16:13 -07:00
|
|
|
const self = await resolveSelf(acctLower);
|
|
|
|
|
2019-02-02 11:43:43 -08:00
|
|
|
logger.succ(`return new remote user: ${chalk.magenta(acctLower)}`);
|
2018-10-30 10:16:13 -07:00
|
|
|
return await createPerson(self.href);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resync) {
|
2019-02-02 11:04:57 -08:00
|
|
|
logger.info(`try resync: ${acctLower}`);
|
2018-10-30 10:16:13 -07:00
|
|
|
const self = await resolveSelf(acctLower);
|
|
|
|
|
|
|
|
if ((user as IRemoteUser).uri !== self.href) {
|
|
|
|
// if uri mismatch, Fix (user@host <=> AP's Person id(IRemoteUser.uri)) mapping.
|
2019-02-02 11:04:57 -08:00
|
|
|
logger.info(`uri missmatch: ${acctLower}`);
|
|
|
|
logger.info(`recovery missmatch uri for (username=${username}, host=${host}) from ${(user as IRemoteUser).uri} to ${self.href}`);
|
2018-03-31 03:55:00 -07:00
|
|
|
|
2018-10-30 10:16:13 -07:00
|
|
|
// validate uri
|
|
|
|
const uri = new URL(self.href);
|
2019-04-09 08:59:41 -07:00
|
|
|
if (uri.hostname !== host) {
|
2018-10-30 10:16:13 -07:00
|
|
|
throw new Error(`Invalied uri`);
|
|
|
|
}
|
|
|
|
|
2019-04-07 05:50:36 -07:00
|
|
|
await Users.update({
|
2018-10-30 10:16:13 -07:00
|
|
|
usernameLower,
|
|
|
|
host: host
|
2019-02-01 20:57:26 -08:00
|
|
|
}, {
|
2019-04-07 05:50:36 -07:00
|
|
|
uri: self.href
|
2018-10-30 10:16:13 -07:00
|
|
|
});
|
|
|
|
} else {
|
2019-02-02 11:04:57 -08:00
|
|
|
logger.info(`uri is fine: ${acctLower}`);
|
2018-03-31 03:55:00 -07:00
|
|
|
}
|
|
|
|
|
2018-10-30 10:16:13 -07:00
|
|
|
await updatePerson(self.href);
|
|
|
|
|
2019-02-02 11:04:57 -08:00
|
|
|
logger.info(`return resynced remote user: ${acctLower}`);
|
2019-04-12 09:43:22 -07:00
|
|
|
return await Users.findOne({ uri: self.href }).then(u => {
|
|
|
|
if (u == null) {
|
2019-04-13 12:17:24 -07:00
|
|
|
throw new Error('user not found');
|
2019-04-12 09:43:22 -07:00
|
|
|
} else {
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
});
|
2019-02-02 11:43:43 -08:00
|
|
|
}
|
2018-03-31 03:55:00 -07:00
|
|
|
|
2019-02-02 11:43:43 -08:00
|
|
|
logger.info(`return existing remote user: ${acctLower}`);
|
2018-03-31 03:55:00 -07:00
|
|
|
return user;
|
2019-04-07 07:06:07 -07:00
|
|
|
}
|
2018-10-30 10:16:13 -07:00
|
|
|
|
|
|
|
async function resolveSelf(acctLower: string) {
|
2019-02-02 11:43:43 -08:00
|
|
|
logger.info(`WebFinger for ${chalk.yellow(acctLower)}`);
|
2019-02-03 16:02:23 -08:00
|
|
|
const finger = await webFinger(acctLower).catch(e => {
|
2019-04-09 23:07:21 -07: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-03 16:02:23 -08:00
|
|
|
});
|
2019-04-12 09:43:22 -07:00
|
|
|
const self = finger.links.find(link => link.rel != null && link.rel.toLowerCase() === 'self');
|
2018-10-30 10:16:13 -07:00
|
|
|
if (!self) {
|
2019-02-03 16:06:13 -08:00
|
|
|
logger.error(`Failed to WebFinger for ${chalk.yellow(acctLower)}: self link not found`);
|
2018-10-30 10:16:13 -07:00
|
|
|
throw new Error('self link not found');
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|