2018-03-31 10:55:00 +00:00
|
|
|
import { toUnicode, toASCII } from 'punycode';
|
|
|
|
import webFinger from './webfinger';
|
2018-04-08 06:25:17 +00:00
|
|
|
import config from '../config';
|
2018-10-30 17:16:13 +00:00
|
|
|
import { createPerson, updatePerson } from './activitypub/models/person';
|
|
|
|
import { URL } from 'url';
|
2019-02-02 19:04:57 +00:00
|
|
|
import { remoteLogger } from './logger';
|
2019-02-02 19:43:43 +00:00
|
|
|
import chalk from 'chalk';
|
2019-04-07 12:50:36 +00:00
|
|
|
import { User, IRemoteUser } from '../models/entities/user';
|
|
|
|
import { Users } from '../models';
|
2018-03-31 10:55:00 +00:00
|
|
|
|
2019-02-02 19:04:57 +00:00
|
|
|
const logger = remoteLogger.createSubLogger('resolve-user');
|
2018-10-30 17:16:13 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
export default async (username: string, _host: string, option?: any, resync = false): Promise<User> => {
|
2018-03-31 10:55:00 +00:00
|
|
|
const usernameLower = username.toLowerCase();
|
2018-05-06 19:26:45 +00:00
|
|
|
|
|
|
|
if (_host == null) {
|
2019-02-02 19:04:57 +00:00
|
|
|
logger.info(`return local user: ${usernameLower}`);
|
2019-04-07 12:50:36 +00:00
|
|
|
return await Users.findOne({ usernameLower, host: null });
|
2018-05-06 19:26:45 +00:00
|
|
|
}
|
|
|
|
|
2018-11-11 04:11:16 +00:00
|
|
|
const configHostAscii = toASCII(config.host).toLowerCase();
|
|
|
|
const configHost = toUnicode(configHostAscii);
|
|
|
|
|
2018-04-14 05:39:07 +00:00
|
|
|
const hostAscii = toASCII(_host).toLowerCase();
|
|
|
|
const host = toUnicode(hostAscii);
|
2018-03-31 10:55:00 +00:00
|
|
|
|
2018-11-11 04:11:16 +00:00
|
|
|
if (configHost == host) {
|
2019-02-02 19:04:57 +00:00
|
|
|
logger.info(`return local user: ${usernameLower}`);
|
2019-04-07 12:50:36 +00:00
|
|
|
return await Users.findOne({ usernameLower, host: null });
|
2018-04-08 06:25:17 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
const user = await Users.findOne({ usernameLower, host }, option);
|
2018-10-30 17:16:13 +00:00
|
|
|
|
|
|
|
const acctLower = `${usernameLower}@${hostAscii}`;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resync) {
|
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);
|
|
|
|
|
|
|
|
if ((user as IRemoteUser).uri !== self.href) {
|
|
|
|
// 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}`);
|
|
|
|
logger.info(`recovery missmatch uri for (username=${username}, host=${host}) from ${(user as IRemoteUser).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);
|
|
|
|
if (uri.hostname !== hostAscii) {
|
|
|
|
throw new Error(`Invalied uri`);
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
await Users.update({
|
2018-10-30 17:16:13 +00:00
|
|
|
usernameLower,
|
|
|
|
host: host
|
2019-02-02 04:57:26 +00:00
|
|
|
}, {
|
2019-04-07 12:50:36 +00:00
|
|
|
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}`);
|
2019-04-07 12:50:36 +00:00
|
|
|
return await Users.findOne({ uri: self.href });
|
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;
|
|
|
|
};
|
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)}`);
|
2019-02-04 00:02:23 +00:00
|
|
|
const finger = await webFinger(acctLower).catch(e => {
|
|
|
|
logger.error(`Failed to WebFinger for ${chalk.yellow(acctLower)}: ${e.message} (${e.status})`);
|
|
|
|
throw e;
|
|
|
|
});
|
2018-10-30 17:16:13 +00:00
|
|
|
const self = finger.links.find(link => link.rel && link.rel.toLowerCase() === 'self');
|
|
|
|
if (!self) {
|
2019-02-04 00:06:13 +00:00
|
|
|
logger.error(`Failed to WebFinger for ${chalk.yellow(acctLower)}: self link not found`);
|
2018-10-30 17:16:13 +00:00
|
|
|
throw new Error('self link not found');
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|