diff --git a/scripts/langindex.json b/scripts/langindex.json index 4f57c5479..392af6ed2 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -160,6 +160,8 @@ "addon.messages.contactlistempty": "local_moodlemobileapp", "addon.messages.contactname": "local_moodlemobileapp", "addon.messages.contacts": "message", + "addon.messages.deleteallconfirm": "message", + "addon.messages.deleteconversationq": "message", "addon.messages.deletemessage": "local_moodlemobileapp", "addon.messages.deletemessageconfirmation": "local_moodlemobileapp", "addon.messages.errordeletemessage": "local_moodlemobileapp", diff --git a/src/addon/messages/lang/en.json b/src/addon/messages/lang/en.json index b494638c7..9bfb0f29b 100644 --- a/src/addon/messages/lang/en.json +++ b/src/addon/messages/lang/en.json @@ -12,6 +12,8 @@ "contactlistempty": "The contact list is empty", "contactname": "Contact name", "contacts": "Contacts", + "deleteallconfirm": "Are you sure you would like to delete this entire conversation? This will not delete it for other conversation participants.", + "deleteconversation": "Delete conversation", "deletemessage": "Delete message", "deletemessageconfirmation": "Are you sure you want to delete this message? It will only be deleted from your messaging history and will still be viewable by the user who sent or received the message.", "errordeletemessage": "Error while deleting the message.", diff --git a/src/addon/messages/pages/discussion/discussion.html b/src/addon/messages/pages/discussion/discussion.html index 1dbde4d4c..25251f0c3 100644 --- a/src/addon/messages/pages/discussion/discussion.html +++ b/src/addon/messages/pages/discussion/discussion.html @@ -13,6 +13,7 @@ + diff --git a/src/addon/messages/pages/discussion/discussion.ts b/src/addon/messages/pages/discussion/discussion.ts index 13c0ec5d7..cd1d1d967 100644 --- a/src/addon/messages/pages/discussion/discussion.ts +++ b/src/addon/messages/pages/discussion/discussion.ts @@ -76,6 +76,7 @@ export class AddonMessagesDiscussionPage implements OnDestroy { isGroup = false; members: any = {}; // Members that wrote a message, indexed by ID. favouriteIcon = 'fa-star'; + deleteIcon = 'trash'; constructor(private eventsProvider: CoreEventsProvider, sitesProvider: CoreSitesProvider, navParams: NavParams, private userProvider: CoreUserProvider, private navCtrl: NavController, private messagesSync: AddonMessagesSyncProvider, @@ -984,6 +985,33 @@ export class AddonMessagesDiscussionPage implements OnDestroy { }); } + /** + * Delete the conversation. + * + * @param {Function} [done] Function to call when done. + */ + deleteConversation(done?: () => void): void { + this.domUtils.showConfirm(this.translate.instant('addon.messages.deleteallconfirm')).then(() => { + this.deleteIcon = 'spinner'; + + return this.messagesProvider.deleteConversation(this.conversation.id).then(() => { + this.eventsProvider.trigger(AddonMessagesProvider.UPDATE_CONVERSATION_LIST_EVENT, { + conversationId: this.conversation.id, + action: 'delete' + }, this.siteId); + + this.conversationId = undefined; + this.conversation = undefined; + this.messages = []; + }).finally(() => { + this.deleteIcon = 'trash'; + done && done(); + }); + }).catch((error) => { + this.domUtils.showErrorModalDefault(error, 'Error deleting conversation.'); + }); + } + /** * Page destroyed. */ diff --git a/src/addon/messages/providers/messages-offline.ts b/src/addon/messages/providers/messages-offline.ts index f746c9147..f340c7e17 100644 --- a/src/addon/messages/providers/messages-offline.ts +++ b/src/addon/messages/providers/messages-offline.ts @@ -109,6 +109,21 @@ export class AddonMessagesOfflineProvider { }); } + /** + * Delete all the messages in a conversation. + * + * @param {number} conversationId Conversation ID. + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise} Promise resolved if stored, rejected if failure. + */ + deleteConversationMessages(conversationId: number, siteId?: string): Promise { + return this.sitesProvider.getSite(siteId).then((site) => { + return site.getDb().deleteRecords(AddonMessagesOfflineProvider.CONVERSATION_MESSAGES_TABLE, { + conversationid: conversationId + }); + }); + } + /** * Delete a message. * diff --git a/src/addon/messages/providers/messages.ts b/src/addon/messages/providers/messages.ts index 8e160d4d4..e6f859509 100644 --- a/src/addon/messages/providers/messages.ts +++ b/src/addon/messages/providers/messages.ts @@ -106,6 +106,49 @@ export class AddonMessagesProvider { }); } + /** + * Delete a conversation. + * + * @param {number} conversationId Conversation to delete. + * @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} Promise resolved when the conversation has been deleted. + */ + deleteConversation(conversationId: number, siteId?: string, userId?: number): Promise { + return this.deleteConversations([conversationId], siteId, userId); + } + + /** + * Delete several conversations. + * + * @param {number[]} conversationIds Conversations to delete. + * @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} Promise resolved when the conversations have been deleted. + */ + deleteConversations(conversationIds: number[], siteId?: string, userId?: number): Promise { + return this.sitesProvider.getSite(siteId).then((site) => { + userId = userId || site.getUserId(); + + const params = { + userid: userId, + conversationids: conversationIds + }; + + return site.write('core_message_delete_conversations_by_id', params).then(() => { + const promises = []; + + conversationIds.forEach((conversationId) => { + promises.push(this.messagesOffline.deleteConversationMessages(conversationId, site.getId()).catch(() => { + // Ignore errors (shouldn't happen). + })); + }); + + return Promise.all(promises); + }); + }); + } + /** * Delete a message (online or offline). * diff --git a/src/assets/lang/en.json b/src/assets/lang/en.json index e42dcf18d..45d914929 100644 --- a/src/assets/lang/en.json +++ b/src/assets/lang/en.json @@ -160,6 +160,8 @@ "addon.messages.contactlistempty": "The contact list is empty", "addon.messages.contactname": "Contact name", "addon.messages.contacts": "Contacts", + "addon.messages.deleteallconfirm": "Are you sure you would like to delete this entire conversation? This will not delete it for other conversation participants.", + "addon.messages.deleteconversation": "Delete conversation", "addon.messages.deletemessage": "Delete message", "addon.messages.deletemessageconfirmation": "Are you sure you want to delete this message? It will only be deleted from your messaging history and will still be viewable by the user who sent or received the message.", "addon.messages.errordeletemessage": "Error while deleting the message.",