From 5469ff682606a20e004f564678360741769a4c87 Mon Sep 17 00:00:00 2001 From: CrescentLeaf Date: Sun, 26 Oct 2025 23:02:56 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8B=A5factor:=20addContact=20->=20s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/api/ApiDeclare.ts | 2 +- client/ui/dialog/AddContactDialog.tsx | 6 ++-- server/api/ApiDeclare.ts | 2 +- server/api/UserApi.ts | 51 +++++++++++++++++++-------- 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/client/api/ApiDeclare.ts b/client/api/ApiDeclare.ts index 60e3ffa..6245120 100644 --- a/client/api/ApiDeclare.ts +++ b/client/api/ApiDeclare.ts @@ -12,7 +12,7 @@ export type CallMethod = "User.getInfo" | "User.getMyContacts" | - "User.addContact" | + "User.addContacts" | "User.removeContacts" | "User.getMyRecentChats" | diff --git a/client/ui/dialog/AddContactDialog.tsx b/client/ui/dialog/AddContactDialog.tsx index 9e9baba..b290f00 100644 --- a/client/ui/dialog/AddContactDialog.tsx +++ b/client/ui/dialog/AddContactDialog.tsx @@ -18,14 +18,14 @@ export default function AddContactDialog({ const inputTargetRef = React.useRef(null) async function addContact() { - const re = await Client.invoke("User.addContact", { - target: inputTargetRef.current!.value, + const re = await Client.invoke("User.addContacts", { + targets: [inputTargetRef.current!.value], token: data.access_token, }) if (checkApiSuccessOrSncakbar(re, "添加失敗")) return snackbar({ - message: "添加成功!", + message: re.msg, placement: "top", }) EventBus.emit('ContactsList.updateContacts') diff --git a/server/api/ApiDeclare.ts b/server/api/ApiDeclare.ts index 377544d..a4c7766 100644 --- a/server/api/ApiDeclare.ts +++ b/server/api/ApiDeclare.ts @@ -12,7 +12,7 @@ export type CallMethod = "User.getInfo" | "User.getMyContacts" | - "User.addContact" | + "User.addContacts" | "User.removeContacts" | "User.getMyRecentChats" | diff --git a/server/api/UserApi.ts b/server/api/UserApi.ts index d6ae048..c79fec1 100644 --- a/server/api/UserApi.ts +++ b/server/api/UserApi.ts @@ -339,8 +339,41 @@ export default class UserApi extends BaseApi { } }) // 添加聯絡人 - this.registerEvent("User.addContact", (args, { deviceId }) => { - if (this.checkArgsMissing(args, ['token', 'target'])) return { + this.registerEvent("User.addContacts", (args, { deviceId }) => { + if (this.checkArgsMissing(args, ['token', 'targets'])) return { + msg: "参数缺失", + code: 400, + } + + const token = TokenManager.decode(args.token as string) + if (!this.checkToken(token, deviceId)) return { + code: 401, + msg: "令牌无效", + } + + let fail = 0 + const user = User.findById(token.author) as User + for (const target of (args.targets as string[])) { + const chat = Chat.findById(target) || Chat.findByName(target) + const targetUser = User.findByAccount(target) as User + if (chat) + user!.addContact(chat.bean.id) + else if (targetUser) { + const privChat = ChatPrivate.findOrCreateForPrivate(user, targetUser) + user!.addContact(privChat.bean.id) + } else { + fail++ + } + } + + return { + msg: "添加成功 (失败 " + fail + " 个)", + code: 200, + } + }) + // 添加聯絡人 + this.registerEvent("User.removeContacts", (args, { deviceId }) => { + if (this.checkArgsMissing(args, ['token', 'targets'])) return { msg: "参数缺失", code: 400, } @@ -352,19 +385,7 @@ export default class UserApi extends BaseApi { } const user = User.findById(token.author) as User - const chat = Chat.findById(args.target as string) || Chat.findByName(args.target as string) - const targetUser = User.findByAccount(args.target as string) as User - if (chat) - user!.addContact(chat.bean.id) - else if (targetUser) { - const privChat = ChatPrivate.findOrCreateForPrivate(user, targetUser) - user!.addContact(privChat.bean.id) - } else { - return { - msg: "找不到目标", - code: 404, - } - } + user.removeContacts(args.targets as string[]) return { msg: "成功",