diff --git a/src/controllers/ConversationsController.ts b/src/controllers/ConversationsController.ts index af4d500..22131e7 100644 --- a/src/controllers/ConversationsController.ts +++ b/src/controllers/ConversationsController.ts @@ -319,6 +319,27 @@ export class ConversationsController { h.success("The conversation has been deleted."); } + /** + * Update the content of a conversation message + * + * @param h Request handler + */ + public static async UpdateMessage(h: RequestHandler) { + const messageID = h.postInt("messageID"); + const newContent = h.postString("content"); + + if(newContent.length < 3) + h.error(401, "Invalid new message content!"); + + // Check out whether the user own the message or not + if(!await ConversationsHelper.IsUserMessageOwner(h.getUserId(), messageID)) + h.error(401, "You do not own this conversation message!"); + + await ConversationsHelper.UpdateMessageContent(messageID, newContent); + + h.success("Conversation message content successfully update"); + } + /** * 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 682a488..f78a569 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -72,6 +72,8 @@ export const Routes : Route[] = [ {path: "/conversations/get_list_unread", cb: (h) => ConversationsController.GetListUnread(h)}, {path: "/conversations/delete", cb: (h) => ConversationsController.DeleteConversation(h)}, + + {path: "/conversations/updateMessage", cb: (h) => ConversationsController.UpdateMessage(h)}, // Search controller diff --git a/src/helpers/ConversationsHelper.ts b/src/helpers/ConversationsHelper.ts index 4af58fa..1e79b2d 100644 --- a/src/helpers/ConversationsHelper.ts +++ b/src/helpers/ConversationsHelper.ts @@ -243,6 +243,22 @@ export class ConversationsHelper { }) == 1; } + /** + * Check out whether a user is the owner of a message or not + * + * @param userID Target user ID + * @param messageID Target message ID + */ + public static async IsUserMessageOwner(userID: number, messageID: number) : Promise { + return (await DatabaseHelper.Count({ + table: MESSAGES_TABLE, + where: { + id: messageID, + user_id: userID + } + })) > 0; + } + /** * Get the last messages of a conversation * @@ -382,6 +398,24 @@ export class ConversationsHelper { } + /** + * Update message content + * + * @param messageID Target message ID + * @param newContent New message content + */ + public static async UpdateMessageContent(messageID: number, newContent: string) { + await DatabaseHelper.UpdateRows({ + table: MESSAGES_TABLE, + where: { + id: messageID + }, + set: { + message: newContent + } + }); + } + /** * Search for private conversations between two users *