diff --git a/src/controllers/ConversationsController.ts b/src/controllers/ConversationsController.ts index 22131e7..90e7ef6 100644 --- a/src/controllers/ConversationsController.ts +++ b/src/controllers/ConversationsController.ts @@ -340,6 +340,22 @@ export class ConversationsController { h.success("Conversation message content successfully update"); } + /** + * Delete a conversation message + * + * @param h Request handler + */ + public static async DeleteMessage(h: RequestHandler) { + const messageID = h.postInt("messageID"); + + if(!await ConversationsHelper.IsUserMessageOwner(h.getUserId(), messageID)) + h.error(401, "You do not own this conversation message!"); + + await ConversationsHelper.DeleteMessageById(messageID); + + h.success("Conversation message has been successfully 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 f78a569..72c2412 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -74,6 +74,8 @@ export const Routes : Route[] = [ {path: "/conversations/delete", cb: (h) => ConversationsController.DeleteConversation(h)}, {path: "/conversations/updateMessage", cb: (h) => ConversationsController.UpdateMessage(h)}, + + {path: "/conversations/deleteMessage", cb: (h) => ConversationsController.DeleteMessage(h)}, // Search controller diff --git a/src/helpers/ConversationsHelper.ts b/src/helpers/ConversationsHelper.ts index 1e79b2d..a7b913f 100644 --- a/src/helpers/ConversationsHelper.ts +++ b/src/helpers/ConversationsHelper.ts @@ -310,6 +310,26 @@ export class ConversationsHelper { })).map(m => this.DBToConversationMessage(convID, m)); } + /** + * Get information about a single conversation message + * + * @param messageID The ID of the message to get + * @throws If the message was not found + */ + private static async GetSingleMessage(messageID: number) : Promise { + const row = await DatabaseHelper.QueryRow({ + table: MESSAGES_TABLE, + where: { + id: messageID + } + }); + + if(row == null) + throw Error("The message was not found!"); + + return this.DBToConversationMessage(row.conv_id, row); + } + /** * Get older messages of a conversation * @@ -565,6 +585,18 @@ export class ConversationsHelper { await this.RemoveMember(convID, memberID); } + /** + * Delete a conversation message identified by its ID + * + * @param id The ID of the message to delete + */ + public static async DeleteMessageById(id: number) { + // Get information about the message + const message = await this.GetSingleMessage(id); + + await this.DeleteMessage(message); + } + /** * Delete a conversation message from the database *