Skip to content

Commit

Permalink
Chat: Make collection management mor robust
Browse files Browse the repository at this point in the history
  • Loading branch information
mjansenDatabay committed Mar 27, 2020
1 parent 48e19c2 commit 9ed74ae
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 45 deletions.
69 changes: 37 additions & 32 deletions Modules/Chatroom/chat/Model/Conversation.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ var Conversation = function Conversation(id, participants)
return _id;
};

/**
*
* @param participants
* @returns {boolean}
*/
this.matchesParticipants = function(participants) {
for(var index in _participants)
{
if(_participants.hasOwnProperty(index) && !hasParticipant(_participants[index], participants))
{
for (let index in _participants) {
if (_participants.hasOwnProperty(index) && !hasParticipant(_participants[index], participants)) {
return false;
}
}
Expand Down Expand Up @@ -108,8 +111,8 @@ var Conversation = function Conversation(id, participants)
};

this.removeParticipant = function(participant) {
var participantIndex = getParticipantIndex(participant, _participants);
if (participantIndex !== false) {
let participantIndex = getParticipantIndex(participant, _participants);
if (participantIndex !== -1) {
_participants.splice(participantIndex, 1);
participant.removeConversation(this);
}
Expand Down Expand Up @@ -159,39 +162,41 @@ var Conversation = function Conversation(id, participants)
}
}

/**
*
* @param participant
* @param participants
* @returns {number}
*/
function getParticipantIndex(participant, participants) {
for (var key in participants) {
if (participants.hasOwnProperty(key)) {
var id = participants[key].id;

if (typeof participants[key].getId === 'function'){
id = participants[key].getId();
}
const index = participants.findIndex(function(val) {
if (null === val) {
return false;
}

if (id == participant.getId()) {
return key;
}
let id = val.id;
if (typeof val.getId === 'function') {
id = val.getId();
}
}
return false;
}

function hasParticipant(participant, participants) {
for(var key in participants) {
if(participants.hasOwnProperty(key)) {
var id = participants[key].id;
if (id.toString() === participant.getId().toString()) {
return true;
}

if(typeof participants[key].getId === 'function'){
id = participants[key].getId();
}
return false;
});

if(id > 0 && id == participant.getId()) {
return true;
}
}
return index;
}

}
return false;
/**
*
* @param participant
* @param participants
* @returns {boolean}
*/
function hasParticipant(participant, participants) {
return getParticipantIndex(participant, participants) !== -1;
}
};

Expand Down
5 changes: 3 additions & 2 deletions Modules/Chatroom/chat/Model/ConversationCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ var ConversationCollection = function ConversationCollection() {
};

this.getForParticipants = function(participants) {
for (var id in _collection) {
for (let id in _collection) {
if (_collection.hasOwnProperty(id)) {
var conversation = _collection[id];
let conversation = _collection[id];

if (conversation.matchesParticipants(participants)) {
return conversation;
}
}
}

return null;
};
};
Expand Down
27 changes: 16 additions & 11 deletions Modules/Chatroom/chat/Model/ConversationParticipant.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ function Participant(id, name) {
};

this.removeConversation = function(conversation) {
var conversationIndex = getConversationIndex(conversation, _conversations);
if (conversationIndex !== false) {
let conversationIndex = getConversationIndex(conversation, _conversations);
if (conversationIndex !== -1) {
_conversations.splice(conversationIndex, 1);
}
};
Expand All @@ -170,17 +170,22 @@ function Participant(id, name) {
return _conversations;
};

/**
*
* @param conversation
* @param conversations
* @returns {number}
*/
function getConversationIndex(conversation, conversations) {
for (var key in conversations) {
if (conversations.hasOwnProperty(key)) {
var id = conversations[key].getId();

if (id == conversation.getId()) {
return key;
}
const index = conversations.findIndex(function(val) {
if (null === val) {
return false;
}
}
return false;

return val.getId().toString() === conversation.getId().toString();
});

return index;
}

/**
Expand Down

0 comments on commit 9ed74ae

Please sign in to comment.