fix calckey-js | fix migration url

This commit is contained in:
cutestnekoaqua 2022-12-12 19:35:33 +01:00
parent 7a8ab8b381
commit db7cc867c5
3 changed files with 33 additions and 15 deletions

View file

@ -6,6 +6,8 @@ import { publishToFollowers } from '@/services/i/update.js';
import { publishMainStream } from '@/services/stream.js'; import { publishMainStream } from '@/services/stream.js';
import { DAY } from '@/const.js'; import { DAY } from '@/const.js';
import { apiLogger } from '../../logger.js'; import { apiLogger } from '../../logger.js';
import { UserProfiles } from '@/models/index.js';
import config from '@/config/index.js';
import define from '../../define.js'; import define from '../../define.js';
import { ApiError } from '../../error.js'; import { ApiError } from '../../error.js';
@ -58,14 +60,19 @@ export default define(meta, paramDef, async (ps, user) => {
throw new ApiError(meta.errors.noSuchUser); throw new ApiError(meta.errors.noSuchUser);
}); });
const profileTo = await UserProfiles.findOneByOrFail({ userId: knownAs.id });
let toUrl: string | null = profileTo.url;
if(!toUrl) {
toUrl = `${config.url}/@${knownAs.username}`;
}
const updates = {} as Partial<User>; const updates = {} as Partial<User>;
// FIXME: .uri is local uri, not remote uri! if (!toUrl) toUrl = '';
if (!knownAs.uri) knownAs.uri = '';
if (updates.alsoKnownAs == null || updates.alsoKnownAs.length === 0) { if (updates.alsoKnownAs == null || updates.alsoKnownAs.length === 0) {
updates.alsoKnownAs = [knownAs.uri]; updates.alsoKnownAs = [toUrl];
} else { } else {
updates.alsoKnownAs.push(knownAs.uri); updates.alsoKnownAs.push(toUrl);
} }
await Users.update(user.id, updates); await Users.update(user.id, updates);

View file

@ -2,12 +2,13 @@ import type { User } from '@/models/entities/user.js';
import { resolveUser } from '@/remote/resolve-user.js'; import { resolveUser } from '@/remote/resolve-user.js';
import { DAY } from '@/const.js'; import { DAY } from '@/const.js';
import DeliverManager from '@/remote/activitypub/deliver-manager.js'; import DeliverManager from '@/remote/activitypub/deliver-manager.js';
import { deliver } from '@/queue/index.js';
import { renderActivity } from '@/remote/activitypub/renderer/index.js'; import { renderActivity } from '@/remote/activitypub/renderer/index.js';
import { genId } from '@/misc/gen-id.js'; import { genId } from '@/misc/gen-id.js';
import define from '../../define.js'; import define from '../../define.js';
import { ApiError } from '../../error.js'; import { ApiError } from '../../error.js';
import { apiLogger } from '../../logger.js'; import { apiLogger } from '../../logger.js';
import { UserProfiles } from '@/models/index.js';
import config from '@/config/index.js';
export const meta = { export const meta = {
tags: ['users'], tags: ['users'],
@ -52,17 +53,16 @@ export const paramDef = {
required: ['moveToAccount'], required: ['moveToAccount'],
} as const; } as const;
function moveActivity(to: User, from: User) { function moveActivity(toUrl: string, fromUrl: string) {
const activity = { const activity = {
id: genId(), id: genId(),
actor: from, actor: fromUrl,
type: 'Move', type: 'Move',
object: from, object: fromUrl,
target: to, target: toUrl,
} as any; } as any;
const content = renderActivity(activity); return renderActivity(activity);
deliver(to, content, from.inbox);
} }
// eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export
@ -80,16 +80,27 @@ export default define(meta, paramDef, async (ps, user) => {
apiLogger.warn(`failed to resolve remote user: ${e}`); apiLogger.warn(`failed to resolve remote user: ${e}`);
throw new ApiError(meta.errors.noSuchMoveTarget); throw new ApiError(meta.errors.noSuchMoveTarget);
}); });
const profileFrom = await UserProfiles.findOneByOrFail({ userId: user.id });
let fromUrl: string | null = profileFrom.url;
if(!fromUrl) {
fromUrl = `${config.url}/@${user.username}`;
}
const profileTo = await UserProfiles.findOneByOrFail({ userId: moveTo.id });
let toUrl: string | null = profileTo.url;
if(!toUrl) {
toUrl = `${config.url}/@${moveTo.username}`;
}
let allowed = false; let allowed = false;
moveTo.alsoKnownAs?.forEach(element => { moveTo.alsoKnownAs?.forEach(element => {
if (user.uri?.includes(element)) allowed = true; if (fromUrl!.includes(element)) allowed = true;
}); });
if (!allowed || !moveTo.uri || !user.uri) throw new ApiError(meta.errors.remoteAccountForbids); if (!allowed || !toUrl || !fromUrl) throw new ApiError(meta.errors.remoteAccountForbids);
const moveAct = moveActivity(moveTo, user); const moveAct = moveActivity(toUrl, fromUrl);
const dm = new DeliverManager(user, moveAct); const dm = new DeliverManager(user, moveAct);
dm.addFollowersRecipe(); dm.addFollowersRecipe();
dm.execute(); dm.execute();

View file

@ -1,5 +1,5 @@
import * as misskey from 'calckey-js'; import * as misskey from 'calckey-js';
import * as Acct from 'misskey-js/built/acct'; import * as Acct from 'calckey-js/built/acct';
export const acct = (user: misskey.Acct) => { export const acct = (user: misskey.Acct) => {
return Acct.toString(user); return Acct.toString(user);