refactor: 重寫 Chat 成員邏輯

* 不再區分 user_a/b, 直接使用 members_list 雙成員模式
* 爲以後群聊打下基礎
This commit is contained in:
CrescentLeaf
2025-09-22 23:08:19 +08:00
parent 184a80436d
commit 10da3b8e77
3 changed files with 34 additions and 16 deletions

View File

@@ -7,6 +7,7 @@ import ChatBean from './ChatBean.ts'
import { SQLInputValue } from "node:sqlite" import { SQLInputValue } from "node:sqlite"
import chalk from "chalk" import chalk from "chalk"
import User from "./User.ts" import User from "./User.ts"
import ChatType from "./ChatType.ts"
/** /**
* Chat.ts - Wrapper and manager * Chat.ts - Wrapper and manager
@@ -25,8 +26,7 @@ export default class Chat {
/* Chat ID */ id TEXT NOT NULL, /* Chat ID */ id TEXT NOT NULL,
/* 標題 (群組) */ title TEXT, /* 標題 (群組) */ title TEXT,
/* 頭像 (群組) */ avatar BLOB, /* 頭像 (群組) */ avatar BLOB,
/* UserIdA (私信) */ user_a_id TEXT, /* 成員 */ members_list TEXT,
/* UserIdB (私信) */ user_b_id TEXT,
/* 设置 */ settings TEXT NOT NULL /* 设置 */ settings TEXT NOT NULL
); );
`) `)
@@ -46,7 +46,7 @@ export default class Chat {
return new Chat(beans[0]) return new Chat(beans[0])
} }
static create(chatId: string, type: 'private' | 'group') { static create(chatId: string, type: ChatType) {
const chat = new Chat( const chat = new Chat(
Chat.findAllBeansByCondition( Chat.findAllBeansByCondition(
'count = ?', 'count = ?',
@@ -55,16 +55,14 @@ export default class Chat {
id, id,
title, title,
avatar, avatar,
user_a_id, members_list,
user_b_id,
settings settings
) VALUES (?, ?, ?, ?, ?, ?, ?);`).run( ) VALUES (?, ?, ?, ?, ?, ?);`).run(
type, type,
chatId, chatId,
null, null,
null, null,
null, "[]",
null,
"{}" "{}"
).lastInsertRowid ).lastInsertRowid
)[0] )[0]
@@ -81,11 +79,28 @@ export default class Chat {
this.bean[key] = value this.bean[key] = value
} }
getMembersList() {
return JSON.parse(this.bean.members_list) as string[]
}
addMember(userId: string) {
const ls = this.getMembersList()
ls.push(userId)
this.setMembers(ls)
}
setMembers(members: string[]) {
this.setAttr("members_list", JSON.stringify(members))
}
removeMembers(members: string[]) {
const ls = this.getMembersList().filter((v) => !members.includes(v))
this.setAttr("members_list", JSON.stringify(ls))
}
getAnotherUserForPrivate(userMySelf: User) { getAnotherUserForPrivate(userMySelf: User) {
const user_a_id = this.getMembersList()[0]
const user_b_id = this.getMembersList()[0]
// 注意: 這裏已經確定了 Chat, 不需要再指定對方用戶 // 注意: 這裏已經確定了 Chat, 不需要再指定對方用戶
if (this.bean.user_a_id == userMySelf.bean.id) if (user_a_id == userMySelf.bean.id)
return User.findById(this.bean?.user_b_id as string) return User.findById(user_b_id as string)
if (this.bean.user_b_id == userMySelf.bean.id) if (user_b_id == userMySelf.bean.id)
return userMySelf return userMySelf
return null return null

View File

@@ -1,10 +1,11 @@
import ChatType from "./ChatType.ts"
export default class ChatBean { export default class ChatBean {
declare type: "private" | "group" declare type: ChatType
declare id: string declare id: string
declare title?: string declare title?: string
declare avatar_file_hash?: string declare avatar_file_hash?: string
declare user_a_id?: string declare members_list: string
declare user_b_id?: string
declare settings: string declare settings: string
[key: string]: unknown [key: string]: unknown

View File

@@ -13,8 +13,10 @@ export default class ChatPrivate extends Chat {
static createForPrivate(userA: User, userB: User) { static createForPrivate(userA: User, userB: User) {
const chat = this.create(this.getChatIdByUsersId(userA.bean.id, userB.bean.id), 'private') const chat = this.create(this.getChatIdByUsersId(userA.bean.id, userB.bean.id), 'private')
chat.setAttr('user_a_id', userA.bean.id) chat.setMembers([
chat.setAttr('user_b_id', userB.bean.id) userA.bean.id,
userB.bean.id
])
} }
static findByUsersForPrivate(userA: User, userB: User) { static findByUsersForPrivate(userA: User, userB: User) {
const chat = this.findById(this.getChatIdByUsersId(userA.bean.id, userB.bean.id)) const chat = this.findById(this.getChatIdByUsersId(userA.bean.id, userB.bean.id))