diff --git a/src/controllers/UserWebSocketActions.ts b/src/controllers/UserWebSocketActions.ts index e939643..1f14794 100644 --- a/src/controllers/UserWebSocketActions.ts +++ b/src/controllers/UserWebSocketActions.ts @@ -10,6 +10,8 @@ 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"; export class UserWebSocketActions { @@ -20,6 +22,29 @@ export class UserWebSocketActions { */ 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(); } @@ -64,6 +89,25 @@ export class UserWebSocketActions { })); } } + + /** + * 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))) { + + UserWebSocketController.Send(client.clientID, client.socketID, new WsMessage({ + id: "", + title: "new_conv_message", + data: ConversationsController.ConversationMessageToAPI(msg) + })); + + } + + } } @@ -71,4 +115,7 @@ export class UserWebSocketActions { 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)); \ No newline at end of file +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)); \ No newline at end of file diff --git a/src/controllers/UserWebSocketController.ts b/src/controllers/UserWebSocketController.ts index 5910d2a..5680607 100644 --- a/src/controllers/UserWebSocketController.ts +++ b/src/controllers/UserWebSocketController.ts @@ -26,7 +26,9 @@ export interface ActiveClient { clientID: number, userID: number, ws: ws, - incognito: boolean + incognito: boolean, + + registeredConversations: Set, } // Tokens are valid only 10 seconds after they are generated @@ -116,7 +118,8 @@ export class UserWebSocketController { clientID: entry.clientID, userID: entry.userID, ws: ws, - incognito: entry.incognito + incognito: entry.incognito, + registeredConversations: new Set() } this.active_clients.push(client); diff --git a/src/controllers/UserWebSocketRoutes.ts b/src/controllers/UserWebSocketRoutes.ts index e07939f..2bb1756 100644 --- a/src/controllers/UserWebSocketRoutes.ts +++ b/src/controllers/UserWebSocketRoutes.ts @@ -20,6 +20,8 @@ export const UserWebSocketRoutes: UserWebSocketRoute[] = [ // Main controller {title: "$main/set_incognito", handler: (h) => UserWebSocketActions.SetIncognito(h)}, + {title: "$main/register_conv", handler: (h) => UserWebSocketActions.RegisterConv(h)}, + {title: "$main/unregister_conv", handler: (h) => UserWebSocketActions.UnregisterConv(h)}, // Likes controller {title: "likes/update", handler: (h) => LikesController.Update(h)}, diff --git a/src/entities/WebSocketRequestHandler.ts b/src/entities/WebSocketRequestHandler.ts index b107cf5..1d364fc 100644 --- a/src/entities/WebSocketRequestHandler.ts +++ b/src/entities/WebSocketRequestHandler.ts @@ -43,7 +43,7 @@ export class UserWebSocketRequestsHandler extends BaseRequestsHandler { throw new Error("User WS error ("+code+"): "+message); } - public success(message: string): void { + public success(message: string = ""): void { this.sendResponse("success", message); }