From 83e61f49ca4b173b75137d56a1999659181ea6e4 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 7 Dec 2019 17:35:14 +0100 Subject: [PATCH] Can get the list of unread conversations --- src/controllers/ConversationsController.ts | 27 +++++++++++++ src/controllers/Routes.ts | 4 +- src/entities/UnreadConversation.ts | 13 ++++++ src/helpers/ConversationsHelper.ts | 47 ++++++++++++++++++++++ 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 src/entities/UnreadConversation.ts diff --git a/src/controllers/ConversationsController.ts b/src/controllers/ConversationsController.ts index a8ba941..921b336 100644 --- a/src/controllers/ConversationsController.ts +++ b/src/controllers/ConversationsController.ts @@ -4,6 +4,7 @@ import { Conversation, BaseConversation } from "../entities/Conversation"; import { UserHelper } from "../helpers/UserHelper"; import { removeHTMLNodes } from "../utils/StringUtils"; import { ConversationMessage } from "../entities/ConversationMessage"; +import { UnreadConversation } from "../entities/UnreadConversation"; /** * Conversations controller @@ -294,6 +295,17 @@ export class ConversationsController { }) } + /** + * Get the list of unread conversations of the user + * + * @param h Request handler + */ + public static async GetListUnread(h: RequestHandler) { + const list = await ConversationsHelper.GetListUnread(h.getUserId()); + + h.send(list.map(e => this.UnreadConversationToAPI(e))); + } + /** * Get and return safely a conversation ID specified in a $_POST Request * @@ -341,4 +353,19 @@ export class ConversationsController { image_path: c.hasImage ? c.imageURL : null }; } + + /** + * Turn an UnreadConversation object into an API entry + * + * @param c Target conversation + */ + private static UnreadConversationToAPI(c: UnreadConversation): any { + return { + id: c.id, + conv_name: c.name, + last_active: c.lastActive, + userID: c.userID, + message: c.message + } + } } \ No newline at end of file diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 2144a97..3664649 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -67,7 +67,9 @@ export const Routes : Route[] = [ {path: "/conversations/get_older_messages", cb: (h) => ConversationsController.GetOlderMessages(h)}, - {path: "/conversations/get_number_unread", cb: (h) => ConversationsController.CountUnreadForUser(h)} + {path: "/conversations/get_number_unread", cb: (h) => ConversationsController.CountUnreadForUser(h)}, + + {path: "/conversations/get_list_unread", cb: (h) => ConversationsController.GetListUnread(h)}, // Search controller diff --git a/src/entities/UnreadConversation.ts b/src/entities/UnreadConversation.ts new file mode 100644 index 0000000..d091274 --- /dev/null +++ b/src/entities/UnreadConversation.ts @@ -0,0 +1,13 @@ +/** + * Single unread conversation information + * + * @author Pierre HUBERT + */ + +export interface UnreadConversation { + id: number, + name: string, + lastActive: number, + userID: number, + message: string +} \ No newline at end of file diff --git a/src/helpers/ConversationsHelper.ts b/src/helpers/ConversationsHelper.ts index a401ba2..c19df64 100644 --- a/src/helpers/ConversationsHelper.ts +++ b/src/helpers/ConversationsHelper.ts @@ -2,6 +2,7 @@ import { Conversation, BaseConversation } from "../entities/Conversation"; import { DatabaseHelper } from "./DatabaseHelper"; import { time } from "../utils/DateUtils"; import { ConversationMessage, BaseConversationMessage } from "../entities/ConversationMessage"; +import { UnreadConversation } from "../entities/UnreadConversation"; /** * Conversations helper @@ -409,6 +410,52 @@ export class ConversationsHelper { }); } + /** + * Get the list of unread conversations of the user + * + * @param userID Target user ID + */ + public static async GetListUnread(userID: number) : Promise> { + return (await DatabaseHelper.Query({ + table: USERS_TABLE, + tableAlias: "users", + + joins: [ + + // Join with conversations list table + { + table: LIST_TABLE, + tableAlias: "list", + condition: "users.conv_id = list.id" + }, + + // Join with message table to get the latest message + { + table: MESSAGES_TABLE, + tableAlias: "messages", + condition: "messages.conv_id = users.conv_id" + } + ], + + where: { + "users.user_id": userID, + "users.following": 1, + "users.saw_last_message": 0, + }, + + customWhere: "list.last_active = messages.time_insert", + + order: "list.last_active DESC" + + })).map(m => { + id: m.conv_id, + name: m.name, + lastActive: m.last_active, + userID: m.user_id, + message: m.message, + }); + } + /** * Get the list of members of a conversation *