Merge pull request '[PR]: enhance: multiple alsoKnownAs' (#10232) from nmkj/calckey:more-aka into develop

Reviewed-on: https://codeberg.org/calckey/calckey/pulls/10232
This commit is contained in:
Kainoa Kanter 2023-05-31 19:10:25 +00:00
commit f56136f680
4 changed files with 111 additions and 70 deletions

View File

@ -1,4 +1,4 @@
import type { User, UserDetailedNotMeOnly } from "@/models/entities/user.js"; import type { User } 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";
@ -6,10 +6,9 @@ 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";
import { parse } from "@/misc/acct.js";
export const meta = { export const meta = {
tags: ["users"], tags: ["users"],
@ -38,49 +37,57 @@ export const meta = {
code: "URI_NULL", code: "URI_NULL",
id: "bf326f31-d430-4f97-9933-5d61e4d48a23", id: "bf326f31-d430-4f97-9933-5d61e4d48a23",
}, },
alreadyMoved: {
message: "You have already moved your account.",
code: "ALREADY_MOVED",
id: "56f20ec9-fd06-4fa5-841b-edd6d7d4fa31",
},
yourself: {
message: "You can't set yourself as your own alias.",
code: "FORBIDDEN_TO_SET_YOURSELF",
id: "25c90186-4ab0-49c8-9bba-a1fa6c202ba4",
},
}, },
} as const; } as const;
export const paramDef = { export const paramDef = {
type: "object", type: "object",
properties: { properties: {
alsoKnownAs: { type: "string" }, alsoKnownAs: {
type: "array",
maxItems: 10,
uniqueItems: true,
items: { type: "string" },
},
}, },
required: ["alsoKnownAs"], required: ["alsoKnownAs"],
} as const; } as const;
export default define(meta, paramDef, async (ps, user) => { 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);
if (user.movedToUri) throw new ApiError(meta.errors.alreadyMoved);
let unfiltered: string = ps.alsoKnownAs; const newAka = new Set<string>();
const updates = {} as Partial<User>;
if (!unfiltered) { for (const line of ps.alsoKnownAs) {
updates.alsoKnownAs = null; if (!line) throw new ApiError(meta.errors.noSuchUser);
} else { const { username, host } = parse(line);
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 aka = await resolveUser(username, host).catch((e) => {
const knownAs = await resolveUser(userAddress[0], userAddress[1]).catch( apiLogger.warn(`failed to resolve remote user: ${e}`);
(e) => { throw new ApiError(meta.errors.noSuchUser);
apiLogger.warn(`failed to resolve remote user: ${e}`); });
throw new ApiError(meta.errors.noSuchUser);
},
);
const toUrl: string | null = knownAs.uri; if (aka.id === user.id) throw new ApiError(meta.errors.yourself);
if (!toUrl) { if (!aka.uri) throw new ApiError(meta.errors.uriNull);
throw new ApiError(meta.errors.uriNull);
} newAka.add(aka.uri);
if (updates.alsoKnownAs == null || updates.alsoKnownAs.length === 0) {
updates.alsoKnownAs = [toUrl];
} else {
updates.alsoKnownAs.push(toUrl);
}
} }
const updates = {
alsoKnownAs: newAka.size > 0 ? Array.from(newAka) : null,
} as Partial<User>;
await Users.update(user.id, updates); await Users.update(user.id, updates);
const iObj = await Users.pack<true, true>(user.id, user, { const iObj = await Users.pack<true, true>(user.id, user, {

View File

@ -10,9 +10,9 @@ import deleteFollowing from "@/services/following/delete.js";
import create from "@/services/following/create.js"; import create from "@/services/following/create.js";
import { getUser } from "@/server/api/common/getters.js"; import { getUser } from "@/server/api/common/getters.js";
import { Followings, Users } from "@/models/index.js"; import { Followings, Users } from "@/models/index.js";
import { UserProfiles } from "@/models/index.js";
import config from "@/config/index.js"; import config from "@/config/index.js";
import { publishMainStream } from "@/services/stream.js"; import { publishMainStream } from "@/services/stream.js";
import { parse } from "@/misc/acct.js";
export const meta = { export const meta = {
tags: ["users"], tags: ["users"],
@ -95,22 +95,13 @@ 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);
if (user.movedToUri) throw new ApiError(meta.errors.alreadyMoved); if (user.movedToUri) throw new ApiError(meta.errors.alreadyMoved);
let unfiltered: string = ps.moveToAccount; const { username, host } = parse(ps.moveToAccount);
if (!unfiltered) { if (!host) throw new ApiError(meta.errors.notRemote);
const moveTo: User = await resolveUser(username, host).catch((e) => {
apiLogger.warn(`failed to resolve remote user: ${e}`);
throw new ApiError(meta.errors.noSuchMoveTarget); 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 moveTo: User = await resolveUser(userAddress[0], userAddress[1]).catch(
(e) => {
apiLogger.warn(`failed to resolve remote user: ${e}`);
throw new ApiError(meta.errors.noSuchMoveTarget);
},
);
let fromUrl: string | null = user.uri; let fromUrl: string | null = user.uri;
if (!fromUrl) { if (!fromUrl) {
fromUrl = `${config.url}/users/${user.id}`; fromUrl = `${config.url}/users/${user.id}`;
@ -134,6 +125,7 @@ export default define(meta, paramDef, async (ps, user) => {
if (!toUrl) toUrl = ""; if (!toUrl) toUrl = "";
updates.movedToUri = toUrl; updates.movedToUri = toUrl;
updates.alsoKnownAs = user.alsoKnownAs?.concat(toUrl) ?? [toUrl];
await Users.update(user.id, updates); await Users.update(user.id, updates);
const iObj = await Users.pack<true, true>(user.id, user, { const iObj = await Users.pack<true, true>(user.id, user, {

View File

@ -54,7 +54,7 @@ export const paramDef = {
anyOf: [ anyOf: [
{ {
properties: { properties: {
userId: { type: "string", format: "misskey:id" }, userId: { type: "string" },
}, },
required: ["userId"], required: ["userId"],
}, },
@ -65,7 +65,6 @@ export const paramDef = {
uniqueItems: true, uniqueItems: true,
items: { items: {
type: "string", type: "string",
format: "misskey:id",
}, },
}, },
}, },
@ -95,21 +94,27 @@ export default define(meta, paramDef, async (ps, me) => {
return []; return [];
} }
const users = await Users.findBy( const isUrl = ps.userIds[0].startsWith("http");
isAdminOrModerator let users: User[];
? { if (isUrl) {
id: In(ps.userIds), users = await Users.findBy(
} isAdminOrModerator
: { ? { uri: In(ps.userIds) }
id: In(ps.userIds), : { uri: In(ps.userIds), isSuspended: false },
isSuspended: false, );
}, } else {
); users = await Users.findBy(
isAdminOrModerator
? { id: In(ps.userIds) }
: { id: In(ps.userIds), isSuspended: false },
);
}
// リクエストされた通りに並べ替え // リクエストされた通りに並べ替え
const _users: User[] = []; const _users: User[] = [];
for (const id of ps.userIds) { for (const id of ps.userIds) {
_users.push(users.find((x) => x.id === id)!); const res = users.find((x) => (isUrl ? x.uri === id : x.id === id));
if (res) _users.push(res);
} }
return await Promise.all( return await Promise.all(
@ -129,7 +134,9 @@ export default define(meta, paramDef, async (ps, me) => {
} else { } else {
const q: FindOptionsWhere<User> = const q: FindOptionsWhere<User> =
ps.userId != null ps.userId != null
? { id: ps.userId } ? ps.userId.startsWith("http")
? { uri: ps.userId }
: { id: ps.userId }
: { usernameLower: ps.username!.toLowerCase(), host: IsNull() }; : { usernameLower: ps.username!.toLowerCase(), host: IsNull() };
user = await Users.findOneBy(q); user = await Users.findOneBy(q);

View File

@ -2,14 +2,14 @@
<div class="_formRoot"> <div class="_formRoot">
<FormSection> <FormSection>
<template #label>{{ i18n.ts.moveTo }}</template> <template #label>{{ i18n.ts.moveTo }}</template>
<FormInfo warn class="_formBlock">{{
i18n.ts.moveAccountDescription
}}</FormInfo>
<FormInput v-model="moveToAccount" class="_formBlock"> <FormInput v-model="moveToAccount" class="_formBlock">
<template #prefix <template #prefix
><i class="ph-airplane-takeoff ph-bold ph-lg"></i ><i class="ph-airplane-takeoff ph-bold ph-lg"></i
></template> ></template>
<template #label>{{ i18n.ts.moveToLabel }}</template> <template #label>{{ i18n.ts.moveToLabel }}</template>
<template #caption>{{
i18n.ts.moveAccountDescription
}}</template>
</FormInput> </FormInput>
<FormButton primary danger @click="move(moveToAccount)"> <FormButton primary danger @click="move(moveToAccount)">
{{ i18n.ts.moveAccount }} {{ i18n.ts.moveAccount }}
@ -18,19 +18,31 @@
<FormSection> <FormSection>
<template #label>{{ i18n.ts.moveFrom }}</template> <template #label>{{ i18n.ts.moveFrom }}</template>
<FormInput v-model="accountAlias" class="_formBlock"> <FormInfo warn class="_formBlock">{{
i18n.ts.moveFromDescription
}}</FormInfo>
<FormInput
v-for="(_, i) in accountAlias"
v-model="accountAlias[i]"
class="_formBlock"
>
<template #prefix <template #prefix
><i class="ph-airplane-landing ph-bold ph-lg"></i ><i class="ph-airplane-landing ph-bold ph-lg"></i
></template> ></template>
<template #label>{{ i18n.ts.moveFromLabel }}</template> <template #label>{{
<template #caption>{{ i18n.ts.moveFromDescription }}</template> `#${i + 1} ${i18n.ts.moveFromLabel}`
}}</template>
</FormInput> </FormInput>
<FormButton <FormButton
class="button" class="button"
:disabled="accountAlias.length >= 10"
inline inline
primary style="margin-right: 8px"
@click="save(accountAlias.toString())" @click="add"
><i class="ph-plus ph-bold ph-lg"></i>
{{ i18n.ts.add }}</FormButton
> >
<FormButton class="button" inline primary @click="save">
<i class="ph-floppy-disk-back ph-bold ph-lg"></i> <i class="ph-floppy-disk-back ph-bold ph-lg"></i>
{{ i18n.ts.save }} {{ i18n.ts.save }}
</FormButton> </FormButton>
@ -42,17 +54,40 @@
import FormSection from "@/components/form/section.vue"; import FormSection from "@/components/form/section.vue";
import FormInput from "@/components/form/input.vue"; import FormInput from "@/components/form/input.vue";
import FormButton from "@/components/MkButton.vue"; import FormButton from "@/components/MkButton.vue";
import FormInfo from "@/components/MkInfo.vue";
import * as os from "@/os"; import * as os from "@/os";
import { i18n } from "@/i18n"; import { i18n } from "@/i18n";
import { definePageMetadata } from "@/scripts/page-metadata"; import { definePageMetadata } from "@/scripts/page-metadata";
import { $i } from "@/account";
import { toString } from "calckey-js/built/acct";
let moveToAccount = $ref(""); let moveToAccount = $ref("");
let accountAlias = $ref(""); let accountAlias = $ref([""]);
async function save(account): Promise<void> { await init();
os.apiWithDialog("i/known-as", {
alsoKnownAs: account, async function init() {
if ($i?.alsoKnownAs && $i.alsoKnownAs.length > 0) {
const aka = await os.api("users/show", { userIds: $i.alsoKnownAs });
accountAlias =
aka && aka.length > 0
? aka.map((user) => `@${toString(user)}`)
: [""];
} else {
accountAlias = [""];
}
}
async function save(): Promise<void> {
const i = await os.apiWithDialog("i/known-as", {
alsoKnownAs: accountAlias.map((e) => e.trim()).filter((e) => e !== ""),
}); });
$i.alsoKnownAs = i.alsoKnownAs;
await init();
}
function add(): void {
accountAlias.push("");
} }
async function move(account): Promise<void> { async function move(account): Promise<void> {