From 5a253f114c710ef977be020e95f092ab4794ad6f Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 7 Dec 2019 18:06:41 +0100 Subject: [PATCH] Remove conversation: Can delete conversation messages --- src/controllers/ConversationsController.ts | 13 +++++ src/controllers/Routes.ts | 2 + src/entities/ConversationMessage.ts | 4 ++ src/helpers/ConversationsHelper.ts | 59 ++++++++++++++++++++++ 4 files changed, 78 insertions(+) diff --git a/src/controllers/ConversationsController.ts b/src/controllers/ConversationsController.ts index 921b336..af4d500 100644 --- a/src/controllers/ConversationsController.ts +++ b/src/controllers/ConversationsController.ts @@ -306,6 +306,19 @@ export class ConversationsController { h.send(list.map(e => this.UnreadConversationToAPI(e))); } + /** + * Request a conversation + * + * @param h Request handler + */ + public static async DeleteConversation(h: RequestHandler) { + const convID = await this.GetPostConversationId("conversationID", h); + + await ConversationsHelper.RemoveUserFromConversation(h.getUserId(), convID); + + h.success("The conversation has been deleted."); + } + /** * Get and return safely a conversation ID specified in a $_POST Request * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 3664649..682a488 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -70,6 +70,8 @@ export const Routes : Route[] = [ {path: "/conversations/get_number_unread", cb: (h) => ConversationsController.CountUnreadForUser(h)}, {path: "/conversations/get_list_unread", cb: (h) => ConversationsController.GetListUnread(h)}, + + {path: "/conversations/delete", cb: (h) => ConversationsController.DeleteConversation(h)}, // Search controller diff --git a/src/entities/ConversationMessage.ts b/src/entities/ConversationMessage.ts index 05f9cd0..4e6c464 100644 --- a/src/entities/ConversationMessage.ts +++ b/src/entities/ConversationMessage.ts @@ -43,4 +43,8 @@ export class ConversationMessage implements ConversationMessageInfo { get imageURL() : string { return pathUserData(this.imagePath); } + + get imageSysPath() : string { + return pathUserData(this.imagePath, true); + } } \ No newline at end of file diff --git a/src/helpers/ConversationsHelper.ts b/src/helpers/ConversationsHelper.ts index ab2eea7..c5f1d05 100644 --- a/src/helpers/ConversationsHelper.ts +++ b/src/helpers/ConversationsHelper.ts @@ -3,6 +3,7 @@ import { DatabaseHelper } from "./DatabaseHelper"; import { time } from "../utils/DateUtils"; import { ConversationMessage, BaseConversationMessage } from "../entities/ConversationMessage"; import { UnreadConversation } from "../entities/UnreadConversation"; +import { existsSync, unlinkSync } from "fs"; /** * Conversations helper @@ -456,6 +457,64 @@ export class ConversationsHelper { }); } + /** + * Remove a user from a conversation + * + * @param userID Target user ID + * @param convID Target conversation ID + */ + public static async RemoveUserFromConversation(userID: number, convID: number) { + if(await this.IsUserModerator(userID, convID)) + await this.DeleteConversations(convID); + //else + // Delete the membersip fot + // TODO : implement + + } + + + /** + * Delete a conversation + * + * @param convID The ID of the conversation to delete + */ + private static async DeleteConversations(convID: number) { + // Get all the messages of the conversations + const messages = await this.GetNewMessages(convID, 0); + + for (const message of messages) { + await this.DeleteMessage(message); + } + + // Delete all the members of the conversation + // TODO + + // Delete the conversation entry itself + // TODO + } + + /** + * Delete a conversation message from the database + * + * @param m The message to delete + */ + private static async DeleteMessage(m: ConversationMessage) { + + // Delete conversation message image (if any) + if(m.hasImage) { + if(existsSync(m.imageSysPath)) + unlinkSync(m.imageSysPath); + } + + // Delete the message from the database + await DatabaseHelper.DeleteRows( + MESSAGES_TABLE, + { + ID: m.id + } + ) + } + /** * Get the list of members of a conversation *