2018-03-31 03:55:00 -07:00
|
|
|
import { toUnicode, toASCII } from 'punycode';
|
2018-10-30 10:16:13 -07:00
|
|
|
import User, { IUser, IRemoteUser } from '../models/user';
|
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';
|
|
|
|
import * as debug from 'debug';
|
2018-03-31 03:55:00 -07:00
|
|
|
|
2018-10-30 10:16:13 -07:00
|
|
|
const log = debug('misskey:remote:resolve-user');
|
|
|
|
|
|
|
|
export default async (username: string, _host: string, option?: any, resync?: boolean): Promise<IUser> => {
|
2018-03-31 03:55:00 -07:00
|
|
|
const usernameLower = username.toLowerCase();
|
2018-05-06 12:26:45 -07:00
|
|
|
|
|
|
|
if (_host == null) {
|
2018-10-30 10:16:13 -07:00
|
|
|
log(`return local user: ${usernameLower}`);
|
2018-10-27 15:49:58 -07:00
|
|
|
return await User.findOne({ usernameLower, host: null });
|
2018-05-06 12:26:45 -07:00
|
|
|
}
|
|
|
|
|
2018-11-10 20:11:16 -08:00
|
|
|
const configHostAscii = toASCII(config.host).toLowerCase();
|
|
|
|
const configHost = toUnicode(configHostAscii);
|
|
|
|
|
2018-04-13 22:39:07 -07:00
|
|
|
const hostAscii = toASCII(_host).toLowerCase();
|
|
|
|
const host = toUnicode(hostAscii);
|
2018-03-31 03:55:00 -07:00
|
|
|
|
2018-11-10 20:11:16 -08:00
|
|
|
if (configHost == host) {
|
2018-10-30 10:16:13 -07:00
|
|
|
log(`return local user: ${usernameLower}`);
|
2018-08-22 19:58:44 -07:00
|
|
|
return await User.findOne({ usernameLower, host: null });
|
2018-04-07 23:25:17 -07:00
|
|
|
}
|
|
|
|
|
2018-10-30 10:16:13 -07:00
|
|
|
const user = await User.findOne({ usernameLower, host }, option);
|
|
|
|
|
|
|
|
const acctLower = `${usernameLower}@${hostAscii}`;
|
2018-03-31 03:55:00 -07:00
|
|
|
|
|
|
|
if (user === null) {
|
2018-10-30 10:16:13 -07:00
|
|
|
const self = await resolveSelf(acctLower);
|
|
|
|
|
|
|
|
log(`return new remote user: ${acctLower}`);
|
|
|
|
return await createPerson(self.href);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resync) {
|
|
|
|
log(`try resync: ${acctLower}`);
|
|
|
|
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.
|
|
|
|
log(`uri missmatch: ${acctLower}`);
|
|
|
|
console.log(`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);
|
|
|
|
if (uri.hostname !== hostAscii) {
|
|
|
|
throw new Error(`Invalied uri`);
|
|
|
|
}
|
|
|
|
|
|
|
|
await User.update({
|
|
|
|
usernameLower,
|
|
|
|
host: host
|
|
|
|
}, {
|
|
|
|
$set: {
|
|
|
|
uri: self.href
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
log(`uri is fine: ${acctLower}`);
|
2018-03-31 03:55:00 -07:00
|
|
|
}
|
|
|
|
|
2018-10-30 10:16:13 -07:00
|
|
|
await updatePerson(self.href);
|
|
|
|
|
|
|
|
log(`return resynced remote user: ${acctLower}`);
|
|
|
|
return await User.findOne({ uri: self.href });
|
|
|
|
}
|
2018-03-31 03:55:00 -07:00
|
|
|
|
2018-10-30 10:16:13 -07:00
|
|
|
log(`return existing remote user: ${acctLower}`);
|
2018-03-31 03:55:00 -07:00
|
|
|
return user;
|
|
|
|
};
|
2018-10-30 10:16:13 -07:00
|
|
|
|
|
|
|
async function resolveSelf(acctLower: string) {
|
|
|
|
log(`WebFinger for ${acctLower}`);
|
|
|
|
const finger = await webFinger(acctLower);
|
|
|
|
const self = finger.links.find(link => link.rel && link.rel.toLowerCase() === 'self');
|
|
|
|
if (!self) {
|
|
|
|
throw new Error('self link not found');
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|