From de11803cea3b5d81d2b084aafab4c3489aed62fa Mon Sep 17 00:00:00 2001 From: rainbow napkin Date: Wed, 10 Sep 2025 03:43:15 -0400 Subject: [PATCH] Fixed !clear command to clear server-side chatBuffer as well as client buffers. --- src/app/channel/chatBuffer.js | 47 +++++++++++++++++++++++++++++++--- src/app/channel/chatHandler.js | 4 +++ src/utils/loggerUtils.js | 5 +++- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/app/channel/chatBuffer.js b/src/app/channel/chatBuffer.js index 6968e36..3a4e466 100644 --- a/src/app/channel/chatBuffer.js +++ b/src/app/channel/chatBuffer.js @@ -118,16 +118,55 @@ class chatBuffer{ } /** - * Saves RAM-Based buffer to Channel Document in DB - * @param {String} reason - Reason for DB save, formatted as 'x minutes/seconds of in/activity', used for logging purposes - * @param {Mongoose.Document} chanDB - Channel Doc to work with, can be left empty for method to auto-find through channel name. + * Clears out buffer timers to prevent saving */ - async saveDB(reason, chanDB){ + clearTimers(){ //clear existing timers clearTimeout(this.inactivityTimer); clearTimeout(this.busyTimer); this.inactivityTimer = null; this.busyTimer = null; + } + + /** + * Clears RAM-Based chat buffer and saves the result to DB + * @param {String} name - Name of user to clear chats from. Left as null or an empty string, it will clear the entire buffer. + */ + async clearBuffer(name){ + //Clear out DB Timers + this.clearTimers(); + + let reason = "clearing chat"; + + //If we have a null or empty string passed as name + if(name == null || name == ""){ + //Nuke that fcker + this.buffer = []; + //Otherwise + }else{ + reason = `clearing ${name}'s chats` + + //Iterate through chat buffer by index + for(let chatIndex in this.buffer){ + //If the current chat we're looking at was submitted by the given user + if(this.buffer[chatIndex].user.toLowerCase() == name.toLowerCase()){ + //Splice that fcker out + this.buffer.splice(chatIndex, 1); + } + } + } + + await this.saveDB(reason); + } + + /** + * Saves RAM-Based buffer to Channel Document in DB + * @param {String} reason - Reason for DB save, formatted as 'x minutes/seconds of in/activity', used for logging purposes + * @param {Mongoose.Document} chanDB - Channel Doc to work with, can be left empty for method to auto-find through channel name. + */ + async saveDB(reason, chanDB){ + //Clear out DB Timers + this.clearTimers(); //if the server is in screamy boi mode if(config.verbose){ diff --git a/src/app/channel/chatHandler.js b/src/app/channel/chatHandler.js index e1d2f9b..4e4c14f 100644 --- a/src/app/channel/chatHandler.js +++ b/src/app/channel/chatHandler.js @@ -334,7 +334,11 @@ class chatHandler{ //If no user was entered OR the user was found if(user == null || target != null){ + //Send command out to browsers to drop chats from buffer this.server.io.in(chan).emit("clearChat", {user}); + + //Clear serverside buffer, down to the DB + activeChan.chatBuffer.clearBuffer(user); } } } diff --git a/src/utils/loggerUtils.js b/src/utils/loggerUtils.js index 46443af..4c6b2cf 100644 --- a/src/utils/loggerUtils.js +++ b/src/utils/loggerUtils.js @@ -176,8 +176,11 @@ module.exports.errorMiddleware = function(err, req, res, next){ module.exports.dumpError = function(err, date = new Date()){ try{ const content = `Error Date: ${date.toLocaleString()} (UTC-${date.getTimezoneOffset()/60})\nError Type: ${err.name}\nError Msg:${err.message}\nStack Trace:\n\n${err.stack}`; + const path = `log/crash/${date.getTime()}.log`; - fs.writeFile(`log/crash/${date.getTime()}.log`, content); + fs.writeFile(path, content); + + module.exports.consoleWarn(`Warning: Unexpected Server Crash gracefully dumped to '${path}'... SOMETHING MAY BE VERY BROKEN!!!!`); }catch(doubleErr){ module.exports.consoleWarn("Yo Dawg, I herd you like errors, so I put an error in your error dump, so you can dump while you dump:"); module.exports.consoleWarn(err);