Merge branch 'MDL-65134-master' of git://github.com/peterRd/moodle
authorJake Dallimore <jake@moodle.com>
Fri, 3 May 2019 02:05:26 +0000 (10:05 +0800)
committerJake Dallimore <jake@moodle.com>
Fri, 3 May 2019 02:05:26 +0000 (10:05 +0800)
message/amd/build/message_drawer_view_conversation.min.js
message/amd/build/message_drawer_view_overview_section.min.js
message/amd/src/message_drawer_view_conversation.js
message/amd/src/message_drawer_view_overview_section.js

index 21837f5..8db9ea6 100644 (file)
Binary files a/message/amd/build/message_drawer_view_conversation.min.js and b/message/amd/build/message_drawer_view_conversation.min.js differ
index 7fb488b..2e99925 100644 (file)
Binary files a/message/amd/build/message_drawer_view_overview_section.min.js and b/message/amd/build/message_drawer_view_overview_section.min.js differ
index 1b4050b..899c188 100644 (file)
@@ -252,13 +252,13 @@ function(
             messages: state.messages.map(function(message) {
                 return $.extend({}, message);
             }),
-            members: Object.keys(state.members).reduce(function(carry, id) {
-                carry[id] = $.extend({}, state.members[id]);
-                carry[id].contactrequests = state.members[id].contactrequests.map(function(request) {
+            members: Object.keys(state.members).map(function(id) {
+                var formattedMember = $.extend({}, state.members[id]);
+                formattedMember.contactrequests = state.members[id].contactrequests.map(function(request) {
                     return $.extend({}, request);
                 });
-                return carry;
-            }, {})
+                return formattedMember;
+            })
         };
     };
 
index eafd6ca..a675ab6 100644 (file)
@@ -158,6 +158,39 @@ function(
         }
     };
 
+    /**
+     * Create a formatted conversation object from the the one we get from events. The new object
+     * will be in a format that matches what we receive from the server.
+     *
+     * @param {Object} conversation
+     * @return {Object} formatted conversation.
+     */
+    var formatConversationFromEvent = function(conversation) {
+        // Recursively lowercase all of the keys for an object.
+        var recursivelyLowercaseKeys = function(object) {
+            return Object.keys(object).reduce(function(carry, key) {
+                if ($.isArray(object[key])) {
+                    carry[key.toLowerCase()] = object[key].map(recursivelyLowercaseKeys);
+                } else {
+                    carry[key.toLowerCase()] = object[key];
+                }
+
+                return carry;
+            }, {});
+        };
+
+        // Recursively lowercase all of the keys for the conversation.
+        var formatted = recursivelyLowercaseKeys(conversation);
+
+        // Make sure all messages have the useridfrom property set.
+        formatted.messages = formatted.messages.map(function(message) {
+            message.useridfrom = message.userfrom.id;
+            return message;
+        });
+
+        return formatted;
+    };
+
     /**
      * Render the messages in the overview page.
      *
@@ -166,7 +199,7 @@ function(
      * @param {Number} userId Logged in user id.
      * @return {Object} jQuery promise.
      */
-    var render = function(contentContainer, conversations, userId) {
+    var render = function(conversations, userId) {
         var formattedConversations = conversations.map(function(conversation) {
 
             var lastMessage = conversation.messages.length ? conversation.messages[conversation.messages.length - 1] : null;
@@ -206,7 +239,7 @@ function(
 
             if (conversation.type == MessageDrawerViewConversationContants.CONVERSATION_TYPES.PUBLIC) {
                 formattedConversation.lastsendername = conversation.members.reduce(function(carry, member) {
-                    if (!carry && member.id == lastMessage.useridfrom) {
+                    if (!carry && lastMessage && member.id == lastMessage.useridfrom) {
                         carry = member.fullname;
                     }
                     return carry;
@@ -222,12 +255,7 @@ function(
             }
         });
 
-        return Templates.render(TEMPLATES.CONVERSATIONS_LIST, {conversations: formattedConversations})
-            .then(function(html) {
-                contentContainer.append(html);
-                return html;
-            })
-            .catch(Notification.exception);
+        return Templates.render(TEMPLATES.CONVERSATIONS_LIST, {conversations: formattedConversations});
     };
 
     /**
@@ -415,61 +443,16 @@ function(
         conversationElement.find(SELECTORS.BLOCKED_ICON_CONTAINER).addClass('hidden');
     };
 
-    /**
-     * Update the last message from / to a contact.
-     *
-     * @param  {Object} element Conversation element.
-     * @param  {Object} conversation The conversation.
-     * @return {Object} jQuery promise
-     */
-    var updateLastMessage = function(element, conversation) {
-        var message = conversation.messages[conversation.messages.length - 1];
-        var senderString = '';
-        var senderStringRequest;
-        if (message.fromLoggedInUser) {
-            senderStringRequest = {key: 'you', component: 'core_message'};
-        } else {
-            senderStringRequest = {key: 'sender', component: 'core_message', param: message.userFrom.fullname};
-        }
-
-        var stringRequests = [
-            senderStringRequest,
-            {key: 'strftimetime24', component: 'core_langconfig'},
-        ];
-        return Str.get_strings(stringRequests)
-            .then(function(strings) {
-                senderString = strings[0];
-                return UserDate.get([{timestamp: message.timeCreated, format: strings[1]}]);
-            })
-            .then(function(dates) {
-                return dates[0];
-            })
-            .then(function(dateString) {
-                element.find(SELECTORS.LAST_MESSAGE_DATE).text(dateString).removeClass('hidden');
-
-                // No need to show sender string for private conversations and where the last message didn't come from you.
-                if (!message.fromLoggedInUser &&
-                        conversation.type === MessageDrawerViewConversationContants.CONVERSATION_TYPES.PRIVATE) {
-                    senderString = '';
-                }
-
-                // Now load the last message.
-                var lastMessage = senderString + " <span class='text-muted'>" + $(message.text).text() + "</span>";
-
-                return element.find(SELECTORS.LAST_MESSAGE).html(lastMessage);
-            });
-    };
-
     /**
      * Create an render new conversation element in the list of conversations.
      *
      * @param  {Object} root Overview messages container element.
      * @param  {Object} conversation The conversation.
+     * @param  {Number} userId The logged in user id.
      * @return {Object} jQuery promise
      */
-    var createNewConversation = function(root, conversation) {
+    var createNewConversationFromEvent = function(root, conversation, userId) {
         var existingConversations = root.find(SELECTORS.CONVERSATION);
-        var text = '';
 
         if (!existingConversations.length) {
             // If we didn't have any conversations then we need to show
@@ -479,31 +462,10 @@ function(
             LazyLoadList.hideEmptyMessage(listRoot);
         }
 
-        var messageCount = conversation.messages.length;
-        var lastMessage = messageCount ? conversation.messages[messageCount - 1] : null;
-
-        if (lastMessage) {
-            text = $(lastMessage.text).text() || lastMessage.text;
-        }
-
-        var formattedConversation = {
-            id: conversation.id,
-            name: conversation.name,
-            subname: conversation.subname,
-            lastmessagedate: lastMessage ? lastMessage.timeCreated : null,
-            sentfromcurrentuser: lastMessage ? lastMessage.fromLoggedInUser : null,
-            lastmessage: text,
-            imageurl: conversation.imageUrl,
-        };
-
         // Cache the conversation.
         loadedConversationsById[conversation.id] = conversation;
 
-        if (new Date().toDateString() == new Date(formattedConversation.lastmessagedate * 1000).toDateString()) {
-            formattedConversation.istoday = true;
-        }
-
-        return Templates.render(TEMPLATES.CONVERSATIONS_LIST, {conversations: [formattedConversation]})
+        return render([conversation], userId)
             .then(function(html) {
                 var contentContainer = LazyLoadList.getContentContainer(root);
                 return contentContainer.prepend(html);
@@ -583,7 +545,14 @@ function(
 
         root.on('show.bs.collapse', function() {
             setExpanded(root);
-            LazyLoadList.show(listRoot, loadCallback, render);
+            LazyLoadList.show(listRoot, loadCallback, function(contentContainer, conversations, userId) {
+                return render(conversations, userId)
+                    .then(function(html) {
+                        contentContainer.append(html);
+                        return html;
+                    })
+                    .catch(Notification.exception);
+            });
         });
 
         root.on('hidden.bs.collapse', function() {
@@ -626,17 +595,27 @@ function(
                 return;
             }
 
+            var loggedInUserId = conversation.loggedInUserId;
             var conversationId = conversation.id;
             var element = getConversationElement(root, conversationId);
+            conversation = formatConversationFromEvent(conversation);
             if (element.length) {
-                updateLastMessage(element, conversation);
+                var contentContainer = LazyLoadList.getContentContainer(root);
+                render([conversation], loggedInUserId)
+                    .then(function(html) {
+                            contentContainer.prepend(html);
+                            element.remove();
+                            return html;
+                        })
+                    .catch(Notification.exception);
             } else {
-                createNewConversation(root, conversation);
+                createNewConversationFromEvent(root, conversation, loggedInUserId);
             }
         });
 
         PubSub.subscribe(MessageDrawerEvents.CONVERSATION_DELETED, function(conversationId) {
             var conversationElement = getConversationElement(root, conversationId);
+            delete loadedConversationsById[conversationId];
             if (conversationElement.length) {
                 deleteConversation(root, conversationElement);
             }
@@ -654,7 +633,11 @@ function(
             if (conversationBelongsToThisSection(conversation)) {
                 conversationElement = getConversationElement(root, conversation.id);
                 if (!conversationElement.length) {
-                    createNewConversation(root, conversation);
+                    createNewConversationFromEvent(
+                        root,
+                        formatConversationFromEvent(conversation),
+                        conversation.loggedInUserId
+                    );
                 }
             } else {
                 conversationElement = getConversationElement(root, conversation.id);
@@ -669,7 +652,11 @@ function(
             if (conversationBelongsToThisSection(conversation)) {
                 conversationElement = getConversationElement(root, conversation.id);
                 if (!conversationElement.length) {
-                    createNewConversation(root, conversation);
+                    createNewConversationFromEvent(
+                        root,
+                        formatConversationFromEvent(conversation),
+                        conversation.loggedInUserId
+                    );
                 }
             } else {
                 conversationElement = getConversationElement(root, conversation.id);
@@ -714,7 +701,14 @@ function(
             if (isVisible(root)) {
                 setExpanded(root);
                 var listRoot = LazyLoadList.getRoot(root);
-                LazyLoadList.show(listRoot, loadCallback, render);
+                LazyLoadList.show(listRoot, loadCallback, function(contentContainer, conversations, userId) {
+                    return render(conversations, userId)
+                        .then(function(html) {
+                            contentContainer.append(html);
+                            return html;
+                        })
+                        .catch(Notification.exception);
+                });
             }
 
             // This is given to us by the calling code because the total counts for all sections