MOBILE-2964 message: Support delete messages for all users

main
Dani Palou 2019-05-06 14:44:03 +02:00
parent 8f8457d6ee
commit be5cbfd728
6 changed files with 49 additions and 9 deletions

View File

@ -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",

View File

@ -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.",

View File

@ -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);

View File

@ -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<any>} Promise resolved when the message has been deleted.
*/
deleteMessage(message: any): Promise<any> {
deleteMessage(message: any, deleteForAll?: boolean): Promise<any> {
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<any>} Promise resolved when the message has been deleted.
*/
deleteMessageForAllOnline(id: number, userId?: number): Promise<any> {
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.
*

View File

@ -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.",

View File

@ -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<void>} Promise resolved if the user confirms and rejected with a canceled error if he cancels.
* @return {Promise<any>} 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<void> {
showConfirm(message: string, title?: string, okText?: string, cancelText?: string, options?: any): Promise<any> {
return new Promise<void>((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);
}
}
];