diff --git a/scripts/langindex.json b/scripts/langindex.json index a2efd09d1..6a41036ab 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -182,6 +182,7 @@ "addon.messages.deleteallconfirm": "message", "addon.messages.deleteallselfconfirm": "message", "addon.messages.deleteconversation": "message", + "addon.messages.deleteforeveryone": "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 3a298fdff..071d2fd73 100644 --- a/src/addon/messages/lang/en.json +++ b/src/addon/messages/lang/en.json @@ -21,6 +21,7 @@ "deleteallconfirm": "Are you sure you would like to delete this entire conversation? This will not delete it for other conversation participants.", "deleteallselfconfirm": "Are you sure you would like to delete this entire personal conversation?", "deleteconversation": "Delete conversation", + "deleteforeveryone": "Delete for me and everyone", "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.ts b/src/addon/messages/pages/discussion/discussion.ts index 3fecd7867..0022208e6 100644 --- a/src/addon/messages/pages/discussion/discussion.ts +++ b/src/addon/messages/pages/discussion/discussion.ts @@ -809,11 +809,25 @@ export class AddonMessagesDiscussionPage implements OnDestroy { * @param {number} index Index where the message is to delete it from the view. */ deleteMessage(message: any, index: number): void { - const langKey = message.pending ? 'core.areyousure' : 'addon.messages.deletemessageconfirmation'; - this.domUtils.showConfirm(this.translate.instant(langKey)).then(() => { + const canDeleteAll = this.conversation && this.conversation.candeletemessagesforallusers, + langKey = message.pending || canDeleteAll ? 'core.areyousure' : 'addon.messages.deletemessageconfirmation', + options: any = {}; + + if (canDeleteAll && !message.pending) { + // Show delete for all checkbox. + options.inputs = [{ + type: 'checkbox', + name: 'deleteforall', + checked: false, + value: true, + label: this.translate.instant('addon.messages.deleteforeveryone') + }]; + } + + this.domUtils.showConfirm(this.translate.instant(langKey), undefined, undefined, undefined, options).then((data) => { const modal = this.domUtils.showModalLoading('core.deleting', true); - return this.messagesProvider.deleteMessage(message).then(() => { + return this.messagesProvider.deleteMessage(message, data && data[0]).then(() => { // Remove message from the list without having to wait for re-fetch. this.messages.splice(index, 1); this.removeMessage(message.hash); diff --git a/src/addon/messages/providers/messages.ts b/src/addon/messages/providers/messages.ts index b651a09a2..fbaccdd11 100644 --- a/src/addon/messages/providers/messages.ts +++ b/src/addon/messages/providers/messages.ts @@ -244,12 +244,17 @@ export class AddonMessagesProvider { * Delete a message (online or offline). * * @param {any} message Message to delete. + * @param {boolean} [deleteForAll] Whether the message should be deleted for all users. * @return {Promise} Promise resolved when the message has been deleted. */ - deleteMessage(message: any): Promise { + deleteMessage(message: any, deleteForAll?: boolean): Promise { if (message.id) { // Message has ID, it means it has been sent to the server. - return this.deleteMessageOnline(message.id, message.read); + if (deleteForAll) { + return this.deleteMessageForAllOnline(message.id); + } else { + return this.deleteMessageOnline(message.id, message.read); + } } // It's an offline message. @@ -283,6 +288,24 @@ export class AddonMessagesProvider { }); } + /** + * Delete a message for all users. + * + * @param {number} id Message ID. + * @param {number} [userId] User we want to delete the message for. If not defined, use current user. + * @return {Promise} Promise resolved when the message has been deleted. + */ + deleteMessageForAllOnline(id: number, userId?: number): Promise { + const params: any = { + messageid: id, + userid: userId || this.sitesProvider.getCurrentSiteUserId() + }; + + return this.sitesProvider.getCurrentSite().write('core_message_delete_message_for_all_users', params).then(() => { + return this.invalidateDiscussionCache(userId); + }); + } + /** * Format a conversation. * diff --git a/src/assets/lang/en.json b/src/assets/lang/en.json index 66ca0a25a..aca180965 100644 --- a/src/assets/lang/en.json +++ b/src/assets/lang/en.json @@ -182,6 +182,7 @@ "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.deleteallselfconfirm": "Are you sure you would like to delete this entire personal conversation?", "addon.messages.deleteconversation": "Delete conversation", + "addon.messages.deleteforeveryone": "Delete for me and everyone", "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.", diff --git a/src/providers/utils/dom.ts b/src/providers/utils/dom.ts index bf303e859..fe2b67500 100644 --- a/src/providers/utils/dom.ts +++ b/src/providers/utils/dom.ts @@ -991,9 +991,9 @@ export class CoreDomUtilsProvider { * @param {string} [okText] Text of the OK button. * @param {string} [cancelText] Text of the Cancel button. * @param {any} [options] More options. See https://ionicframework.com/docs/api/components/alert/AlertController/ - * @return {Promise} Promise resolved if the user confirms and rejected with a canceled error if he cancels. + * @return {Promise} Promise resolved if the user confirms and rejected with a canceled error if he cancels. */ - showConfirm(message: string, title?: string, okText?: string, cancelText?: string, options?: any): Promise { + showConfirm(message: string, title?: string, okText?: string, cancelText?: string, options?: any): Promise { return new Promise((resolve, reject): void => { const hasHTMLTags = this.textUtils.hasHTMLTags(message); let promise; @@ -1023,8 +1023,8 @@ export class CoreDomUtilsProvider { }, { text: okText || this.translate.instant('core.ok'), - handler: (): void => { - resolve(); + handler: (data: any): void => { + resolve(data); } } ];