/** * User websocket actions * * @author Pierre HUBERT */ import { UserWebSocketRequestsHandler } from "../entities/WebSocketRequestHandler"; import { UserWebSocketController } from "./UserWebSocketController"; import { WsMessage } from "../entities/WsMessage"; import { NotificationsHelper } from "../helpers/NotificationsHelper"; import { ConversationsHelper } from "../helpers/ConversationsHelper"; import { EventsHelper } from "../helpers/EventsHelper"; export class UserWebSocketActions { /** * Update incognito status of a user * * @param h Request handler */ public static async SetIncognito(h: UserWebSocketRequestsHandler) { h.wsClient.incognito = h.postBool("enable", false); } /** * Send updated notifications number to some users * * @param usersID Target users ID */ public static async SendNewNotificationsNumber(usersID: number[]) { // Process each user for(const userID of usersID) { if(!UserWebSocketController.IsConnected(userID)) continue; // Notify user UserWebSocketController.Send(userID, "", new WsMessage({ title: "number_notifs", id: "", data: await NotificationsHelper.CountUnread(userID) })); } } /** * Send upated number of unread conversations count * * @param usersID Target users ID */ public static async SendNewUnreadConversationsCount(usersID: number[]) { for(const userID of usersID) { if(!UserWebSocketController.IsConnected(userID)) continue; // Notify user UserWebSocketController.Send(userID, "", new WsMessage({ title: "number_unread_conversations", id: "", data: await ConversationsHelper.CountUnreadForUser(userID) })); } } } // When we get a new number of notifications EventsHelper.Listen("updated_number_notifications", async (e) => await UserWebSocketActions.SendNewNotificationsNumber(e.usersID)); // When we get a new number of unread conversations EventsHelper.Listen("updated_number_unread_conversations", async (e) => await UserWebSocketActions.SendNewUnreadConversationsCount(e.usersID));