Fix: can't recognize rebirthed instance user (#3046)

* resync uri from WebFinger

* trigger resync on user page

* allways update on resync

* Revert "trigger resync on user page"

This reverts commit 8ff139fb49.

* background resync
This commit is contained in:
MeiMei 2018-10-31 02:16:13 +09:00 committed by syuilo
parent ba5ed188a1
commit 441ab2b5f8
3 changed files with 98 additions and 13 deletions

View File

@ -1,13 +1,18 @@
import { toUnicode, toASCII } from 'punycode';
import User, { IUser } from '../models/user';
import User, { IUser, IRemoteUser } from '../models/user';
import webFinger from './webfinger';
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();
if (_host == null) {
log(`return local user: ${usernameLower}`);
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);
if (config.host == host) {
log(`return local user: ${usernameLower}`);
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}`;
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
}
});
} else {
log(`uri is fine: ${acctLower}`);
}
await updatePerson(self.href);
log(`return resynced remote user: ${acctLower}`);
return await User.findOne({ uri: self.href });
}
log(`return existing remote user: ${acctLower}`);
return user;
};
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');
}
user = await createPerson(self.href);
return self;
}
return user;
};

View File

@ -1,5 +1,5 @@
import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import User, { pack, ILocalUser } from '../../../../models/user';
import User, { pack, ILocalUser, isRemoteUser } from '../../../../models/user';
import resolveRemoteUser from '../../../../remote/resolve-user';
const cursorOption = { fields: { data: false } };
@ -61,5 +61,11 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) =>
res(await pack(user, me, {
detail: true
}));
if (isRemoteUser(user)) {
if (user.updatedAt == null || Date.now() - user.updatedAt.getTime() > 1000 * 60 * 60 * 24) {
resolveRemoteUser(username, host, { }, true);
}
}
}
});

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);
});