mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2025-06-24 02:13:28 +00:00
Can be automatically be notified of comments creation
This commit is contained in:
@ -6,6 +6,7 @@ import { time } from "../utils/DateUtils";
|
||||
import { CommentsHelper } from "../helpers/CommentsHelper";
|
||||
import { NotifEventType } from "../entities/Notification";
|
||||
import { NotificationsHelper } from "../helpers/NotificationsHelper";
|
||||
import { AbstractUserConnectionContainer } from "../entities/UserConnectionContainer";
|
||||
|
||||
/**
|
||||
* Comments controller
|
||||
@ -145,7 +146,7 @@ export class CommentsController {
|
||||
* @param h Request handler
|
||||
* @param c Comment
|
||||
*/
|
||||
public static async CommentToAPI(h: RequestHandler, c: Comment) : Promise<any> {
|
||||
public static async CommentToAPI(h: AbstractUserConnectionContainer, c: Comment) : Promise<any> {
|
||||
return {
|
||||
ID: c.id,
|
||||
userID: c.userID,
|
||||
|
@ -13,6 +13,9 @@ import { EventsHelper } from "../helpers/EventsHelper";
|
||||
import { ConversationMessage } from "../entities/ConversationMessage";
|
||||
import { ConversationsController } from "./ConversationsController";
|
||||
import { PostAccessLevel } from "../entities/Post";
|
||||
import { Comment } from "../entities/Comment";
|
||||
import { CommentsController } from "./CommentsController";
|
||||
import { AbritraryUserConnection } from "../entities/UserConnectionContainer";
|
||||
|
||||
export class UserWebSocketActions {
|
||||
|
||||
@ -122,7 +125,7 @@ export class UserWebSocketActions {
|
||||
for(const client of UserWebSocketController.active_clients.filter(
|
||||
(e) => e.registeredConversations.has(msg.convID))) {
|
||||
|
||||
UserWebSocketController.Send(client.userID, client.socketID, new WsMessage({
|
||||
UserWebSocketController.SendToClient(client, new WsMessage({
|
||||
id: "",
|
||||
title: "new_conv_message",
|
||||
data: ConversationsController.ConversationMessageToAPI(msg)
|
||||
@ -132,6 +135,24 @@ export class UserWebSocketActions {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Propagate the creation of a new comment
|
||||
*
|
||||
* @param c New comment
|
||||
*/
|
||||
public static async CreatedNewComment(c: Comment) {
|
||||
|
||||
for(const client of UserWebSocketController.active_clients.filter((e) => e.registeredPosts.has(c.postID))) {
|
||||
|
||||
UserWebSocketController.SendToClient(client, new WsMessage({
|
||||
id: "",
|
||||
title: "new_comment",
|
||||
data: await CommentsController.CommentToAPI(new AbritraryUserConnection(client.userID), c)
|
||||
}))
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -142,4 +163,7 @@ EventsHelper.Listen("updated_number_notifications", async (e) => await UserWebSo
|
||||
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));
|
||||
EventsHelper.Listen("sent_conversation_message", async (e) => await UserWebSocketActions.SentNewConversationMessage(e.msg));
|
||||
|
||||
// When a comment is created
|
||||
EventsHelper.Listen("comment_created", async (e) => await UserWebSocketActions.CreatedNewComment(e.comment))
|
@ -221,11 +221,21 @@ export class UserWebSocketController {
|
||||
(e) => e.userID == userID
|
||||
&& (socketID.length == 0 || e.socketID == socketID)))
|
||||
{
|
||||
if(entry.ws.readyState == ws.OPEN)
|
||||
entry.ws.send(JSON.stringify(message));
|
||||
this.SendToClient(entry, message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to a specific client
|
||||
*
|
||||
* @param client Target client
|
||||
* @param message The message to send
|
||||
*/
|
||||
public static SendToClient(client: ActiveClient, message: WsMessage) {
|
||||
if(client.ws.readyState == ws.OPEN)
|
||||
client.ws.send(JSON.stringify(message));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check out whether a user has an active websocket or not
|
||||
*
|
||||
|
Reference in New Issue
Block a user