From ae88b7a97961ff84bf0f6108b38a18195a6ba86d Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 7 Dec 2019 17:08:53 +0100 Subject: [PATCH] Can get older messages --- src/controllers/ConversationsController.ts | 19 +++++++++++++++++++ src/controllers/Routes.ts | 2 ++ src/helpers/ConversationsHelper.ts | 22 ++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/src/controllers/ConversationsController.ts b/src/controllers/ConversationsController.ts index 8b49f0b..2d52d2d 100644 --- a/src/controllers/ConversationsController.ts +++ b/src/controllers/ConversationsController.ts @@ -264,6 +264,25 @@ export class ConversationsController { h.success("Conversation message was sent!"); } + /** + * Get the older messages of a conversation + * + * @param h Request handler + */ + public static async GetOlderMessages(h: RequestHandler) { + const convID = await this.GetPostConversationId("conversationID", h); + const maxID = h.postInt("oldest_message_id") - 1; + + let limit = h.postInt("limit"); + if(limit < 1) limit = 1; + else if(limit > 30) limit = 30; + + // Get the list of messages + const messages = await ConversationsHelper.GetOlderMessage(convID, maxID, limit); + + h.send(messages.map(e => this.ConversationMessageToAPI(e))); + } + /** * 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 b7a26c6..a17a6d3 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -64,6 +64,8 @@ export const Routes : Route[] = [ {path: "/conversations/refresh_single", cb: (h) => ConversationsController.RefreshSingleConversation(h)}, {path: "/conversations/sendMessage", cb: (h) => ConversationsController.SendMessage(h)}, + + {path: "/conversations/get_older_messages", cb: (h) => ConversationsController.GetOlderMessages(h)}, // Search controller diff --git a/src/helpers/ConversationsHelper.ts b/src/helpers/ConversationsHelper.ts index 9d642f1..5bda853 100644 --- a/src/helpers/ConversationsHelper.ts +++ b/src/helpers/ConversationsHelper.ts @@ -276,6 +276,28 @@ export class ConversationsHelper { })).map(m => this.DBToConversationMessage(convID, m)); } + /** + * Get older messages of a conversation + * + * @param convID ID of the target conversation + * @param startID ID from which the research should start + * @param limit Maximum number of messages to get + * @return The list of messages + */ + public static async GetOlderMessage(convID: number, startID: number, limit: number) : Promise> { + return (await DatabaseHelper.Query({ + table: MESSAGES_TABLE, + where: { + conv_id: convID, + }, + customWhere: "ID <= ?", + customWhereArgs: [startID.toString()], + order: "id DESC", + limit: limit + })) + .map(m => this.DBToConversationMessage(convID, m)).reverse(); + } + /** * Mark the user has seen the last messages of the conversation *