From 94a5ce3ba488420d23dedcd79bcb87ad312bfd87 Mon Sep 17 00:00:00 2001 From: MoonLeeeaf <150461955+MoonLeeeaf@users.noreply.github.com> Date: Sun, 16 Mar 2025 23:49:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=96=B5=E5=91=9C~?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/api/Chat.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 server/api/Chat.js diff --git a/server/api/Chat.js b/server/api/Chat.js new file mode 100644 index 0000000..a53a4cc --- /dev/null +++ b/server/api/Chat.js @@ -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() { + + } +}