From d17ef3c9b2bb645d9c30455fdd4b87009e54288b Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 1 Apr 2020 10:44:49 +0200 Subject: [PATCH] Refactor project --- src/controllers/UserWebSocketActions.ts | 74 ++++++++++++++++++++++ src/controllers/UserWebSocketController.ts | 51 +-------------- src/controllers/UserWebSocketRoutes.ts | 4 ++ src/entities/WebSocketRequestHandler.ts | 2 +- 4 files changed, 80 insertions(+), 51 deletions(-) create mode 100644 src/controllers/UserWebSocketActions.ts diff --git a/src/controllers/UserWebSocketActions.ts b/src/controllers/UserWebSocketActions.ts new file mode 100644 index 0000000..e939643 --- /dev/null +++ b/src/controllers/UserWebSocketActions.ts @@ -0,0 +1,74 @@ +/** + * 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)); \ No newline at end of file diff --git a/src/controllers/UserWebSocketController.ts b/src/controllers/UserWebSocketController.ts index e424685..5910d2a 100644 --- a/src/controllers/UserWebSocketController.ts +++ b/src/controllers/UserWebSocketController.ts @@ -9,8 +9,6 @@ import { RequestHandler } from '../entities/RequestHandler'; 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'; import { UserWebSocketRoutes } from './UserWebSocketRoutes'; import { UserWebSocketRequestsHandler } from '../entities/WebSocketRequestHandler'; import { WsMessage } from '../entities/WsMessage'; @@ -242,54 +240,7 @@ export class UserWebSocketController { && this.active_clients.find(e => e.userID == userID && !e.incognito) == undefined; } - /** - * 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(!this.IsConnected(userID)) - continue; - - // Notify user - this.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(!this.IsConnected(userID)) - continue; - - // Notify user - this.Send(userID, "", new WsMessage({ - 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)); - -// 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 +EventsHelper.Listen("destroyed_login_tokens", (e) => UserWebSocketController.CloseClientSockets(e.client.id, e.userID)); \ No newline at end of file diff --git a/src/controllers/UserWebSocketRoutes.ts b/src/controllers/UserWebSocketRoutes.ts index 520b6f4..e07939f 100644 --- a/src/controllers/UserWebSocketRoutes.ts +++ b/src/controllers/UserWebSocketRoutes.ts @@ -9,6 +9,7 @@ import { UserWebSocketRequestsHandler } from "../entities/WebSocketRequestHandler"; import { LikesController } from "./LikesController"; +import { UserWebSocketActions } from "./UserWebSocketActions"; export interface UserWebSocketRoute { title: string, @@ -17,6 +18,9 @@ export interface UserWebSocketRoute { export const UserWebSocketRoutes: UserWebSocketRoute[] = [ + // Main controller + {title: "$main/set_incognito", handler: (h) => UserWebSocketActions.SetIncognito(h)}, + // Likes controller {title: "likes/update", handler: (h) => LikesController.Update(h)}, diff --git a/src/entities/WebSocketRequestHandler.ts b/src/entities/WebSocketRequestHandler.ts index be708d4..b107cf5 100644 --- a/src/entities/WebSocketRequestHandler.ts +++ b/src/entities/WebSocketRequestHandler.ts @@ -12,7 +12,7 @@ export class UserWebSocketRequestsHandler extends BaseRequestsHandler { private sentResponse = false; - constructor(private wsClient: ActiveClient, private req: WsMessage) { + constructor(public wsClient: ActiveClient, private req: WsMessage) { super(); }