revert url to uri

This commit is contained in:
cutestnekoaqua 2022-12-15 16:33:08 +01:00
parent e67afc66f3
commit af61a1243e
No known key found for this signature in database
GPG Key ID: 6BF0964A5069C1E0
3 changed files with 66 additions and 52 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "calckey", "name": "calckey",
"version": "13.0.0-beta8", "version": "13.0.0-beta9",
"codename": "aqua", "codename": "aqua",
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -1,18 +1,18 @@
import type { User, UserDetailedNotMeOnly } from '@/models/entities/user.js'; import type { User, UserDetailedNotMeOnly } from "@/models/entities/user.js";
import { Users } from '@/models/index.js'; import { Users } from "@/models/index.js";
import { resolveUser } from '@/remote/resolve-user.js'; import { resolveUser } from "@/remote/resolve-user.js";
import acceptAllFollowRequests from '@/services/following/requests/accept-all.js'; import acceptAllFollowRequests from "@/services/following/requests/accept-all.js";
import { publishToFollowers } from '@/services/i/update.js'; 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 { UserProfiles } from "@/models/index.js";
import config from '@/config/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";
export const meta = { export const meta = {
tags: ['users'], tags: ["users"],
secure: true, secure: true,
requireCredential: true, requireCredential: true,
@ -24,24 +24,29 @@ export const meta = {
errors: { errors: {
noSuchUser: { noSuchUser: {
message: 'No such user.', message: "No such user.",
code: 'NO_SUCH_USER', code: "NO_SUCH_USER",
id: 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5', id: "fcd2eef9-a9b2-4c4f-8624-038099e90aa5",
}, },
notRemote: { notRemote: {
message: 'User is not remote. You can only migrate to other instances.', message: "User is not remote. You can only migrate to other instances.",
code: 'NOT_REMOTE', code: "NOT_REMOTE",
id: '4362f8dc-731f-4ad8-a694-be2a88922a24', id: "4362f8dc-731f-4ad8-a694-be2a88922a24",
},
uriNull: {
message: "User ActivityPup URI is null.",
code: "URI_NULL",
id: "bf326f31-d430-4f97-9933-5d61e4d48a23",
}, },
}, },
} as const; } as const;
export const paramDef = { export const paramDef = {
type: 'object', type: "object",
properties: { properties: {
alsoKnownAs: { type: 'string' }, alsoKnownAs: { type: "string" },
}, },
required: ['alsoKnownAs'], required: ["alsoKnownAs"],
} as const; } as const;
// eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export
@ -49,31 +54,33 @@ export default define(meta, paramDef, async (ps, user) => {
if (!ps.alsoKnownAs) throw new ApiError(meta.errors.noSuchUser); if (!ps.alsoKnownAs) throw new ApiError(meta.errors.noSuchUser);
let unfiltered: string = ps.alsoKnownAs; 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<User>; const updates = {} as Partial<User>;
if (!toUrl) toUrl = ''; if (!unfiltered) {
updates.alsoKnownAs = null;
} else {
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) { if (updates.alsoKnownAs == null || updates.alsoKnownAs.length === 0) {
updates.alsoKnownAs = [toUrl]; updates.alsoKnownAs = [toUrl];
} else { } else {
updates.alsoKnownAs.push(toUrl); updates.alsoKnownAs.push(toUrl);
} }
}
await Users.update(user.id, updates); await Users.update(user.id, updates);
@ -83,7 +90,7 @@ export default define(meta, paramDef, async (ps, user) => {
}); });
// Publish meUpdated event // Publish meUpdated event
publishMainStream(user.id, 'meUpdated', iObj); publishMainStream(user.id, "meUpdated", iObj);
if (user.isLocked === false) { if (user.isLocked === false) {
acceptAllFollowRequests(user); acceptAllFollowRequests(user);

View File

@ -52,6 +52,11 @@ export const meta = {
code: 'NO_SUCH_USER', code: 'NO_SUCH_USER',
id: 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5', id: 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5',
}, },
uriNull: {
message: "User ActivityPup URI is null.",
code: "URI_NULL",
id: "bf326f31-d430-4f97-9933-5d61e4d48a23",
},
}, },
} as const; } as const;
@ -81,25 +86,27 @@ export default define(meta, paramDef, async (ps, user) => {
if (user.isAdmin) throw new ApiError(meta.errors.adminForbidden); if (user.isAdmin) throw new ApiError(meta.errors.adminForbidden);
let unfiltered: string = ps.moveToAccount; 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.startsWith('@')) unfiltered = unfiltered.substring(1);
if (!unfiltered.includes('@')) throw new ApiError(meta.errors.notRemote); 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 => { const moveTo: User = await resolveUser(userAddress[0], userAddress[1]).catch(e => {
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 = user.uri;
let fromUrl: string | null = profileFrom.url;
if(!fromUrl) { 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 = moveTo.uri;
let toUrl: string | null = profileTo.url;
if(!toUrl) { if(!toUrl) {
toUrl = `${config.url}/@${moveTo.username}`; throw new ApiError(meta.errors.uriNull);
} }
let allowed = false; let allowed = false;