diff --git a/scripts/langindex.json b/scripts/langindex.json index 2f31199ff..74e8da77f 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -197,6 +197,7 @@ "addon.messages.messagenotsent": "local_moodlemobileapp", "addon.messages.messagepreferences": "message", "addon.messages.messages": "message", + "addon.messages.muteconversation": "message", "addon.messages.newmessage": "message", "addon.messages.newmessages": "local_moodlemobileapp", "addon.messages.nocontactrequests": "message", @@ -230,6 +231,7 @@ "addon.messages.unabletomessage": "message", "addon.messages.unblockuser": "message", "addon.messages.unblockuserconfirm": "message", + "addon.messages.unmuteconversation": "message", "addon.messages.useentertosend": "message", "addon.messages.useentertosenddescdesktop": "local_moodlemobileapp", "addon.messages.useentertosenddescmac": "local_moodlemobileapp", diff --git a/src/addon/messages/lang/en.json b/src/addon/messages/lang/en.json index b5aa51b7e..f428c85c7 100644 --- a/src/addon/messages/lang/en.json +++ b/src/addon/messages/lang/en.json @@ -36,6 +36,7 @@ "messagenotsent": "The message was not sent. Please try again later.", "messagepreferences": "Message preferences", "messages": "Messages", + "muteconversation": "Mute", "newmessage": "New message", "newmessages": "New messages", "nocontactrequests": "No contact requests", @@ -69,6 +70,7 @@ "unabletomessage": "You are unable to message this user", "unblockuser": "Unblock user", "unblockuserconfirm": "Are you sure you want to unblock {{$a}}?", + "unmuteconversation": "Unmute", "useentertosend": "Use enter to send", "useentertosenddescdesktop": "If disabled, you can use Ctrl+Enter to send the message.", "useentertosenddescmac": "If disabled, you can use Cmd+Enter to send the message.", diff --git a/src/addon/messages/pages/discussion/discussion.html b/src/addon/messages/pages/discussion/discussion.html index f93a8b6af..fb5602317 100644 --- a/src/addon/messages/pages/discussion/discussion.html +++ b/src/addon/messages/pages/discussion/discussion.html @@ -5,6 +5,7 @@ + @@ -15,6 +16,7 @@ + diff --git a/src/addon/messages/pages/discussion/discussion.ts b/src/addon/messages/pages/discussion/discussion.ts index fb0f2299a..dae1a0f12 100644 --- a/src/addon/messages/pages/discussion/discussion.ts +++ b/src/addon/messages/pages/discussion/discussion.ts @@ -88,6 +88,8 @@ export class AddonMessagesDiscussionPage implements OnDestroy { requestContactSent = false; requestContactReceived = false; isSelf = false; + muteEnabled = false; + muteIcon = 'volume-off'; constructor(private eventsProvider: CoreEventsProvider, sitesProvider: CoreSitesProvider, navParams: NavParams, private userProvider: CoreUserProvider, private navCtrl: NavController, private messagesSync: AddonMessagesSyncProvider, @@ -99,6 +101,7 @@ export class AddonMessagesDiscussionPage implements OnDestroy { this.siteId = sitesProvider.getCurrentSiteId(); this.currentUserId = sitesProvider.getCurrentSiteUserId(); this.groupMessagingEnabled = this.messagesProvider.isGroupMessagingEnabled(); + this.muteEnabled = this.messagesProvider.isMuteConversationEnabled(); this.logger = logger.getInstance('AddonMessagesDiscussionPage'); @@ -443,6 +446,7 @@ export class AddonMessagesDiscussionPage implements OnDestroy { this.conversationImage = conversation.imageurl; this.isGroup = conversation.type == AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_GROUP; this.favouriteIcon = conversation.isfavourite ? 'fa-star-o' : 'fa-star'; + this.muteIcon = conversation.ismuted ? 'volume-up' : 'volume-off'; if (!this.isGroup) { this.userId = conversation.userid; } @@ -1107,6 +1111,33 @@ export class AddonMessagesDiscussionPage implements OnDestroy { }); } + /** + * Change the mute state of the current conversation. + * + * @param {Function} [done] Function to call when done. + */ + changeMute(done?: () => void): void { + this.muteIcon = 'spinner'; + + this.messagesProvider.muteConversation(this.conversation.id, !this.conversation.ismuted).then(() => { + this.conversation.ismuted = !this.conversation.ismuted; + + // Get the conversation data so it's cached. Don't block the user for this. + this.messagesProvider.getConversation(this.conversation.id, undefined, true); + + this.eventsProvider.trigger(AddonMessagesProvider.UPDATE_CONVERSATION_LIST_EVENT, { + conversationId: this.conversation.id, + action: 'mute', + value: this.conversation.ismuted + }, this.siteId); + }).catch((error) => { + this.domUtils.showErrorModalDefault(error, 'Error changing muted state.'); + }).finally(() => { + this.muteIcon = this.conversation.ismuted ? 'volume-up' : 'volume-off'; + done && done(); + }); + } + /** * Calculate whether there are pending contact requests. */ diff --git a/src/addon/messages/pages/group-conversations/group-conversations.html b/src/addon/messages/pages/group-conversations/group-conversations.html index bcb05e861..ec74e6a41 100644 --- a/src/addon/messages/pages/group-conversations/group-conversations.html +++ b/src/addon/messages/pages/group-conversations/group-conversations.html @@ -101,6 +101,7 @@

+

{{ conversation.unreadcount }} diff --git a/src/addon/messages/pages/group-conversations/group-conversations.ts b/src/addon/messages/pages/group-conversations/group-conversations.ts index a97006ea1..94af243d1 100644 --- a/src/addon/messages/pages/group-conversations/group-conversations.ts +++ b/src/addon/messages/pages/group-conversations/group-conversations.ts @@ -166,8 +166,23 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { }); // Update conversations if we receive an event to do so. - this.updateConversationListObserver = eventsProvider.on(AddonMessagesProvider.UPDATE_CONVERSATION_LIST_EVENT, () => { + this.updateConversationListObserver = eventsProvider.on(AddonMessagesProvider.UPDATE_CONVERSATION_LIST_EVENT, (data) => { + if (data && data.action == 'mute') { + // If the conversation is displayed, change its muted value. + const expandedOption = this.getExpandedOption(); + + if (expandedOption && expandedOption.conversations) { + const conversation = this.findConversation(data.conversationId, undefined, expandedOption); + if (conversation) { + conversation.ismuted = data.value; + } + } + + return; + } + this.refreshData(); + }, this.siteId); // If a message push notification is received, refresh the view. diff --git a/src/addon/messages/providers/messages.ts b/src/addon/messages/providers/messages.ts index 47c238b8e..b651a09a2 100644 --- a/src/addon/messages/providers/messages.ts +++ b/src/addon/messages/providers/messages.ts @@ -1938,6 +1938,34 @@ export class AddonMessagesProvider { }); } + /** + * Returns whether or not a site supports muting or unmuting a conversation. + * + * @param {CoreSite} [site] The site to check, undefined for current site. + * @return {boolean} If related WS is available on current site. + * @since 3.7 + */ + isMuteConversationEnabled(site?: CoreSite): boolean { + site = site || this.sitesProvider.getCurrentSite(); + + return site.wsAvailable('core_message_mute_conversations'); + } + + /** + * Returns whether or not a site supports muting or unmuting a conversation. + * + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise} Promise resolved with boolean: whether related WS is available on a certain site. + * @since 3.7 + */ + isMuteConversationEnabledInSite(siteId?: string): Promise { + return this.sitesProvider.getSite(siteId).then((site) => { + return this.isMuteConversationEnabled(site); + }).catch(() => { + return false; + }); + } + /** * Returns whether or not the plugin is enabled in a certain site. * @@ -2043,6 +2071,53 @@ export class AddonMessagesProvider { return this.sitesProvider.getCurrentSite().write('core_message_mark_all_messages_as_read', params, preSets); } + /** + * Mute or unmute a conversation. + * + * @param {number} conversationId Conversation ID. + * @param {boolean} set Whether to mute or unmute. + * @param {string} [siteId] Site ID. If not defined, use current site. + * @param {number} [userId] User ID. If not defined, current user in the site. + * @return {Promise} Resolved when done. + */ + muteConversation(conversationId: number, set: boolean, siteId?: string, userId?: number): Promise { + return this.muteConversations([conversationId], set, siteId, userId); + } + + /** + * Mute or unmute some conversations. + * + * @param {number[]} conversations Conversation IDs. + * @param {boolean} set Whether to mute or unmute. + * @param {string} [siteId] Site ID. If not defined, use current site. + * @param {number} [userId] User ID. If not defined, current user in the site. + * @return {Promise} Resolved when done. + */ + muteConversations(conversations: number[], set: boolean, siteId?: string, userId?: number): Promise { + return this.sitesProvider.getSite(siteId).then((site) => { + userId = userId || site.getUserId(); + + const params = { + userid: userId, + conversationids: conversations + }, + wsName = set ? 'core_message_mute_conversations' : 'core_message_unmute_conversations'; + + return site.write(wsName, params).then(() => { + // Invalidate the conversations data. + const promises = []; + + conversations.forEach((conversationId) => { + promises.push(this.invalidateConversation(conversationId, site.getId(), userId)); + }); + + return Promise.all(promises).catch(() => { + // Ignore errors. + }); + }); + }); + } + /** * Refresh the number of contact requests sent to the current user. * diff --git a/src/assets/lang/en.json b/src/assets/lang/en.json index 93ba504e9..7ed4e00c2 100644 --- a/src/assets/lang/en.json +++ b/src/assets/lang/en.json @@ -197,6 +197,7 @@ "addon.messages.messagenotsent": "The message was not sent. Please try again later.", "addon.messages.messagepreferences": "Message preferences", "addon.messages.messages": "Messages", + "addon.messages.muteconversation": "Mute", "addon.messages.newmessage": "New message", "addon.messages.newmessages": "New messages", "addon.messages.nocontactrequests": "No contact requests", @@ -230,6 +231,7 @@ "addon.messages.unabletomessage": "You are unable to message this user", "addon.messages.unblockuser": "Unblock user", "addon.messages.unblockuserconfirm": "Are you sure you want to unblock {{$a}}?", + "addon.messages.unmuteconversation": "Unmute", "addon.messages.useentertosend": "Use enter to send", "addon.messages.useentertosenddescdesktop": "If disabled, you can use Ctrl+Enter to send the message.", "addon.messages.useentertosenddescmac": "If disabled, you can use Cmd+Enter to send the message.",