1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-06-24 02:13:28 +00:00

Create new helper method

This commit is contained in:
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
*/
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)
)
}
/**

View File

@ -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> | 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
*