This commit is contained in:
MoonLeeeaf
2025-03-16 23:49:28 +08:00
parent 003f046b33
commit 94a5ce3ba4

54
server/api/Chat.js Normal file
View File

@@ -0,0 +1,54 @@
import io from '../lib/io.js';
const baseDir = 'whiteslik_data/chat'
io.mkdirs(baseDir)
export class ChatManager {
/**
* 获取私聊实例 (双方可对调)
* @param { String } a 用户A 的 ID
* @param { String } b 用户B 的 ID
* @returns { Chat }
*/
static getPrivateChat(a, b) {
let id = [
a,
b,
].sort()
if (!io.exists(`${baseDir}/${id}`)) {
io.mkdirs(`${baseDir}/${id}`)
}
}
}
export class Chat {
constructor(id) {
if (!io.exists(`${baseDir}/${id}`)) throw new Error(`聊天 [id=${id}]不存在!`)
// 尽管所有的键都是 undefined 但是仍然是键哦
for (let k of Object.keys(this)) {
this[k] = io.open(`${baseDir}/${id}/${k}`, 'rw').checkExistsOrWrite('').readAllAndClose().toString()
}
}
updateInfo() {
// 尽管所有的键都是 undefined 但是仍然是键哦
for (let k of Object.keys(this)) {
io.open(`${baseDir}/${this.id}/${k}`, 'w').writeAll((this[k] || '') + '').close()
}
// 防止服务端错误修改此值 主要是都是属性了再搞特殊对待很麻烦的
io.open(`${baseDir}/${this.id}/id`, 'w').writeAll(this.id + '').close()
}
/**
* 聊天 ID
* @type { String }
*/
id
}
export class ChatApi {
static createUser() {
}
}