From e4c26a07cfe4913c5a58c3a29e4b1656fee2bbf8 Mon Sep 17 00:00:00 2001 From: CrescentLeaf Date: Sun, 21 Sep 2025 16:13:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=B7=A9=E5=AD=98=E8=B3=87=E6=96=99,?= =?UTF-8?q?=20=E7=8D=B2=E5=8F=96=E4=BB=BB=E6=84=8F=E7=94=A8=E6=88=B6?= =?UTF-8?q?=E7=9A=84=E8=B3=87=E6=96=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/api/ApiDeclare.ts | 2 ++ client/api/DataCaches.ts | 19 +++++++++++++++++++ server/api/ApiDeclare.ts | 2 ++ server/api/UserApi.ts | 31 ++++++++++++++++++++++++++++++- 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 client/api/DataCaches.ts diff --git a/client/api/ApiDeclare.ts b/client/api/ApiDeclare.ts index 0e739d0..d503082 100644 --- a/client/api/ApiDeclare.ts +++ b/client/api/ApiDeclare.ts @@ -7,6 +7,8 @@ export type CallMethod = "User.updateProfile" | "User.getMyInfo" | + "User.getInfo" | + "User.getMyContacts" | "User.addContact" | "User.removeContacts" | diff --git a/client/api/DataCaches.ts b/client/api/DataCaches.ts new file mode 100644 index 0000000..f422a1a --- /dev/null +++ b/client/api/DataCaches.ts @@ -0,0 +1,19 @@ +import data from "../Data.ts" +import Client from "./Client.ts" +import User from "./client_data/User.ts" + +export default class DataCaches { + static userProfiles: { [key: string]: User} = {} + static async getUserProfile(userId: string): Promise { + if (this.userProfiles[userId]) return this.userProfiles[userId] + const re = await Client.invoke("User.getInfo", { + token: data.access_token, + target: userId + }) + if (re.code != 200) return { + id: '', + nickname: "", + } + return this.userProfiles[userId] = (re.data as unknown as User) + } +} \ No newline at end of file diff --git a/server/api/ApiDeclare.ts b/server/api/ApiDeclare.ts index 0e739d0..d503082 100644 --- a/server/api/ApiDeclare.ts +++ b/server/api/ApiDeclare.ts @@ -7,6 +7,8 @@ export type CallMethod = "User.updateProfile" | "User.getMyInfo" | + "User.getInfo" | + "User.getMyContacts" | "User.addContact" | "User.removeContacts" | diff --git a/server/api/UserApi.ts b/server/api/UserApi.ts index 0a81020..b8f3c0b 100644 --- a/server/api/UserApi.ts +++ b/server/api/UserApi.ts @@ -34,7 +34,7 @@ export default class UserApi extends BaseApi { msg: "驗證失敗", code: 401, } - + clientInfo.userId = access_token.author console.log(chalk.green('[驗]') + ` ${access_token.author} authed on Client ${deviceId} (ip = ${ip})`) if (ApiManager.clients[clientInfo.userId] == null) ApiManager.clients[clientInfo.userId] = { @@ -250,6 +250,35 @@ export default class UserApi extends BaseApi { * 公開資料 * ================================================ */ + // 獲取用戶信息 + this.registerEvent("User.getInfo", (args, { deviceId }) => { + if (this.checkArgsMissing(args, ['token', 'target'])) return { + msg: "參數缺失", + code: 400, + } + const token = TokenManager.decode(args.token as string) + if (!this.checkToken(token, deviceId)) return { + code: 401, + msg: "令牌無效", + } + + const user = User.findById(args.target as string) + if (user == null) return { + code: 404, + msg: "用戶不存在", + } + + return { + msg: "成功", + code: 200, + data: { + username: user!.getUserName(), + nickname: user!.getNickName(), + avatar: user!.getAvatarFileHash() ? "uploaded_files/" + user!.getAvatarFileHash() : null, + id: token.author, + } + } + }) } } \ No newline at end of file