diff --git a/src/controllers/ConversationsController.ts b/src/controllers/ConversationsController.ts index 2d52d2d..a8ba941 100644 --- a/src/controllers/ConversationsController.ts +++ b/src/controllers/ConversationsController.ts @@ -283,6 +283,17 @@ export class ConversationsController { h.send(messages.map(e => this.ConversationMessageToAPI(e))); } + /** + * Get the number of unread conversations for the user + * + * @param h Request handler + */ + public static async CountUnreadForUser(h: RequestHandler) { + h.send({ + nb_unread: await ConversationsHelper.CountUnreadForUser(h.getUserId()) + }) + } + /** * 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 a17a6d3..2144a97 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -66,6 +66,8 @@ export const Routes : Route[] = [ {path: "/conversations/sendMessage", cb: (h) => ConversationsController.SendMessage(h)}, {path: "/conversations/get_older_messages", cb: (h) => ConversationsController.GetOlderMessages(h)}, + + {path: "/conversations/get_number_unread", cb: (h) => ConversationsController.CountUnreadForUser(h)} // Search controller diff --git a/src/helpers/ConversationsHelper.ts b/src/helpers/ConversationsHelper.ts index 5bda853..a401ba2 100644 --- a/src/helpers/ConversationsHelper.ts +++ b/src/helpers/ConversationsHelper.ts @@ -393,6 +393,22 @@ export class ConversationsHelper { return result.map(r => r.conv_id); } + /** + * Count the number of unread conversations of the user + * + * @param userID Target user ID + */ + public static async CountUnreadForUser(userID: number) : Promise { + return await DatabaseHelper.Count({ + table: USERS_TABLE, + where: { + user_id: userID, + saw_last_message: 0, + following: 1 + } + }); + } + /** * Get the list of members of a conversation *