diff --git a/src/controllers/ConversationsController.ts b/src/controllers/ConversationsController.ts index cf1723f..af2112a 100644 --- a/src/controllers/ConversationsController.ts +++ b/src/controllers/ConversationsController.ts @@ -179,6 +179,29 @@ export class ConversationsController { h.send(list); } + /** + * Refresh the messages of a single conversation + * + * @param h Request handler + */ + public static async RefreshSingleConversation(h: RequestHandler) { + const convID = await this.GetPostConversationId("conversationID", h); + const lastMessageID = h.postInt("last_message_id"); + + const messages = lastMessageID == 0 ? + + // Get lastest messages (the user has no message yet) + await ConversationsHelper.GetLastMessages(convID, 10) : + + // Get new messages + await ConversationsHelper.GetNewMessages(convID, lastMessageID); + + // Specify the user has seen the last message + await ConversationsHelper.MarkUserSeen(convID, h.getUserId()); + + h.send(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 a5af6f8..47f2200 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -58,6 +58,8 @@ export const Routes : Route[] = [ {path: "/conversations/updateSettings", cb: (h) => ConversationsController.UpdateSettings(h)}, {path: "/conversations/refresh", cb: (h) => ConversationsController.RefreshList(h)}, + + {path: "/conversations/refresh_single", cb: (h) => ConversationsController.RefreshSingleConversation(h)}, // Search controller