From fadb92e544c6f8a975c51e690c438e1dc7b254b0 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 3 Feb 2024 22:04:53 -0300 Subject: [PATCH] Chat History added. --- src/channel/Channel.ts | 25 +++++++++++-------------- src/data/history.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/channel/Channel.ts b/src/channel/Channel.ts index 0602515..8c35687 100644 --- a/src/channel/Channel.ts +++ b/src/channel/Channel.ts @@ -14,6 +14,7 @@ import { findSocketByPartID, socketsBySocketID } from "../ws/Socket"; import Crown from "./Crown"; import { ChannelList } from "./ChannelList"; import { config } from "./config"; +import { saveChatHistory, getChatHistory } from "../data/history"; import { prisma } from "../data/prisma"; interface CachedKickban { @@ -25,9 +26,13 @@ interface CachedKickban { export class Channel extends EventEmitter { private settings: Partial = config.defaultSettings; private ppl = new Array(); + public chatHistory = new Array(); + private async loadChatHistory() { + this.chatHistory = await getChatHistory(this.getID()); + } + public logger: Logger; - public chatHistory = new Array(); public bans = new Array(); public crown?: Crown; @@ -41,6 +46,7 @@ export class Channel extends EventEmitter { ) { super(); + this.logger = new Logger("Channel - " + _id); // Validate settings in set @@ -83,6 +89,8 @@ export class Channel extends EventEmitter { private bindEventListeners() { if (this.alreadyBound) return; this.alreadyBound = true; + this.loadChatHistory() + this.logger.info("Loaded Chat History."); this.on("update", () => { // Send updated info @@ -102,7 +110,7 @@ export class Channel extends EventEmitter { } }); - this.on("message", (msg: ServerEvents["a"], socket: Socket) => { + this.on("message", async (msg: ServerEvents["a"], socket: Socket) => { if (!msg.message) return; const userFlags = socket.getUserFlags(); @@ -126,19 +134,8 @@ export class Channel extends EventEmitter { this.sendArray([outgoing]); this.chatHistory.push(outgoing); + await saveChatHistory(this.getID(), this.chatHistory) - prisma.chatHistory.upsert({ - where:{ - id: this._id - }, - update:{ - messages: JSON.stringify(this.chatHistory) - }, - create: { - id:this._id, - messages: JSON.stringify(this.chatHistory) - } - }) try { if (msg.message.startsWith("/")) { this.emit("command", msg, socket); diff --git a/src/data/history.ts b/src/data/history.ts index 2e9297c..89926c7 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -1 +1,32 @@ // TODO Chat history +import { prisma } from "./prisma"; + +export async function saveChatHistory( + _id: string, + chatHistory: object +) { + await prisma.chatHistory.upsert({ + where:{ + id: _id + }, + update:{ + messages: JSON.stringify(chatHistory) + }, + create: { + id:_id, + messages: JSON.stringify(chatHistory) + } + }) +} + + +export async function getChatHistory( + _id: string +) { + const history = await prisma.chatHistory.findFirst({where:{id:_id}}) + if(!history) { + return []; + } else { + return JSON.parse(await history.messages); + } +} \ No newline at end of file