fix: Chat 創建失敗, 並修正了 ChatPrivate 獲取對方的邏輯

This commit is contained in:
CrescentLeaf
2025-09-21 02:17:44 +08:00
parent 71e6d24d6e
commit 6a1084eeca
2 changed files with 10 additions and 6 deletions

View File

@@ -25,7 +25,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 /* UserIdA (私信) */ user_a_id TEXT,
/* UserIdB (私信) */ user_b_id TEXT, /* UserIdB (私信) */ user_b_id TEXT,
/* 设置 */ settings TEXT NOT NULL /* 设置 */ settings TEXT NOT NULL
); );
@@ -57,8 +57,8 @@ export default class Chat {
avatar, avatar,
user_a_id, user_a_id,
user_b_id, user_b_id,
settings, settings
) VALUES (?, ?);`).run( ) VALUES (?, ?, ?, ?, ?, ?, ?);`).run(
type, type,
chatId, chatId,
null, null,
@@ -76,7 +76,7 @@ export default class Chat {
constructor(bean: ChatBean) { constructor(bean: ChatBean) {
this.bean = bean this.bean = bean
} }
private setAttr(key: string, value: SQLInputValue): void { setAttr(key: string, value: SQLInputValue): void {
Chat.database.prepare(`UPDATE ${Chat.table_name} SET ${key} = ? WHERE id = ?`).run(value, this.bean.id) Chat.database.prepare(`UPDATE ${Chat.table_name} SET ${key} = ? WHERE id = ?`).run(value, this.bean.id)
this.bean[key] = value this.bean[key] = value
} }

View File

@@ -12,10 +12,14 @@ export default class ChatPrivate extends Chat {
} }
static createForPrivate(userA: User, userB: User) { static createForPrivate(userA: User, userB: User) {
return 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.setAttr('user_b_id', userB.bean.id)
} }
static findByUsersForPrivate(userA: User, userB: User) { static findByUsersForPrivate(userA: User, userB: User) {
return this.fromChat(this.findById(this.getChatIdByUsersId(userA.bean.id, userB.bean.id)) as Chat) const chat = this.findById(this.getChatIdByUsersId(userA.bean.id, userB.bean.id))
if (chat)
return this.fromChat(chat as Chat)
} }
static findOrCreateForPrivate(userA: User, userB: User) { static findOrCreateForPrivate(userA: User, userB: User) {
let a = this.findByUsersForPrivate(userA, userB) let a = this.findByUsersForPrivate(userA, userB)