1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 05:19:22 +00:00

Create new helper method

This commit is contained in:
Pierre HUBERT 2020-04-02 18:56:42 +02:00
parent a4dcf49d74
commit 29b446f20b
3 changed files with 38 additions and 12 deletions

View File

@ -123,18 +123,11 @@ export class UserWebSocketActions {
* @param msg New message * @param msg New message
*/ */
public static async SentNewConversationMessage(msg: ConversationMessage) { public static async SentNewConversationMessage(msg: ConversationMessage) {
for(const client of UserWebSocketController.active_clients.filter( await UserWebSocketController.SendToSpecifcClients(
(e) => e.registeredConversations.has(msg.convID))) { (e) => e.registeredConversations.has(msg.convID),
() => WsMessage.NoIDMessage("new_conv_message", ConversationsController.ConversationMessageToAPI(msg)),
UserWebSocketController.SendToClient(client, new WsMessage({ async (c) => await ConversationsHelper.MarkUserSeen(msg.convID, c.userID)
id: "", )
title: "new_conv_message",
data: ConversationsController.ConversationMessageToAPI(msg)
}));
await ConversationsHelper.MarkUserSeen(msg.convID, client.userID);
}
} }
/** /**

View File

@ -232,6 +232,24 @@ export class UserWebSocketController {
client.ws.send(JSON.stringify(message)); 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> | WsMessage, afterSend ?: (c: ActiveClient) => Promise<void>) {
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 * Check out whether a user has an active websocket or not
* *

View File

@ -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 { get isValidRequest() : boolean {
return typeof this.id === "string" return typeof this.id === "string"
&& typeof this.title === "string" && typeof this.title === "string"