From 5e6091989d29f55002ec136bd8f8d31fba852eeb Mon Sep 17 00:00:00 2001 From: cutestnekoaqua Date: Mon, 12 Dec 2022 19:35:33 +0100 Subject: [PATCH] fix calckey-js | fix migration url --- .../src/server/api/endpoints/i/known-as.ts | 15 ++++++--- .../src/server/api/endpoints/i/move.ts | 31 +++++++++++++------ packages/sw/src/filters/user.ts | 2 +- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/i/known-as.ts b/packages/backend/src/server/api/endpoints/i/known-as.ts index 2a27e36fee..9038bca378 100644 --- a/packages/backend/src/server/api/endpoints/i/known-as.ts +++ b/packages/backend/src/server/api/endpoints/i/known-as.ts @@ -6,6 +6,8 @@ import { publishToFollowers } from '@/services/i/update.js'; import { publishMainStream } from '@/services/stream.js'; import { DAY } from '@/const.js'; import { apiLogger } from '../../logger.js'; +import { UserProfiles } from '@/models/index.js'; +import config from '@/config/index.js'; import define from '../../define.js'; import { ApiError } from '../../error.js'; @@ -58,14 +60,19 @@ export default define(meta, paramDef, async (ps, user) => { 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; - // FIXME: .uri is local uri, not remote uri! - if (!knownAs.uri) knownAs.uri = ''; + if (!toUrl) toUrl = ''; if (updates.alsoKnownAs == null || updates.alsoKnownAs.length === 0) { - updates.alsoKnownAs = [knownAs.uri]; + updates.alsoKnownAs = [toUrl]; } else { - updates.alsoKnownAs.push(knownAs.uri); + updates.alsoKnownAs.push(toUrl); } await Users.update(user.id, updates); diff --git a/packages/backend/src/server/api/endpoints/i/move.ts b/packages/backend/src/server/api/endpoints/i/move.ts index 9063e0ca75..4c03719797 100644 --- a/packages/backend/src/server/api/endpoints/i/move.ts +++ b/packages/backend/src/server/api/endpoints/i/move.ts @@ -2,12 +2,13 @@ import type { User } from '@/models/entities/user.js'; import { resolveUser } from '@/remote/resolve-user.js'; import { DAY } from '@/const.js'; import DeliverManager from '@/remote/activitypub/deliver-manager.js'; -import { deliver } from '@/queue/index.js'; import { renderActivity } from '@/remote/activitypub/renderer/index.js'; import { genId } from '@/misc/gen-id.js'; import define from '../../define.js'; import { ApiError } from '../../error.js'; import { apiLogger } from '../../logger.js'; +import { UserProfiles } from '@/models/index.js'; +import config from '@/config/index.js'; export const meta = { tags: ['users'], @@ -52,17 +53,16 @@ export const paramDef = { required: ['moveToAccount'], } as const; -function moveActivity(to: User, from: User) { +function moveActivity(toUrl: string, fromUrl: string) { const activity = { id: genId(), - actor: from, + actor: fromUrl, type: 'Move', - object: from, - target: to, + object: fromUrl, + target: toUrl, } as any; - const content = renderActivity(activity); - deliver(to, content, from.inbox); + return renderActivity(activity); } // 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}`); 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; 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); dm.addFollowersRecipe(); dm.execute(); diff --git a/packages/sw/src/filters/user.ts b/packages/sw/src/filters/user.ts index c1c7b98a92..03aa95aac7 100644 --- a/packages/sw/src/filters/user.ts +++ b/packages/sw/src/filters/user.ts @@ -1,5 +1,5 @@ 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) => { return Acct.toString(user);