/*Canopy - The next generation of stoner streaming software Copyright (C) 2024-2025 Rainbownapkin and the TTN Community This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 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 .*/ /** * 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]; } }