Basic chat UI complete.
This commit is contained in:
parent
f109314163
commit
e81a4c0973
11 changed files with 393 additions and 71 deletions
|
|
@ -36,7 +36,7 @@ class pmHandler{
|
|||
/**
|
||||
* List of PM Sessions
|
||||
*/
|
||||
this.seshList = [];
|
||||
this.seshList = new Map();
|
||||
|
||||
this.defineListeners();
|
||||
this.setupInput();
|
||||
|
|
@ -66,50 +66,63 @@ class pmHandler{
|
|||
//Store whether or not current message has been consumed by an existing sesh
|
||||
let consumed = false;
|
||||
|
||||
const nameObj = pmHandler.genSeshName(data);
|
||||
|
||||
//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);
|
||||
}
|
||||
const members = nameObj.recipients;
|
||||
|
||||
//For each existing sesh
|
||||
for(let seshIndex in this.seshList){
|
||||
//Get current sesh
|
||||
const sesh = this.seshList[seshIndex];
|
||||
for(const seshEntry of this.seshList){
|
||||
//Pull sesh object from map entry
|
||||
const sesh = seshEntry[1];
|
||||
|
||||
//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 == 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*/
|
||||
//If currently checked sesh ID matches calculated message sesh id
|
||||
if(sesh.id == nameObj.name){
|
||||
//Dump collected message into the matching session
|
||||
sesh.messages.push(data);
|
||||
|
||||
//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() == members.sort().join()){
|
||||
//Dump collected message into the matching session
|
||||
this.seshList[seshIndex].messages.push(data);
|
||||
//Add sesh to sesh map
|
||||
this.seshList.set(sesh.id, sesh);
|
||||
|
||||
//Let the rest of the method know that we've consumed this message
|
||||
consumed = true;
|
||||
}
|
||||
//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, client));
|
||||
//Generate a new sesh
|
||||
const sesh = new pmSesh(data, client);
|
||||
|
||||
//Add it to the sesh list
|
||||
this.seshList.set(sesh.id, sesh);
|
||||
}
|
||||
}
|
||||
|
||||
static genSeshName(message){
|
||||
const recipients = [];
|
||||
|
||||
//Manually iterate through recipients
|
||||
for(const member of message.recipients){
|
||||
//check to make sure we're not adding ourselves
|
||||
if(member != client.user.user){
|
||||
//Copy relevant array members by value instead of reference
|
||||
recipients.push(member);
|
||||
}
|
||||
}
|
||||
|
||||
//If this wasn't our message
|
||||
if(message.sender != client.user.user){
|
||||
//Push sender onto members list
|
||||
recipients.push(message.sender);
|
||||
}
|
||||
|
||||
//Sort recipients
|
||||
recipients.sort();
|
||||
|
||||
return {
|
||||
name: recipients.join(', '),
|
||||
recipients
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -128,29 +141,21 @@ class pmSesh{
|
|||
*/
|
||||
this.client = client;
|
||||
|
||||
const nameObj = pmHandler.genSeshName(message);
|
||||
|
||||
/**
|
||||
* Members of session excluding the currently logged in user
|
||||
*/
|
||||
this.recipients = [];
|
||||
this.recipients = nameObj.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);
|
||||
}
|
||||
/**
|
||||
* Name of the chat sesh, named after out-going recipients
|
||||
*/
|
||||
this.id = nameObj.name;
|
||||
|
||||
/**
|
||||
* Array containing all session messages
|
||||
*/
|
||||
this.messages = [message];
|
||||
this.messages = (message.msg == "" || message.msg == null) ? [] : [message];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue