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",
"version": "13.0.0-beta8",
"version": "13.0.0-beta9",
"codename": "aqua",
"repository": {
"type": "git",

View File

@ -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<User>;
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);

View File

@ -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;