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:
parent
a4dcf49d74
commit
29b446f20b
@ -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)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user