mirror of
				https://gitlab.com/comunic/comunicapiv2
				synced 2025-10-31 01:24:43 +00:00 
			
		
		
		
	Can register to receive new conversation messages
This commit is contained in:
		| @@ -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)); | ||||
| 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)); | ||||
| @@ -26,7 +26,9 @@ export interface ActiveClient { | ||||
| 	clientID: number, | ||||
| 	userID: number, | ||||
| 	ws: ws, | ||||
| 	incognito: boolean | ||||
| 	incognito: boolean, | ||||
|  | ||||
| 	registeredConversations: Set<number>, | ||||
| } | ||||
|  | ||||
| // 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); | ||||
|  | ||||
|   | ||||
| @@ -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)}, | ||||
|   | ||||
| @@ -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); | ||||
| 	} | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user