feat: 緩存資料, 獲取任意用戶的資料

This commit is contained in:
CrescentLeaf
2025-09-21 16:13:01 +08:00
parent f118c6b6f5
commit e4c26a07cf
4 changed files with 53 additions and 1 deletions

View File

@@ -7,6 +7,8 @@ export type CallMethod =
"User.updateProfile" |
"User.getMyInfo" |
"User.getInfo" |
"User.getMyContacts" |
"User.addContact" |
"User.removeContacts" |

19
client/api/DataCaches.ts Normal file
View File

@@ -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<User> {
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)
}
}

View File

@@ -7,6 +7,8 @@ export type CallMethod =
"User.updateProfile" |
"User.getMyInfo" |
"User.getInfo" |
"User.getMyContacts" |
"User.addContact" |
"User.removeContacts" |

View File

@@ -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,
}
}
})
}
}