From 29b446f20be3f8df51e01472a178e447d5f1ee9e Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 2 Apr 2020 18:56:42 +0200 Subject: [PATCH] Create new helper method --- src/controllers/UserWebSocketActions.ts | 17 +++++------------ src/controllers/UserWebSocketController.ts | 18 ++++++++++++++++++ src/entities/WsMessage.ts | 15 +++++++++++++++ 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/controllers/UserWebSocketActions.ts b/src/controllers/UserWebSocketActions.ts index 5a3d2e4..c26b6bd 100644 --- a/src/controllers/UserWebSocketActions.ts +++ b/src/controllers/UserWebSocketActions.ts @@ -123,18 +123,11 @@ export class UserWebSocketActions { * @param msg New message */ public static async SentNewConversationMessage(msg: ConversationMessage) { - for(const client of UserWebSocketController.active_clients.filter( - (e) => e.registeredConversations.has(msg.convID))) { - - UserWebSocketController.SendToClient(client, new WsMessage({ - id: "", - title: "new_conv_message", - data: ConversationsController.ConversationMessageToAPI(msg) - })); - - await ConversationsHelper.MarkUserSeen(msg.convID, client.userID); - } - + await UserWebSocketController.SendToSpecifcClients( + (e) => e.registeredConversations.has(msg.convID), + () => WsMessage.NoIDMessage("new_conv_message", ConversationsController.ConversationMessageToAPI(msg)), + async (c) => await ConversationsHelper.MarkUserSeen(msg.convID, c.userID) + ) } /** diff --git a/src/controllers/UserWebSocketController.ts b/src/controllers/UserWebSocketController.ts index f995600..a5ec5be 100644 --- a/src/controllers/UserWebSocketController.ts +++ b/src/controllers/UserWebSocketController.ts @@ -232,6 +232,24 @@ export class UserWebSocketController { client.ws.send(JSON.stringify(message)); } + /** + * Send an adapted message only to some clients on some conditions + * + * @param filter The filter to apply to the connections + * @param genMsg The message to send to a client once it has been selected + * @param afterSend Optional asynchronous callback called for each client + * after a message was sent + */ + public static async SendToSpecifcClients(filter: (c: ActiveClient) => boolean, + genMsg: (c: ActiveClient) => Promise | WsMessage, afterSend ?: (c: ActiveClient) => Promise) { + for(const client of UserWebSocketController.active_clients.filter((e) => filter(e))) { + UserWebSocketController.SendToClient(client, await genMsg(client)); + + if(afterSend) + await afterSend(client); + } + } + /** * Check out whether a user has an active websocket or not * diff --git a/src/entities/WsMessage.ts b/src/entities/WsMessage.ts index 7534cb7..3a0cbed 100644 --- a/src/entities/WsMessage.ts +++ b/src/entities/WsMessage.ts @@ -22,6 +22,21 @@ export class WsMessage implements WsMessageBuilder { } } + /** + * Construct quickly a message with no ID (to propagate + * events to clients) + * + * @param title The title of message + * @param data Data associated with the message + */ + public static NoIDMessage(title: string, data: any) : WsMessage { + return new this({ + id: "", + title: title, + data: data + }); + } + get isValidRequest() : boolean { return typeof this.id === "string" && typeof this.title === "string"