feat(wip): 實現 ChatPrivate

This commit is contained in:
CrescentLeaf
2025-09-14 14:32:24 +08:00
parent 2b54a7a13a
commit 0af3e7a449
3 changed files with 45 additions and 7 deletions

View File

@@ -21,6 +21,7 @@ export default class Chat {
db.exec(`
CREATE TABLE IF NOT EXISTS ${Chat.table_name} (
/* 序号 */ count INTEGER PRIMARY KEY AUTOINCREMENT,
/* 類型 */ type TEXT NOT NULL,
/* Chat ID */ id TEXT NOT NULL,
/* 標題 (群組) */ title TEXT,
/* 頭像 (群組) */ avatar BLOB,
@@ -32,7 +33,7 @@ export default class Chat {
return db
}
private static findAllBeansByCondition(condition: string, ...args: SQLInputValue[]): ChatBean[] {
protected static findAllBeansByCondition(condition: string, ...args: SQLInputValue[]): ChatBean[] {
return this.database.prepare(`SELECT * FROM ${Chat.table_name} WHERE ${condition}`).all(...args) as unknown as ChatBean[]
}
@@ -45,15 +46,25 @@ export default class Chat {
return new Chat(beans[0])
}
static create(chatId: string) {
static create(chatId: string, type: 'private' | 'group') {
const chat = new Chat(
Chat.findAllBeansByCondition(
'count = ?',
Chat.database.prepare(`INSERT INTO ${Chat.table_name} (
type,
id,
settings
title,
avatar,
user_a_id,
user_b_id,
settings,
) VALUES (?, ?);`).run(
type,
chatId,
null,
null,
null,
null,
"{}"
).lastInsertRowid
)[0]
@@ -61,10 +72,6 @@ export default class Chat {
return chat
}
static createFromTwoUsers(userA: User, userB: User) {
return this.create([userA.bean.id, userB.bean.id].sort().join('-'))
}
declare bean: ChatBean
constructor(bean: ChatBean) {
this.bean = bean

View File

@@ -1,5 +1,10 @@
export default class ChatBean {
declare type: "paivate" | "group"
declare id: string
declare title?: string
declare avatar_file_hash?: string
declare user_a_id?: string
declare user_b_id?: string
declare settings: string
[key: string]: unknown

View File

@@ -0,0 +1,26 @@
import Chat from "./Chat.ts"
import User from "./User.ts";
export default class ChatPrivate extends Chat {
static getChatIdByUsersId(userIdA: string, userIdB: string) {
return [userIdA, userIdB].sort().join('-')
}
static createForPrivate(userA: User, userB: User) {
return this.create(this.getChatIdByUsersId(userA.bean.id, userB.bean.id), 'private')
}
static findForPrivate() {
}
getTitleForPrivate(user: User, targetUser: User) {
const chat = Chat.findById(ChatPrivate.getChatIdByUsersId(user.bean.id, targetUser.bean.id))
if (chat?.bean.user_a_id == user.bean.id)
return targetUser.getNickName()
if (chat?.bean.user_b_id == user.bean.id)
return user.getNickName()
return "未知對話"
}
}