Starting work on client-side chat sesh handling

This commit is contained in:
rainbow napkin 2025-09-30 03:25:15 -04:00
parent e19ae74412
commit d8e5c64c13

View file

@ -15,23 +15,100 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
/**
* 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];
}
}