From 0ed1c0dd8980d4aeda17c80e2e68fece8063449c Mon Sep 17 00:00:00 2001 From: rainbow napkin Date: Thu, 2 Oct 2025 03:38:40 -0400 Subject: [PATCH] More work decoupling chatPreprocessor from src/app/channel. --- src/app/channel/chatHandler.js | 21 +++++++++++--- ...andPreprocessor.js => commandProcessor.js} | 28 +------------------ src/app/chatPreprocessor.js | 21 +++----------- src/schemas/tokebot/tokeCommandSchema.js | 4 +-- 4 files changed, 24 insertions(+), 50 deletions(-) rename src/app/channel/{commandPreprocessor.js => commandProcessor.js} (92%) diff --git a/src/app/channel/chatHandler.js b/src/app/channel/chatHandler.js index 4e4c14f..df21492 100644 --- a/src/app/channel/chatHandler.js +++ b/src/app/channel/chatHandler.js @@ -18,7 +18,9 @@ along with this program. If not, see .*/ const validator = require('validator') //local imports -const commandPreprocessor = require('./commandPreprocessor'); +const chatPreprocessor = require('../chatPreprocessor'); +const commandProcessor = require('./commandProcessor'); +const tokebot = require('./tokebot'); const loggerUtils = require('../../utils/loggerUtils'); const linkUtils = require('../../utils/linkUtils'); const emoteValidator = require('../../validators/emoteValidator'); @@ -42,7 +44,11 @@ class chatHandler{ /** * Child Command Pre-Processor Object */ - this.commandPreprocessor = new commandPreprocessor(server, this) + this.chatPreprocessor = new chatPreprocessor( + server, + new commandProcessor(server, this), + new tokebot(server, this) + ); /** * Max chat buffer message count @@ -67,8 +73,15 @@ class chatHandler{ * @param {Socket} socket - Socket we're receiving the request from * @param {Object} data - Event payload */ - handleChat(socket, data){ - this.commandPreprocessor.preprocess(socket, data); + async handleChat(socket, data){ + //Preprocess chat data + const preprocessedChat = await this.chatPreprocessor.preprocess(socket, data); + + //If send flag wasn't set to false + if(preprocessedChat != false){ + //Send that shit! + this.relayUserChat(socket, preprocessedChat.message, preprocessedChat.chatType, preprocessedChat.links); + } } /** diff --git a/src/app/channel/commandPreprocessor.js b/src/app/channel/commandProcessor.js similarity index 92% rename from src/app/channel/commandPreprocessor.js rename to src/app/channel/commandProcessor.js index c08c653..213ec1c 100644 --- a/src/app/channel/commandPreprocessor.js +++ b/src/app/channel/commandProcessor.js @@ -14,36 +14,10 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see .*/ -//NPM Imports -const validator = require('validator');//No express here, so regular validator it is! - //Local Imports -const chatPreprocessor = require('../chatPreprocessor'); -const tokebot = require('./tokebot'); -const linkUtils = require('../../utils/linkUtils'); const permissionModel = require('../../schemas/permissionSchema'); const channelModel = require('../../schemas/channel/channelSchema'); -/** - * Class containing global server-side chat/command pre-processing logic - */ -class commandPreprocessor extends chatPreprocessor{ - /** - * Instantiates a commandPreprocessor object - * @param {channelManager} server - Parent Server Object - * @param {chatHandler} chatHandler - Parent Chat Handler Object - */ - constructor(server, chatHandler){ - //Call derived constructor - super( - server, - chatHandler, - new commandProcessor(server, chatHandler), - new tokebot(server, chatHandler) - ); - } -} - /** * Class representing global server-side chat/command processing logic */ @@ -317,4 +291,4 @@ class commandProcessor{ } } -module.exports = commandPreprocessor; \ No newline at end of file +module.exports = commandProcessor; \ No newline at end of file diff --git a/src/app/chatPreprocessor.js b/src/app/chatPreprocessor.js index 43e77fc..cbd1cea 100644 --- a/src/app/chatPreprocessor.js +++ b/src/app/chatPreprocessor.js @@ -27,19 +27,13 @@ class chatPreprocessor{ /** * Instantiates a commandPreprocessor object * @param {channelManager} server - Parent Server Object - * @param {chatHandler} chatHandler - Parent Chat Handler Object */ - constructor(server, chatHandler, commandProcessor, tokebot){ + constructor(server, commandProcessor, tokebot){ /** * Parent Server Object */ this.server = server; - /** - * Parent Chat Handler Object - */ - this.chatHandler = chatHandler; - /** * Child Command Processor Object. Contains functions named after commands. */ @@ -82,8 +76,10 @@ class chatPreprocessor{ await this.prepMessage(commandObj); //Send the chat - this.sendChat(commandObj); + return commandObj; } + + return false; } /** @@ -157,15 +153,6 @@ class chatPreprocessor{ //Validate links and mark them by embed type await this.markLinks(commandObj); } - - /** - * Relays chat to channel via parent Chat Handler object - * @param {Object} commandObj - Object representing a single given command/chat request - */ - sendChat(commandObj){ - //FUCKIN' SEND IT! - this.chatHandler.relayUserChat(commandObj.socket, commandObj.message, commandObj.chatType, commandObj.links); - } } module.exports = chatPreprocessor; \ No newline at end of file diff --git a/src/schemas/tokebot/tokeCommandSchema.js b/src/schemas/tokebot/tokeCommandSchema.js index a6e9715..892b7d0 100644 --- a/src/schemas/tokebot/tokeCommandSchema.js +++ b/src/schemas/tokebot/tokeCommandSchema.js @@ -39,7 +39,7 @@ tokeCommandSchema.pre('save', async function (next){ //if the command was changed if(this.isModified("command")){ //Get server tokebot object - const tokebot = server.channelManager.chatHandler.commandPreprocessor.tokebot; + const tokebot = server.channelManager.chatHandler.chatPreprocessor.tokebot; //If tokebot is up and running if(tokebot != null && tokebot.tokeCommands != null){ @@ -58,7 +58,7 @@ tokeCommandSchema.pre('save', async function (next){ */ tokeCommandSchema.pre('deleteOne', {document: true}, async function (next){ //Get server tokebot object (isn't this a fun dot crawler? Why hasn't anyone asked me to stop writing software yet?) - const tokebot = server.channelManager.chatHandler.commandPreprocessor.tokebot; + const tokebot = server.channelManager.chatHandler.chatPreprocessor.tokebot; //Get the index of the command within tokeCommand and splice it out tokebot.tokeCommands.splice(tokebot.tokeCommands.indexOf(this.command),1);