This commit is contained in:
CrescentLeaf
2025-09-20 20:32:26 +08:00
parent 1acc73c7b4
commit eaf0f98058
8 changed files with 114 additions and 40 deletions

View File

@@ -30,13 +30,13 @@ export default class Chat {
/* 设置 */ settings TEXT NOT NULL
);
`)
return db
return db
}
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[]
}
static findById(id: string) {
const beans = this.findAllBeansByCondition('id = ?', id)
if (beans.length == 0)
@@ -49,7 +49,7 @@ export default class Chat {
static create(chatId: string, type: 'private' | 'group') {
const chat = new Chat(
Chat.findAllBeansByCondition(
'count = ?',
'count = ?',
Chat.database.prepare(`INSERT INTO ${Chat.table_name} (
type,
id,
@@ -80,4 +80,13 @@ export default class Chat {
Chat.database.prepare(`UPDATE ${Chat.table_name} SET ${key} = ? WHERE id = ?`).run(value, this.bean.id)
this.bean[key] = value
}
getTitleForPrivate(userMySelf: User) {
if (this.bean.user_a_id == userMySelf.bean.id)
return User.findById(this.bean?.user_b_id as string)?.getNickName() || "未知對話"
if (this.bean.user_b_id == userMySelf.bean.id)
return userMySelf.getNickName()
return "未知對話"
}
}

View File

@@ -25,15 +25,4 @@ export default class ChatPrivate extends Chat {
}
return a
}
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 "未知對話"
}
}

View File

@@ -9,8 +9,11 @@ import config from '../config.ts'
import UserBean from './UserBean.ts'
import FileManager from './FileManager.ts'
import { SQLInputValue } from "node:sqlite";
import DataWrongError from "../api/DataWrongError.ts";
import { SQLInputValue } from "node:sqlite"
import DataWrongError from "../api/DataWrongError.ts"
import ChatPrivate from "./ChatPrivate.ts"
import Chat from "./Chat.ts"
import ChatBean from "./ChatBean.ts";
type UserBeanKey = keyof UserBean
@@ -77,6 +80,7 @@ export default class User {
)[0]
)
avatar && user.setAvatar(avatar)
user.addContact(ChatPrivate.getChatIdByUsersId(user.bean.id, user.bean.id))
return user
}
@@ -115,12 +119,14 @@ export default class User {
setUserName(userName: string) {
this.setAttr("username", userName)
}
addContact(userId) {
addContact(chatId: string) {
const ls = this.getContactsList()
ls.push(chatId)
this.setAttr("contacts_list", JSON.stringify(ls))
}
getContactsList() {
try {
return JSON.parse(this.bean.contacts_list)
return JSON.parse(this.bean.contacts_list) as string[]
} catch (e) {
console.log(chalk.yellow(`警告: 聯絡人組解析失敗: ${(e as Error).message}`))
return [