1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 13:29:22 +00:00

Can register to receive new conversation messages

This commit is contained in:
Pierre HUBERT 2020-04-01 11:31:15 +02:00
parent a1194312de
commit fab2231ace
4 changed files with 56 additions and 4 deletions

View File

@ -10,6 +10,8 @@ import { WsMessage } from "../entities/WsMessage";
import { NotificationsHelper } from "../helpers/NotificationsHelper"; import { NotificationsHelper } from "../helpers/NotificationsHelper";
import { ConversationsHelper } from "../helpers/ConversationsHelper"; import { ConversationsHelper } from "../helpers/ConversationsHelper";
import { EventsHelper } from "../helpers/EventsHelper"; import { EventsHelper } from "../helpers/EventsHelper";
import { ConversationMessage } from "../entities/ConversationMessage";
import { ConversationsController } from "./ConversationsController";
export class UserWebSocketActions { export class UserWebSocketActions {
@ -20,6 +22,29 @@ export class UserWebSocketActions {
*/ */
public static async SetIncognito(h: UserWebSocketRequestsHandler) { public static async SetIncognito(h: UserWebSocketRequestsHandler) {
h.wsClient.incognito = h.postBool("enable", false); 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)
}));
}
}
} }
@ -72,3 +116,6 @@ EventsHelper.Listen("updated_number_notifications", async (e) => await UserWebSo
// When we get a new number of unread conversations // 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));

View File

@ -26,7 +26,9 @@ export interface ActiveClient {
clientID: number, clientID: number,
userID: number, userID: number,
ws: ws, ws: ws,
incognito: boolean incognito: boolean,
registeredConversations: Set<number>,
} }
// Tokens are valid only 10 seconds after they are generated // Tokens are valid only 10 seconds after they are generated
@ -116,7 +118,8 @@ export class UserWebSocketController {
clientID: entry.clientID, clientID: entry.clientID,
userID: entry.userID, userID: entry.userID,
ws: ws, ws: ws,
incognito: entry.incognito incognito: entry.incognito,
registeredConversations: new Set()
} }
this.active_clients.push(client); this.active_clients.push(client);

View File

@ -20,6 +20,8 @@ export const UserWebSocketRoutes: UserWebSocketRoute[] = [
// Main controller // Main controller
{title: "$main/set_incognito", handler: (h) => UserWebSocketActions.SetIncognito(h)}, {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 // Likes controller
{title: "likes/update", handler: (h) => LikesController.Update(h)}, {title: "likes/update", handler: (h) => LikesController.Update(h)},

View File

@ -43,7 +43,7 @@ export class UserWebSocketRequestsHandler extends BaseRequestsHandler {
throw new Error("User WS error ("+code+"): "+message); throw new Error("User WS error ("+code+"): "+message);
} }
public success(message: string): void { public success(message: string = ""): void {
this.sendResponse("success", message); this.sendResponse("success", message);
} }