From fad098114a77a49409e980f7e43036680f7a66bc Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 7 Dec 2019 18:26:06 +0100 Subject: [PATCH] Can delete a membership to a conversation --- src/helpers/ConversationsHelper.ts | 44 ++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/src/helpers/ConversationsHelper.ts b/src/helpers/ConversationsHelper.ts index 07cda5c..4af58fa 100644 --- a/src/helpers/ConversationsHelper.ts +++ b/src/helpers/ConversationsHelper.ts @@ -278,6 +278,22 @@ export class ConversationsHelper { })).map(m => this.DBToConversationMessage(convID, m)); } + /** + * Get all the messages of a single user for a conversation + * + * @param convID Target conversation ID + * @param userID Target user ID + */ + public static async GetUserMessagesForConversation(convID: number, userID: number): Promise> { + return (await DatabaseHelper.Query({ + table: MESSAGES_TABLE, + where: { + conv_id: convID, + user_id: userID + } + })).map(m => this.DBToConversationMessage(convID, m)); + } + /** * Get older messages of a conversation * @@ -464,11 +480,13 @@ export class ConversationsHelper { * @param convID Target conversation ID */ public static async RemoveUserFromConversation(userID: number, convID: number) { + // Check whether the user is the owner of the conversation or not if(await this.IsUserModerator(userID, convID)) await this.DeleteConversations(convID); - //else - // Delete the membersip fot - // TODO : implement + + else + // Only delete the messages & membership of teh user + await this.DeleteMember(convID, userID); } @@ -479,9 +497,8 @@ export class ConversationsHelper { * @param convID The ID of the conversation to delete */ private static async DeleteConversations(convID: number) { - // Get all the messages of the conversations + // Get & delete all the messages of the conversations const messages = await this.GetNewMessages(convID, 0); - for (const message of messages) { await this.DeleteMessage(message); } @@ -497,6 +514,23 @@ export class ConversationsHelper { }); } + /** + * Delete a conversation membership + * + * @param convID Target conversation + * @param memberID Target user ID + */ + private static async DeleteMember(convID: number, memberID: number) { + // Get & delete all the messages of the member + const messages = await this.GetUserMessagesForConversation(convID, memberID); + for (const message of messages) { + await this.DeleteMessage(message); + } + + // Delete membership + await this.RemoveMember(convID, memberID); + } + /** * Delete a conversation message from the database *