From d8e5c64c139ac524f236a2632639a7b07705075a Mon Sep 17 00:00:00 2001 From: rainbow napkin Date: Tue, 30 Sep 2025 03:25:15 -0400 Subject: [PATCH] Starting work on client-side chat sesh handling --- www/js/channel/pmHandler.js | 81 ++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/www/js/channel/pmHandler.js b/www/js/channel/pmHandler.js index 956bd57..c26f47c 100644 --- a/www/js/channel/pmHandler.js +++ b/www/js/channel/pmHandler.js @@ -15,23 +15,100 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see .*/ /** - * Class for handling incoming Private Messages + * Class for handling incoming Private Messages for the entire client */ class pmHandler{ + /** + * Instantiates a new Private Message Handler object + * @param {channel} client - Parent client Management Object + */ constructor(client){ + /** + * Parent client management object + */ this.client = client; + /** + * PM Icon in the main chat bar + */ this.pmIcon = document.querySelector('#chat-panel-pm-icon'); + /** + * List of PM Sessions + */ + this.seshList = []; + this.defineListeners(); this.setupInput(); } + /** + * Defines network related event listeners for PM Handler + */ defineListeners(){ - + this.client.pmSocket.on("message", this.handlePM.bind(this)); + this.client.pmSocket.on("sent", this.handlePM.bind(this)); } + /** + * Defines inpet related event listeners for PM handler + */ setupInput(){ this.pmIcon.addEventListener("click", ()=>{this.client.cPanel.setActivePanel(new pmPanel(client))}); } + + /** + * Handles received Private Messages from the PM service on the server, organizing it into the proper session from the sesh list + * Or creating a new sesh where a matching one does not a exist + * @param {object} data - Private Message data from the server + */ + handlePM(data){ + //Store whether or not current message has been consumed by an existing sesh + let consumed = false; + + //For each existing sesh + for(let seshIndex in this.seshList){ + //Get current sesh + const sesh = this.seshList[seshIndex]; + + //Check to see if the length of sesh recipients equals current length (only check on arrays that actually make sense to save time) + if(sesh.recipients.length == data.recipients.length){ + /*Feels like cheating to have the JS engine to the hard bits by just telling it to sort them. + That being said, since the function is implemented into the JS Engine itself + It will be quicker than any custom comparison code we can write*/ + + //Sort recipient lists so lists with the same user will be equal when joined together in a string and compare, if they're the same... + if(sesh.recipients.sort().join() == data.recipients.sort().join()){ + //Dump collected message into the matching session + this.seshList[seshIndex].messages.push(data); + + //Let the rest of the method know that we've consumed this message + consumed = true; + } + } + } + + //If we made it through the loop without consuming the message + if(!consumed){ + //Add it to it's own fresh new sesh + this.seshList.push(new pmSesh(data)); + } + } +} + +/** + * Class which represents an existing Private Messaging session between two or more users + */ +class pmSesh{ + /** + * Instatiates a new pmSession object + * @param {Object} message - Initial Private Message object from server that initiated the session + */ + constructor(message){ + //Add recipients from message + this.recipients = message.recipients; + + //Add message to messages array + this.messages = [message]; + } } \ No newline at end of file