From af61a1243ec4d8369c980d9dd6636bcb15d5c90d Mon Sep 17 00:00:00 2001 From: cutestnekoaqua Date: Thu, 15 Dec 2022 16:33:08 +0100 Subject: [PATCH] revert url to uri --- package.json | 2 +- .../src/server/api/endpoints/i/known-as.ts | 95 ++++++++++--------- .../src/server/api/endpoints/i/move.ts | 21 ++-- 3 files changed, 66 insertions(+), 52 deletions(-) diff --git a/package.json b/package.json index 9552559436..1bbc2f7c63 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "calckey", - "version": "13.0.0-beta8", + "version": "13.0.0-beta9", "codename": "aqua", "repository": { "type": "git", 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 9038bca378..a7a32aee6f 100644 --- a/packages/backend/src/server/api/endpoints/i/known-as.ts +++ b/packages/backend/src/server/api/endpoints/i/known-as.ts @@ -1,18 +1,18 @@ -import type { User, UserDetailedNotMeOnly } from '@/models/entities/user.js'; -import { Users } from '@/models/index.js'; -import { resolveUser } from '@/remote/resolve-user.js'; -import acceptAllFollowRequests from '@/services/following/requests/accept-all.js'; -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'; +import type { User, UserDetailedNotMeOnly } from "@/models/entities/user.js"; +import { Users } from "@/models/index.js"; +import { resolveUser } from "@/remote/resolve-user.js"; +import acceptAllFollowRequests from "@/services/following/requests/accept-all.js"; +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"; export const meta = { - tags: ['users'], + tags: ["users"], secure: true, requireCredential: true, @@ -24,24 +24,29 @@ export const meta = { errors: { noSuchUser: { - message: 'No such user.', - code: 'NO_SUCH_USER', - id: 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5', + message: "No such user.", + code: "NO_SUCH_USER", + id: "fcd2eef9-a9b2-4c4f-8624-038099e90aa5", }, notRemote: { - message: 'User is not remote. You can only migrate to other instances.', - code: 'NOT_REMOTE', - id: '4362f8dc-731f-4ad8-a694-be2a88922a24', + message: "User is not remote. You can only migrate to other instances.", + code: "NOT_REMOTE", + id: "4362f8dc-731f-4ad8-a694-be2a88922a24", + }, + uriNull: { + message: "User ActivityPup URI is null.", + code: "URI_NULL", + id: "bf326f31-d430-4f97-9933-5d61e4d48a23", }, }, } as const; export const paramDef = { - type: 'object', + type: "object", properties: { - alsoKnownAs: { type: 'string' }, + alsoKnownAs: { type: "string" }, }, - required: ['alsoKnownAs'], + required: ["alsoKnownAs"], } as const; // eslint-disable-next-line import/no-default-export @@ -49,30 +54,32 @@ export default define(meta, paramDef, async (ps, user) => { if (!ps.alsoKnownAs) throw new ApiError(meta.errors.noSuchUser); let unfiltered: string = ps.alsoKnownAs; - - if (unfiltered.startsWith('@')) unfiltered = unfiltered.substring(1); - if (!unfiltered.includes('@')) throw new ApiError(meta.errors.notRemote); - - const userAddress: string[] = unfiltered.split('@'); - - const knownAs: UserDetailedNotMeOnly = await resolveUser(userAddress[0], userAddress[1]).catch(e => { - apiLogger.warn(`failed to resolve remote user: ${e}`); - 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; - if (!toUrl) toUrl = ''; - if (updates.alsoKnownAs == null || updates.alsoKnownAs.length === 0) { - updates.alsoKnownAs = [toUrl]; + if (!unfiltered) { + updates.alsoKnownAs = null; } else { - updates.alsoKnownAs.push(toUrl); + if (unfiltered.startsWith('acct:')) unfiltered = unfiltered.substring(5); + if (unfiltered.startsWith("@")) unfiltered = unfiltered.substring(1); + if (!unfiltered.includes("@")) throw new ApiError(meta.errors.notRemote); + + const userAddress: string[] = unfiltered.split("@"); + const knownAs = await resolveUser(userAddress[0], userAddress[1]).catch( + (e) => { + apiLogger.warn(`failed to resolve remote user: ${e}`); + throw new ApiError(meta.errors.noSuchUser); + } + ); + + let toUrl: string | null = knownAs.uri; + if (!toUrl) { + throw new ApiError(meta.errors.uriNull); + } + if (updates.alsoKnownAs == null || updates.alsoKnownAs.length === 0) { + updates.alsoKnownAs = [toUrl]; + } else { + updates.alsoKnownAs.push(toUrl); + } } await Users.update(user.id, updates); @@ -83,7 +90,7 @@ export default define(meta, paramDef, async (ps, user) => { }); // Publish meUpdated event - publishMainStream(user.id, 'meUpdated', iObj); + publishMainStream(user.id, "meUpdated", iObj); if (user.isLocked === false) { acceptAllFollowRequests(user); diff --git a/packages/backend/src/server/api/endpoints/i/move.ts b/packages/backend/src/server/api/endpoints/i/move.ts index bae1cc2cac..5a26d3e3c0 100644 --- a/packages/backend/src/server/api/endpoints/i/move.ts +++ b/packages/backend/src/server/api/endpoints/i/move.ts @@ -52,6 +52,11 @@ export const meta = { code: 'NO_SUCH_USER', id: 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5', }, + uriNull: { + message: "User ActivityPup URI is null.", + code: "URI_NULL", + id: "bf326f31-d430-4f97-9933-5d61e4d48a23", + }, }, } as const; @@ -81,25 +86,27 @@ export default define(meta, paramDef, async (ps, user) => { if (user.isAdmin) throw new ApiError(meta.errors.adminForbidden); let unfiltered: string = ps.moveToAccount; + if (!unfiltered) { + throw new ApiError(meta.errors.noSuchMoveTarget); + } + if (unfiltered.startsWith('acct:')) unfiltered = unfiltered.substring(5); if (unfiltered.startsWith('@')) unfiltered = unfiltered.substring(1); if (!unfiltered.includes('@')) throw new ApiError(meta.errors.notRemote); - const userAddress: string[] = unfiltered.split('@'); + const userAddress: string[] = unfiltered.split('@'); const moveTo: User = await resolveUser(userAddress[0], userAddress[1]).catch(e => { 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; + let fromUrl: string | null = user.uri; if(!fromUrl) { - fromUrl = `${config.url}/@${user.username}`; + throw new ApiError(meta.errors.uriNull); } - const profileTo = await UserProfiles.findOneByOrFail({ userId: moveTo.id }); - let toUrl: string | null = profileTo.url; + let toUrl: string | null = moveTo.uri; if(!toUrl) { - toUrl = `${config.url}/@${moveTo.username}`; + throw new ApiError(meta.errors.uriNull); } let allowed = false;