Files
LingChair/server/data/ChatPrivate.ts
CrescentLeaf 5d5b04ba05 refactor: 重構 對話 成員的儲存邏輯
* 使用關聯資料庫, 鏈接 user_id 和 chat_id
2025-09-23 09:20:30 +08:00

34 lines
1.1 KiB
TypeScript

import chalk from "chalk"
import Chat from "./Chat.ts"
import User from "./User.ts"
export default class ChatPrivate extends Chat {
static fromChat(chat: Chat) {
return new ChatPrivate(chat.bean)
}
static getChatIdByUsersId(userIdA: string, userIdB: string) {
return [userIdA, userIdB].sort().join('-')
}
static createForPrivate(userA: User, userB: User) {
const chat = this.create(this.getChatIdByUsersId(userA.bean.id, userB.bean.id), 'private')
chat.addMembers([
userA.bean.id,
userB.bean.id
])
}
static findByUsersForPrivate(userA: User, userB: User) {
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) {
let a = this.findByUsersForPrivate(userA, userB)
if (a == null) {
this.createForPrivate(userA, userB)
a = this.findByUsersForPrivate(userA, userB) as ChatPrivate
}
return a
}
}