MDL-65134 core_message: Rerender the message with the updated last msg

* When a new last message we need to reorder the messages within the
message list. Call render for this.
* Standardize the cached items
* Delete cached entries on clear
This commit is contained in:
Peter 2019-04-12 12:45:39 +08:00
parent e145874b8b
commit d4b1eee6ee
4 changed files with 54 additions and 64 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View 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;
})
};
};

View File

@ -158,6 +158,26 @@ function(
}
};
/**
* Reformat the conversations to a common standard because this is linked directly to the ajax response and via
* an event publish which operate on the same fields but in a different format
* @param conversations
*/
var formatConversationsForRender = function(conversations) {
// Convert the conversation to the standard stored and then cache the conversation.
return conversations.map(function(conversation) {
return Object.keys(conversation).reduce(function(carry, key){
if ($.isArray(conversation[key])) {
carry[key.toLowerCase()] = formatConversationsForRender(conversation[key]);
} else {
carry[key.toLowerCase()] = conversation[key];
}
return carry;
}, {});
}, []);
};
/**
* Render the messages in the overview page.
*
@ -166,7 +186,8 @@ function(
* @param {Number} userId Logged in user id.
* @return {Object} jQuery promise.
*/
var render = function(contentContainer, conversations, userId) {
var render = function(conversations, userId) {
conversations = formatConversationsForRender(conversations);
var formattedConversations = conversations.map(function(conversation) {
var lastMessage = conversation.messages.length ? conversation.messages[conversation.messages.length - 1] : null;
@ -222,12 +243,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,51 +431,6 @@ 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.
*
@ -484,6 +455,7 @@ function(
if (lastMessage) {
text = $(lastMessage.text).text() || lastMessage.text;
conversation.messages[messageCount - 1].useridfrom = lastMessage.userFrom.id;
}
var formattedConversation = {
@ -496,8 +468,8 @@ function(
imageurl: conversation.imageUrl,
};
// Cache the conversation.
loadedConversationsById[conversation.id] = conversation;
// Convert the conversation to the standard stored and then cache the conversation.
loadedConversationsById[conversation.id] = formatConversationsForRender([conversation])[0];
if (new Date().toDateString() == new Date(formattedConversation.lastmessagedate * 1000).toDateString()) {
formattedConversation.istoday = true;
@ -583,7 +555,12 @@ 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;
});
});
});
root.on('hidden.bs.collapse', function() {
@ -629,7 +606,14 @@ function(
var conversationId = conversation.id;
var element = getConversationElement(root, conversationId);
if (element.length) {
updateLastMessage(element, conversation);
var contentContainer = LazyLoadList.getContentContainer(root);
render([conversation], conversation.loggedInUserId)
.then(function(html) {
contentContainer.prepend(html);
element.remove();
return html;
})
.catch(Notification.exception);
} else {
createNewConversation(root, conversation);
}
@ -637,6 +621,7 @@ function(
PubSub.subscribe(MessageDrawerEvents.CONVERSATION_DELETED, function(conversationId) {
var conversationElement = getConversationElement(root, conversationId);
delete loadedConversationsById[conversationId];
if (conversationElement.length) {
deleteConversation(root, conversationElement);
}
@ -714,7 +699,12 @@ 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;
});
});
}
// This is given to us by the calling code because the total counts for all sections