Improved Private Message Session Management
This commit is contained in:
parent
d8e5c64c13
commit
e2020406a7
|
|
@ -66,19 +66,37 @@ class pmHandler{
|
|||
//Store whether or not current message has been consumed by an existing sesh
|
||||
let consumed = false;
|
||||
|
||||
//Create members array from scratch to avoid changing the input data for further processing
|
||||
const members = [];
|
||||
|
||||
//Manually iterate through recipients
|
||||
for(const member of data.recipients){
|
||||
//check to make sure we're not adding ourselves
|
||||
if(member != this.client.user.user){
|
||||
//Copy relevant array members by value instead of reference
|
||||
members.push(member);
|
||||
}
|
||||
}
|
||||
|
||||
//If this wasn't our message
|
||||
if(data.sender != this.client.user.user){
|
||||
//Push sender onto members list
|
||||
members.push(data.sender);
|
||||
}
|
||||
|
||||
//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){
|
||||
if(sesh.recipients.length == members.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()){
|
||||
if(sesh.recipients.sort().join() == members.sort().join()){
|
||||
//Dump collected message into the matching session
|
||||
this.seshList[seshIndex].messages.push(data);
|
||||
|
||||
|
|
@ -91,7 +109,7 @@ class pmHandler{
|
|||
//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));
|
||||
this.seshList.push(new pmSesh(data, client));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -104,11 +122,35 @@ 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;
|
||||
constructor(message, client){
|
||||
/**
|
||||
* Parent client management object
|
||||
*/
|
||||
this.client = client;
|
||||
|
||||
//Add message to messages array
|
||||
/**
|
||||
* Members of session excluding the currently logged in user
|
||||
*/
|
||||
this.recipients = [];
|
||||
|
||||
//Manually iterate through recipients
|
||||
for(const member of message.recipients){
|
||||
//check to make sure we're not adding ourselves
|
||||
if(member != this.client.user.user){
|
||||
//Copy relevant array members by value instead of reference
|
||||
this.recipients.push(member);
|
||||
}
|
||||
}
|
||||
|
||||
//If this wasn't our message
|
||||
if(message.sender != this.client.user.user){
|
||||
//Push sender onto members list
|
||||
this.recipients.push(message.sender);
|
||||
}
|
||||
|
||||
/**
|
||||
* Array containing all session messages
|
||||
*/
|
||||
this.messages = [message];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue