resync uri from WebFinger

This commit is contained in:
mei23 2018-10-28 22:00:36 +09:00
parent 22d0d11895
commit e9fc7caeb3
3 changed files with 96 additions and 13 deletions

View file

@ -1,13 +1,18 @@
import { toUnicode, toASCII } from 'punycode'; import { toUnicode, toASCII } from 'punycode';
import User, { IUser } from '../models/user'; import User, { IUser, IRemoteUser } from '../models/user';
import webFinger from './webfinger'; import webFinger from './webfinger';
import config from '../config'; import config from '../config';
import { createPerson } from './activitypub/models/person'; import { createPerson, updatePerson } from './activitypub/models/person';
import { URL } from 'url';
import * as debug from 'debug';
export default async (username: string, _host: string, option?: any): Promise<IUser> => { const log = debug('misskey:remote:resolve-user');
export default async (username: string, _host: string, option?: any, resync?: boolean): Promise<IUser> => {
const usernameLower = username.toLowerCase(); const usernameLower = username.toLowerCase();
if (_host == null) { if (_host == null) {
log(`return local user: ${usernameLower}`);
return await User.findOne({ usernameLower, host: null }); return await User.findOne({ usernameLower, host: null });
} }
@ -15,22 +20,64 @@ export default async (username: string, _host: string, option?: any): Promise<IU
const host = toUnicode(hostAscii); const host = toUnicode(hostAscii);
if (config.host == host) { if (config.host == host) {
log(`return local user: ${usernameLower}`);
return await User.findOne({ usernameLower, host: null }); return await User.findOne({ usernameLower, host: null });
} }
let user = await User.findOne({ usernameLower, host }, option); const user = await User.findOne({ usernameLower, host }, option);
if (user === null) {
const acctLower = `${usernameLower}@${hostAscii}`; const acctLower = `${usernameLower}@${hostAscii}`;
if (user === null) {
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}`);
// 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
}
});
await updatePerson(self.href);
log(`return resynced remote user: ${acctLower}`);
return await User.findOne({ uri: self.href });
} else {
log(`uri is fine: ${acctLower}`);
}
}
log(`return existing remote user: ${acctLower}`);
return user;
};
async function resolveSelf(acctLower: string) {
log(`WebFinger for ${acctLower}`);
const finger = await webFinger(acctLower); const finger = await webFinger(acctLower);
const self = finger.links.find(link => link.rel && link.rel.toLowerCase() === 'self'); const self = finger.links.find(link => link.rel && link.rel.toLowerCase() === 'self');
if (!self) { if (!self) {
throw new Error('self link not found'); throw new Error('self link not found');
} }
return self;
user = await createPerson(self.href);
} }
return user;
};

View file

@ -26,6 +26,10 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) =>
const [host, hostErr] = $.str.optional.nullable.get(params.host); const [host, hostErr] = $.str.optional.nullable.get(params.host);
if (hostErr) return rej('invalid host param'); if (hostErr) return rej('invalid host param');
// Get 'resync' parameter
const [resync = false, resyncErr] = $.bool.optional.get(params.resync);
if (resyncErr) return rej('invalid resync param');
if (userIds) { if (userIds) {
const users = await User.find({ const users = await User.find({
_id: { _id: {
@ -40,7 +44,7 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) =>
// Lookup user // Lookup user
if (typeof host === 'string') { if (typeof host === 'string') {
try { try {
user = await resolveRemoteUser(username, host, cursorOption); user = await resolveRemoteUser(username, host, cursorOption, resync);
} catch (e) { } catch (e) {
console.warn(`failed to resolve remote user: ${e}`); console.warn(`failed to resolve remote user: ${e}`);
return rej('failed to resolve remote user'); return rej('failed to resolve remote user');

View file

@ -0,0 +1,32 @@
import parseAcct from "../misc/acct/parse";
import resolveUser from '../remote/resolve-user';
import * as debug from 'debug';
debug.enable('*');
async function main(acct: string): Promise<any> {
const { username, host } = parseAcct(acct);
await resolveUser(username, host, {}, true);
}
// get args
const args = process.argv.slice(2);
let acct = args[0];
// normalize args
acct = acct.replace(/^@/, '');
// check args
if (!acct.match(/^\w+@\w/)) {
throw `Invalied acct format. Valied format are user@host`;
}
console.log(`resync ${acct}`);
main(acct).then(() => {
console.log('success');
process.exit(0);
}).catch(e => {
console.warn(e);
process.exit(1);
});