From 9ed74ae9ba1c5b2cd25fbcc53555e034a9fb24e6 Mon Sep 17 00:00:00 2001 From: mjansen Date: Fri, 27 Mar 2020 16:28:31 +0100 Subject: [PATCH] Chat: Make collection management mor robust --- Modules/Chatroom/chat/Model/Conversation.js | 69 ++++++++++--------- .../chat/Model/ConversationCollection.js | 5 +- .../chat/Model/ConversationParticipant.js | 27 +++++--- 3 files changed, 56 insertions(+), 45 deletions(-) diff --git a/Modules/Chatroom/chat/Model/Conversation.js b/Modules/Chatroom/chat/Model/Conversation.js index 67c0dddfc302..623f5d86f407 100644 --- a/Modules/Chatroom/chat/Model/Conversation.js +++ b/Modules/Chatroom/chat/Model/Conversation.js @@ -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; } } @@ -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); } @@ -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; } }; diff --git a/Modules/Chatroom/chat/Model/ConversationCollection.js b/Modules/Chatroom/chat/Model/ConversationCollection.js index 352a98bf26b9..cab5722b0fcc 100644 --- a/Modules/Chatroom/chat/Model/ConversationCollection.js +++ b/Modules/Chatroom/chat/Model/ConversationCollection.js @@ -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; }; }; diff --git a/Modules/Chatroom/chat/Model/ConversationParticipant.js b/Modules/Chatroom/chat/Model/ConversationParticipant.js index f081deab4a16..ea551c4455d3 100644 --- a/Modules/Chatroom/chat/Model/ConversationParticipant.js +++ b/Modules/Chatroom/chat/Model/ConversationParticipant.js @@ -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); } }; @@ -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; } /**