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