fix: 添加了新的字段代替 chat id

* 谁又能想到 chat id 的可变性和依赖性恰恰埋下了祸患呢
This commit is contained in:
CrescentLeaf
2025-10-24 22:21:28 +08:00
parent afd9193dea
commit 2d78e39ca1
9 changed files with 49 additions and 38 deletions

View File

@@ -27,8 +27,9 @@ export default class Chat {
/* 序号 */ count INTEGER PRIMARY KEY AUTOINCREMENT,
/* 类型 */ type TEXT NOT NULL,
/* ID */ id TEXT NOT NULL,
/* 检索 */ name TEXT,
/* 标题 */ title TEXT,
/* 头像 */ avatar_file_hash BLOB,
/* 头像 */ avatar_file_hash TEXT,
/* 设置 */ settings TEXT NOT NULL
);
`)
@@ -48,21 +49,32 @@ export default class Chat {
return new Chat(beans[0])
}
static create(chatId: string, type: ChatType) {
if (this.findAllChatBeansByCondition('id = ?', chatId).length > 0)
throw new DataWrongError(`对话 ID ${chatId} 已被使用`)
static findByName(name: string) {
const beans = this.findAllChatBeansByCondition('name = ?', name)
if (beans.length == 0)
return null
else if (beans.length > 1)
console.error(chalk.red(`警告: 查询 name = ${name} 时, 查询到多个相同 name 的 Chat`))
return new Chat(beans[0])
}
static create(chatName: string | undefined, type: ChatType) {
if (this.findAllChatBeansByCondition('id = ?', chatName || null).length > 0)
throw new DataWrongError(`对话名称 ${chatName} 已被使用`)
const chat = new Chat(
Chat.findAllChatBeansByCondition(
'count = ?',
Chat.database.prepare(`INSERT INTO Chat (
type,
id,
name,
title,
avatar_file_hash,
settings
) VALUES (?, ?, ?, ?, ?);`).run(
) VALUES (?, ?, ?, ?, ?, ?);`).run(
type,
chatId,
crypto.randomUUID(),
chatName || null,
null,
null,
"{}"
@@ -200,11 +212,11 @@ export default class Chat {
return null
}
setId(id: string) {
if (this.bean.id == id) return
if (Chat.findAllChatBeansByCondition('id = ?', id).length > 0)
throw new DataWrongError(`对话ID ${id} 已被使用`)
this.setAttr("id", id)
setName(name: string) {
if (this.bean.name == name) return
if (name != null && Chat.findAllChatBeansByCondition('name = ?', name).length > 0)
throw new DataWrongError(`对话名称 ${name} 已被使用`)
this.setAttr("name", name)
}
setTitle(title: string) {
if (this.bean.type == 'private')

View File

@@ -4,6 +4,7 @@ export default class ChatBean {
declare count: number
declare type: ChatType
declare id: string
declare name?: string
declare title?: string
declare avatar_file_hash?: string
declare members_list: string

View File

@@ -40,7 +40,7 @@ export default class ChatGroup extends Chat {
return new ChatGroup(chat.bean)
}
static createGroup(chatId?: string) {
return this.create(chatId || crypto.randomUUID(), 'group')
static createGroup(group_name?: string) {
return this.create(group_name, 'group')
}
}

View File

@@ -8,11 +8,12 @@ export default class ChatPrivate extends Chat {
}
static getChatIdByUsersId(userIdA: string, userIdB: string) {
return [userIdA, userIdB].sort().join('------')
return 'priv_' + [userIdA, userIdB].sort().join('__').replaceAll('-', '_')
}
static createForPrivate(userA: User, userB: User) {
const chat = this.create(this.getChatIdByUsersId(userA.bean.id, userB.bean.id), 'private')
const chat = this.create(undefined, 'private')
chat.setAttr('id', this.getChatIdByUsersId(userA.bean.id, userB.bean.id))
chat.addMembers([
userA.bean.id,
userB.bean.id