From 197fa12cde6840981b5abffa85d7321e34109002 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 31 Mar 2020 10:49:31 +0200 Subject: [PATCH] Send new number unread conversations for user --- src/controllers/UserWebSocketController.ts | 26 +++++++++++++++++++++- src/helpers/ConversationsHelper.ts | 9 +++++++- src/helpers/EventsHelper.ts | 6 +++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/controllers/UserWebSocketController.ts b/src/controllers/UserWebSocketController.ts index 4ca57e4..c34322b 100644 --- a/src/controllers/UserWebSocketController.ts +++ b/src/controllers/UserWebSocketController.ts @@ -10,6 +10,7 @@ import { time } from '../utils/DateUtils'; import { randomStr } from '../utils/CryptUtils'; import { EventsHelper } from '../helpers/EventsHelper'; import { NotificationsHelper } from '../helpers/NotificationsHelper'; +import { ConversationsHelper } from '../helpers/ConversationsHelper'; interface PendingRequests { time: number, @@ -208,10 +209,33 @@ export class UserWebSocketController { } } + + /** + * Send upated number of unread conversations count + * + * @param usersID Target users ID + */ + public static async SendNewUnreadConversationsCount(usersID: number[]) { + for(const userID of usersID) { + + if(!this.IsConnected(userID)) + continue; + + // Notify user + this.Send(userID, "", { + title: "number_unread_conversations", + id: "", + data: await ConversationsHelper.CountUnreadForUser(userID) + }); + } + } } // When user sign out EventsHelper.Listen("destroyed_login_tokens", (e) => UserWebSocketController.CloseClientSockets(e.client.id, e.userID)); // When we get a new number of notifications -EventsHelper.Listen("updated_number_notifications", async (e) => await UserWebSocketController.SendNewNotificationsNumber(e.usersID)); \ No newline at end of file +EventsHelper.Listen("updated_number_notifications", async (e) => await UserWebSocketController.SendNewNotificationsNumber(e.usersID)); + +// When we get a new number of unread conversations +EventsHelper.Listen("updated_number_unread_conversations", async (e) => await UserWebSocketController.SendNewUnreadConversationsCount(e.usersID)); \ No newline at end of file diff --git a/src/helpers/ConversationsHelper.ts b/src/helpers/ConversationsHelper.ts index 5eaea03..f320423 100644 --- a/src/helpers/ConversationsHelper.ts +++ b/src/helpers/ConversationsHelper.ts @@ -4,6 +4,7 @@ import { time } from "../utils/DateUtils"; import { ConversationMessage, BaseConversationMessage } from "../entities/ConversationMessage"; import { UnreadConversation } from "../entities/UnreadConversation"; import { existsSync, unlinkSync } from "fs"; +import { EventsHelper } from "./EventsHelper"; /** * Conversations helper @@ -407,7 +408,7 @@ export class ConversationsHelper { * @param userID Target user ID */ public static async MarkUserSeen(convID: number, userID: number) { - await DatabaseHelper.UpdateRows({ + const count = await DatabaseHelper.UpdateRows({ table: USERS_TABLE, where: { conv_id: convID, @@ -417,6 +418,12 @@ export class ConversationsHelper { saw_last_message: 1 } }); + + // Send an event if required + if(count > 0) + await EventsHelper.Emit("updated_number_unread_conversations", { + usersID: [userID] + }) } /** diff --git a/src/helpers/EventsHelper.ts b/src/helpers/EventsHelper.ts index de0d3fe..965d61b 100644 --- a/src/helpers/EventsHelper.ts +++ b/src/helpers/EventsHelper.ts @@ -18,12 +18,18 @@ export interface UpdatedNotificationsNumberEvent { usersID: number[] } +// When some users have an updated number of unread conversations +export interface UpdateNumberUnreadConversations { + usersID: number[] +} + /** * Global map of all possible events */ export interface EventsMap { "destroyed_login_tokens": DestroyedLoginTokensEvent, "updated_number_notifications": UpdatedNotificationsNumberEvent, + "updated_number_unread_conversations": UpdateNumberUnreadConversations } export class EventsHelper {