1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-27 07:49:22 +00:00
comunicapiv2/src/controllers/UserWebSocketActions.ts

122 lines
3.4 KiB
TypeScript
Raw Normal View History

2020-04-01 08:44:49 +00:00
/**
* 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";
import { ConversationMessage } from "../entities/ConversationMessage";
import { ConversationsController } from "./ConversationsController";
2020-04-01 08:44:49 +00:00
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);
h.success();
}
/**
* Register to a conversation
*
* @param h Request handler
*/
public static async RegisterConv(h: UserWebSocketRequestsHandler) {
const convID = await h.postConversationId("convID");
h.wsClient.registeredConversations.add(convID);
h.success();
}
/**
* Unregister a conversation
*
* @param h Request handler
*/
public static async UnregisterConv(h: UserWebSocketRequestsHandler) {
const convID = await h.postConversationId("convID");
h.wsClient.registeredConversations.delete(convID);
h.success();
2020-04-01 08:44:49 +00:00
}
/**
* 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)
}));
}
}
/**
* Propagate a new conversation message
*
* @param msg New message
*/
public static async SentNewConversationMessage(msg: ConversationMessage) {
for(const client of UserWebSocketController.active_clients.filter(
(e) => e.registeredConversations.has(msg.convID))) {
2020-04-01 12:28:39 +00:00
UserWebSocketController.Send(client.userID, client.socketID, new WsMessage({
id: "",
title: "new_conv_message",
data: ConversationsController.ConversationMessageToAPI(msg)
}));
2020-04-01 12:33:54 +00:00
await ConversationsHelper.MarkUserSeen(msg.convID, client.userID);
}
}
2020-04-01 08:44:49 +00:00
}
// 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));
// When a new message is sent
EventsHelper.Listen("sent_conversation_message", async (e) => await UserWebSocketActions.SentNewConversationMessage(e.msg));