Merge branch 'MDL-66607-master' of git://github.com/andrewnicols/moodle

This commit is contained in:
Eloy Lafuente (stronk7)
2020-03-12 10:08:22 +01:00
6 changed files with 144 additions and 15 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -59,6 +59,7 @@ define(
'core/backoff_timer',
'core/custom_interaction_events',
'core/notification',
'core/pending',
'core/pubsub',
'core/str',
'core_message/message_repository',
@@ -78,6 +79,7 @@ function(
BackOffTimer,
CustomEvents,
Notification,
Pending,
PubSub,
Str,
Repository,
@@ -107,6 +109,8 @@ function(
var isResetting = true;
// If the UI is currently sending a message.
var isSendingMessage = false;
// If the UI is currently deleting a conversation.
var isDeletingConversationContent = false;
// A buffer of messages to send.
var sendMessageBuffer = [];
// These functions which will be generated when this module is
@@ -558,7 +562,7 @@ function(
var mostRecentMessage = messages.length ? messages[messages.length - 1] : null;
var lastTimeCreated = mostRecentMessage ? mostRecentMessage.timeCreated : null;
if (lastTimeCreated && !isResetting && !isSendingMessage) {
if (lastTimeCreated && !isResetting && !isSendingMessage && !isDeletingConversationContent) {
// There may be multiple messages with the same time created value since
// the accuracy is only down to the second. The server will include these
// messages in the result (since it does a >= comparison on time from) so
@@ -613,12 +617,18 @@ function(
*/
var markConversationAsRead = function(conversationId) {
var loggedInUserId = viewState.loggedInUserId;
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:markConversationAsRead');
return Repository.markAllConversationMessagesAsRead(loggedInUserId, conversationId)
.then(function() {
var newState = StateManager.markMessagesAsRead(viewState, viewState.messages);
PubSub.publish(MessageDrawerEvents.CONVERSATION_READ, conversationId);
return render(newState);
})
.then(function(result) {
pendingPromise.resolve();
return result;
});
};
@@ -643,6 +653,8 @@ function(
*/
var blockUser = function(userId) {
var newState = StateManager.setLoadingConfirmAction(viewState, true);
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:blockUser');
render(newState);
return Repository.blockUser(viewState.loggedInUserId, userId)
@@ -652,6 +664,11 @@ function(
newState = StateManager.setLoadingConfirmAction(newState, false);
PubSub.publish(MessageDrawerEvents.CONTACT_BLOCKED, userId);
return render(newState);
})
.then(function(result) {
pendingPromise.resolve();
return result;
});
};
@@ -676,6 +693,7 @@ function(
*/
var unblockUser = function(userId) {
var newState = StateManager.setLoadingConfirmAction(viewState, true);
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:unblockUser');
render(newState);
return Repository.unblockUser(viewState.loggedInUserId, userId)
@@ -685,6 +703,11 @@ function(
newState = StateManager.setLoadingConfirmAction(newState, false);
PubSub.publish(MessageDrawerEvents.CONTACT_UNBLOCKED, userId);
return render(newState);
})
.then(function(result) {
pendingPromise.resolve();
return result;
});
};
@@ -709,6 +732,7 @@ function(
*/
var removeContact = function(userId) {
var newState = StateManager.setLoadingConfirmAction(viewState, true);
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:removeContact');
render(newState);
return Repository.deleteContacts(viewState.loggedInUserId, [userId])
@@ -718,6 +742,11 @@ function(
newState = StateManager.setLoadingConfirmAction(newState, false);
PubSub.publish(MessageDrawerEvents.CONTACT_REMOVED, userId);
return render(newState);
})
.then(function(result) {
pendingPromise.resolve();
return result;
});
};
@@ -742,6 +771,7 @@ function(
*/
var addContact = function(userId) {
var newState = StateManager.setLoadingConfirmAction(viewState, true);
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:addContactRequests');
render(newState);
return Repository.createContactRequest(viewState.loggedInUserId, userId)
@@ -757,6 +787,11 @@ function(
newState = StateManager.addContactRequests(newState, [request]);
newState = StateManager.setLoadingConfirmAction(newState, false);
return render(newState);
})
.then(function(result) {
pendingPromise.resolve();
return result;
});
};
@@ -768,6 +803,7 @@ function(
var setFavourite = function() {
var userId = viewState.loggedInUserId;
var conversationId = viewState.id;
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:setFavourite');
return Repository.setFavouriteConversations(userId, [conversationId])
.then(function() {
@@ -779,6 +815,11 @@ function(
MessageDrawerEvents.CONVERSATION_SET_FAVOURITE,
formatConversationForEvent(viewState)
);
})
.then(function(result) {
pendingPromise.resolve();
return result;
});
};
@@ -790,6 +831,7 @@ function(
var unsetFavourite = function() {
var userId = viewState.loggedInUserId;
var conversationId = viewState.id;
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:unsetFavourite');
return Repository.unsetFavouriteConversations(userId, [conversationId])
.then(function() {
@@ -801,6 +843,11 @@ function(
MessageDrawerEvents.CONVERSATION_UNSET_FAVOURITE,
formatConversationForEvent(viewState)
);
})
.then(function(result) {
pendingPromise.resolve();
return result;
});
};
@@ -812,6 +859,7 @@ function(
var setMuted = function() {
var userId = viewState.loggedInUserId;
var conversationId = viewState.id;
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:markConversationAsRead');
return Repository.setMutedConversations(userId, [conversationId])
.then(function() {
@@ -823,6 +871,11 @@ function(
MessageDrawerEvents.CONVERSATION_SET_MUTED,
formatConversationForEvent(viewState)
);
})
.then(function(result) {
pendingPromise.resolve();
return result;
});
};
@@ -868,6 +921,7 @@ function(
* @return {Promise} Renderer promise.
*/
var deleteSelectedMessages = function() {
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:deleteSelectedMessages');
var messageIds = viewState.pendingDeleteMessageIds;
var sentMessages = viewState.messages.filter(function(message) {
// If a message sendState is null then it means it was loaded from the server or if it's
@@ -880,6 +934,7 @@ function(
var deleteMessagesPromise = $.Deferred().resolve().promise();
if (sentMessages.length) {
// We only need to send a request to the server if we're trying to delete messages that
// have successfully been sent.
@@ -893,6 +948,14 @@ function(
}
}
// Mark that we are deleting content from the conversation to prevent updates of it.
isDeletingConversationContent = true;
// Stop polling for new messages to the open conversation.
if (newMessagesPollTimer) {
newMessagesPollTimer.stop();
}
return deleteMessagesPromise.then(function() {
var newState = StateManager.removeMessagesById(viewState, messageIds);
newState = StateManager.removePendingDeleteMessagesById(newState, messageIds);
@@ -910,8 +973,14 @@ function(
PubSub.publish(MessageDrawerEvents.CONVERSATION_DELETED, newState.id);
}
isDeletingConversationContent = false;
return render(newState);
})
.then(function(result) {
pendingPromise.resolve();
return result;
})
.catch(Notification.exception);
};
@@ -934,9 +1003,18 @@ function(
* @return {Promise} Renderer promise.
*/
var deleteConversation = function() {
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:markConversationAsRead');
var newState = StateManager.setLoadingConfirmAction(viewState, true);
render(newState);
// Mark that we are deleting the conversation to prevent updates of it.
isDeletingConversationContent = true;
// Stop polling for new messages to the open conversation.
if (newMessagesPollTimer) {
newMessagesPollTimer.stop();
}
return Repository.deleteConversation(viewState.loggedInUserId, viewState.id)
.then(function() {
var newState = StateManager.removeMessages(viewState, viewState.messages);
@@ -945,7 +1023,14 @@ function(
newState = StateManager.setLoadingConfirmAction(newState, false);
PubSub.publish(MessageDrawerEvents.CONVERSATION_DELETED, newState.id);
isDeletingConversationContent = false;
return render(newState);
})
.then(function(result) {
pendingPromise.resolve();
return result;
});
};
@@ -973,6 +1058,8 @@ function(
* @return {Promise} Renderer promise.
*/
var acceptContactRequest = function(userId) {
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:acceptContactRequest');
// Search the list of the logged in user's contact requests to find the
// one from this user.
var loggedInUserId = viewState.loggedInUserId;
@@ -994,6 +1081,11 @@ function(
PubSub.publish(MessageDrawerEvents.CONTACT_ADDED, viewState.members[userId]);
PubSub.publish(MessageDrawerEvents.CONTACT_REQUEST_ACCEPTED, request);
return;
})
.then(function(result) {
pendingPromise.resolve();
return result;
});
};
@@ -1004,6 +1096,8 @@ function(
* @return {Promise} Renderer promise.
*/
var declineContactRequest = function(userId) {
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:declineContactRequest');
// Search the list of the logged in user's contact requests to find the
// one from this user.
var loggedInUserId = viewState.loggedInUserId;
@@ -1024,6 +1118,11 @@ function(
.then(function() {
PubSub.publish(MessageDrawerEvents.CONTACT_REQUEST_DECLINED, request);
return;
})
.then(function(result) {
pendingPromise.resolve();
return result;
});
};
@@ -1044,6 +1143,8 @@ function(
return;
}
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:processSendMessageBuffer');
// Flag that we're processing the queue.
isSendingMessage = true;
// Grab all of the messages in the buffer.
@@ -1131,11 +1232,17 @@ function(
PubSub.publish(MessageDrawerEvents.CONVERSATION_NEW_LAST_MESSAGE, conversation);
return;
})
.then(function(result) {
pendingPromise.resolve();
return result;
})
.catch(function(e) {
var errorMessage;
if (e.message) {
var errorMessage = $.Deferred().resolve(e.message).promise();
errorMessage = $.Deferred().resolve(e.message).promise();
} else {
var errorMessage = Str.get_string('unknownerror', 'core');
errorMessage = Str.get_string('unknownerror', 'core');
}
var handleFailedMessages = function(errorMessage) {
@@ -1148,6 +1255,11 @@ function(
};
errorMessage.then(handleFailedMessages)
.then(function(result) {
pendingPromise.resolve();
return result;
})
.catch(function(e) {
// Hrmm, we can't even load the error messages string! We'll have to
// hard code something in English here if we still haven't got a message
@@ -1211,8 +1323,6 @@ function(
/**
* Cancel edit mode (selecting the messages).
*
* @return {Promise} Renderer promise.
*/
var cancelEditMode = function() {
cancelRequest(getOtherUserId());
@@ -1249,6 +1359,8 @@ function(
renderable.deferred.resolve(true);
// Keep processing the buffer until it's empty.
processRenderBuffer(header, body, footer);
return;
})
.catch(function(error) {
isRendering = false;
@@ -1760,6 +1872,7 @@ function(
renderBuffer = [];
isResetting = true;
isSendingMessage = false;
isDeletingConversationContent = false;
sendMessageBuffer = [];
var loggedInUserId = loggedInUserProfile.id;
@@ -75,6 +75,7 @@ function(
var LOAD_LIMIT = 50;
var loadedConversationsById = {};
var deletedConversationsById = {};
var loadedTotalCounts = false;
var loadedUnreadCounts = false;
@@ -196,7 +197,6 @@ function(
/**
* Render the messages in the overview page.
*
* @param {Object} contentContainer Conversations content container.
* @param {Array} conversations List of conversations to render.
* @param {Number} userId Logged in user id.
* @return {Object} jQuery promise.
@@ -662,6 +662,7 @@ function(
return;
}
var pendingPromise = new Pending('core_message/message_drawer_view_overview_section:new');
var loggedInUserId = conversation.loggedInUserId;
var conversationId = conversation.id;
var element = getConversationElement(root, conversationId);
@@ -670,19 +671,34 @@ function(
var contentContainer = LazyLoadList.getContentContainer(root);
render([conversation], loggedInUserId)
.then(function(html) {
contentContainer.prepend(html);
element.remove();
return html;
})
if (deletedConversationsById[conversationId]) {
// This conversation was deleted at some point since the messaging drawer was created.
if (conversation.messages[0].timeadded < deletedConversationsById[conversationId]) {
// The 'new' message was added before the conversation was deleted.
// This is probably stale data.
return;
}
}
contentContainer.prepend(html);
element.remove();
return;
})
.then(pendingPromise.resolve)
.catch(Notification.exception);
} else if (conversation.messages.length) {
createNewConversationFromEvent(root, conversation, loggedInUserId)
.then(pendingPromise.resolve)
.catch();
} else {
createNewConversationFromEvent(root, conversation, loggedInUserId);
pendingPromise.resolve();
}
});
PubSub.subscribe(MessageDrawerEvents.CONVERSATION_DELETED, function(conversationId) {
var conversationElement = getConversationElement(root, conversationId);
delete loadedConversationsById[conversationId];
deletedConversationsById[conversationId] = new Date();
if (conversationElement.length) {
deleteConversation(root, conversationElement);
}