MOBILE-2631 message: Allow deleting conversations

main
Dani Palou 2018-11-28 13:47:16 +01:00
parent f22f33dad4
commit 3ea1040cd9
7 changed files with 93 additions and 0 deletions

View File

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

View File

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

View File

@ -13,6 +13,7 @@
<core-context-menu-item [hidden]="!showInfo || !isGroup" [priority]="1000" [content]="'addon.messages.groupinfo' | translate" (action)="viewInfo()" [iconAction]="'information-circle'"></core-context-menu-item>
<core-context-menu-item [hidden]="!groupMessagingEnabled || !conversation" [priority]="800" [content]="(conversation && conversation.isfavourite ? 'addon.messages.removefromfavourites' : 'addon.messages.addtofavourites') | translate" (action)="changeFavourite($event)" [iconAction]="favouriteIcon" [closeOnClick]="false"></core-context-menu-item>
<core-context-menu-item [hidden]="!canDelete" [priority]="400" [content]="'addon.messages.showdeletemessages' | translate" (action)="toggleDelete()" [iconAction]="'trash'"></core-context-menu-item>
<core-context-menu-item [hidden]="!groupMessagingEnabled || !conversationId || isGroup" [priority]="200" [content]="'addon.messages.deleteconversation' | translate" (action)="deleteConversation($event)" [iconAction]="deleteIcon" [closeOnClick]="false"></core-context-menu-item>
</core-context-menu>
</core-navbar-buttons>
</ion-header>

View File

@ -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.
*/

View File

@ -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<any>} Promise resolved if stored, rejected if failure.
*/
deleteConversationMessages(conversationId: number, siteId?: string): Promise<any> {
return this.sitesProvider.getSite(siteId).then((site) => {
return site.getDb().deleteRecords(AddonMessagesOfflineProvider.CONVERSATION_MESSAGES_TABLE, {
conversationid: conversationId
});
});
}
/**
* Delete a message.
*

View File

@ -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<any>} Promise resolved when the conversation has been deleted.
*/
deleteConversation(conversationId: number, siteId?: string, userId?: number): Promise<any> {
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<any>} Promise resolved when the conversations have been deleted.
*/
deleteConversations(conversationIds: number[], siteId?: string, userId?: number): Promise<any> {
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).
*

View File

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