feat: RecentChats in User & Bean

This commit is contained in:
CrescentLeaf
2025-10-04 14:12:07 +08:00
parent a1eddf813d
commit 4133c13cf8
2 changed files with 21 additions and 2 deletions

View File

@@ -14,6 +14,7 @@ import DataWrongError from "../api/DataWrongError.ts"
import ChatPrivate from "./ChatPrivate.ts" import ChatPrivate from "./ChatPrivate.ts"
import Chat from "./Chat.ts" import Chat from "./Chat.ts"
import ChatBean from "./ChatBean.ts" import ChatBean from "./ChatBean.ts"
import MapJson from "../MapJson.ts";
type UserBeanKey = keyof UserBean type UserBeanKey = keyof UserBean
@@ -36,7 +37,8 @@ export default class User {
/* 用戶名, 可選 */ username TEXT, /* 用戶名, 可選 */ username TEXT,
/* 昵称 */ nickname TEXT NOT NULL, /* 昵称 */ nickname TEXT NOT NULL,
/* 头像, 可选 */ avatar_file_hash TEXT, /* 头像, 可选 */ avatar_file_hash TEXT,
/* 聯絡人 */ contacts_list TEXT NOT NULL, /* 聯絡人列表 */ contacts_list TEXT NOT NULL,
/* 最近對話 */ recent_chats TEXT NOT NULL,
/* 设置 */ settings TEXT NOT NULL /* 设置 */ settings TEXT NOT NULL
); );
`) `)
@@ -66,8 +68,9 @@ export default class User {
nickname, nickname,
avatar_file_hash, avatar_file_hash,
contacts_list, contacts_list,
recent_chats,
settings settings
) VALUES (?, ?, ?, ?, ?, ?, ?, ?);`).run( ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);`).run(
crypto.randomUUID(), crypto.randomUUID(),
password, password,
Date.now(), Date.now(),
@@ -75,6 +78,7 @@ export default class User {
nickName, nickName,
null, null,
'[]', '[]',
JSON.stringify(new Map(), MapJson.replacer),
"{}" "{}"
).lastInsertRowid ).lastInsertRowid
)[0] )[0]
@@ -122,6 +126,20 @@ export default class User {
setUserName(userName: string) { setUserName(userName: string) {
this.setAttr("username", userName) this.setAttr("username", userName)
} }
updateRecentChat(chatId: string, content: string) {
const map = JSON.parse(this.bean.recent_chats, MapJson.reviver) as Map<string, string>
map.delete(chatId)
map.set(chatId, content)
this.setAttr("recent_chats", JSON.stringify(map, MapJson.replacer))
}
getRecentChats() {
try {
return JSON.parse(this.bean.recent_chats, MapJson.reviver) as Map<string, string>
} catch (e) {
console.log(chalk.yellow(`警告: 最近对话列表解析失敗: ${(e as Error).message}`))
return new Map()
}
}
addContact(chatId: string) { addContact(chatId: string) {
const ls = this.getContactsList() const ls = this.getContactsList()
if (ls.indexOf(chatId) != -1 || ChatPrivate.getChatIdByUsersId(this.bean.id, this.bean.id) == chatId) return if (ls.indexOf(chatId) != -1 || ChatPrivate.getChatIdByUsersId(this.bean.id, this.bean.id) == chatId) return

View File

@@ -7,6 +7,7 @@ export default class UserBean {
declare nickname: string declare nickname: string
declare avatar_file_hash?: string declare avatar_file_hash?: string
declare contacts_list: string declare contacts_list: string
declare recent_chats: string
declare settings: string declare settings: string
[key: string]: unknown [key: string]: unknown