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";
|
2020-04-01 09:31:15 +00:00
|
|
|
import { ConversationMessage } from "../entities/ConversationMessage";
|
|
|
|
import { ConversationsController } from "./ConversationsController";
|
2020-04-01 15:26:56 +00:00
|
|
|
import { PostAccessLevel } from "../entities/Post";
|
2020-04-01 15:51:33 +00:00
|
|
|
import { Comment } from "../entities/Comment";
|
|
|
|
import { CommentsController } from "./CommentsController";
|
|
|
|
import { AbritraryUserConnection } from "../entities/UserConnectionContainer";
|
2020-04-02 08:54:03 +00:00
|
|
|
import { CommentsHelper } from "../helpers/CommentsHelper";
|
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);
|
2020-04-01 09:31:15 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-01 15:26:56 +00:00
|
|
|
/**
|
|
|
|
* Request to be notified of post changes
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async RegisterPost(h: UserWebSocketRequestsHandler) {
|
|
|
|
const postID = await h.postPostIDWithAccess("postID", PostAccessLevel.BASIC_ACCESS);
|
|
|
|
h.wsClient.registeredPosts.add(postID);
|
|
|
|
h.success();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregister a post
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async UnregisterPost(h: UserWebSocketRequestsHandler) {
|
|
|
|
const postID = h.postInt("postID"); // Warning ! we do not check post access level here because it is not required!
|
|
|
|
h.wsClient.registeredPosts.delete(postID);
|
|
|
|
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
|
2020-04-01 15:55:44 +00:00
|
|
|
UserWebSocketController.Send(userID, new WsMessage({
|
2020-04-01 08:44:49 +00:00
|
|
|
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
|
2020-04-01 15:55:44 +00:00
|
|
|
UserWebSocketController.Send(userID, new WsMessage({
|
2020-04-01 08:44:49 +00:00
|
|
|
title: "number_unread_conversations",
|
|
|
|
id: "",
|
|
|
|
data: await ConversationsHelper.CountUnreadForUser(userID)
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 09:31:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2020-04-01 15:51:33 +00:00
|
|
|
UserWebSocketController.SendToClient(client, new WsMessage({
|
2020-04-01 09:31:15 +00:00
|
|
|
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 09:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-04-01 15:51:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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",
|
2020-04-02 07:47:07 +00:00
|
|
|
data: await CommentsController.CommentToAPI(new AbritraryUserConnection(client.userID), c, true)
|
2020-04-01 15:51:33 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2020-04-02 08:54:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Propagate comment update
|
|
|
|
*
|
2020-04-02 16:25:23 +00:00
|
|
|
* @param c Updated comment
|
2020-04-02 08:54:03 +00:00
|
|
|
*/
|
|
|
|
public static async UpdatedComment(commentID: number) {
|
|
|
|
const comment = await CommentsHelper.GetSingle(commentID);
|
|
|
|
|
2020-04-02 08:57:38 +00:00
|
|
|
for(const client of UserWebSocketController.active_clients.filter((e) => e.registeredPosts.has(comment.postID))) {
|
|
|
|
UserWebSocketController.SendToClient(client, new WsMessage({
|
|
|
|
id: "",
|
|
|
|
title: "comment_updated",
|
2020-04-02 09:07:43 +00:00
|
|
|
data: await CommentsController.CommentToAPI(new AbritraryUserConnection(client.userID), comment)
|
2020-04-02 08:57:38 +00:00
|
|
|
}))
|
|
|
|
}
|
2020-04-02 08:54:03 +00:00
|
|
|
}
|
2020-04-02 16:25:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Propagage the deletion of a comment
|
|
|
|
*
|
|
|
|
* @param comment Deleted comment
|
|
|
|
*/
|
|
|
|
public static async DeletedComment(comment: Comment) {
|
|
|
|
console.log(comment);
|
|
|
|
}
|
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
|
2020-04-01 09:31:15 +00:00
|
|
|
EventsHelper.Listen("updated_number_unread_conversations", async (e) => await UserWebSocketActions.SendNewUnreadConversationsCount(e.usersID));
|
|
|
|
|
|
|
|
// When a new message is sent
|
2020-04-01 15:51:33 +00:00
|
|
|
EventsHelper.Listen("sent_conversation_message", async (e) => await UserWebSocketActions.SentNewConversationMessage(e.msg));
|
|
|
|
|
2020-04-02 16:25:23 +00:00
|
|
|
// When a comment is created / updated / deleted
|
2020-04-02 08:54:03 +00:00
|
|
|
EventsHelper.Listen("comment_created", async (e) => await UserWebSocketActions.CreatedNewComment(e.comment))
|
2020-04-02 16:25:23 +00:00
|
|
|
EventsHelper.Listen("comment_updated", async (e) => await UserWebSocketActions.UpdatedComment(e.commentID));
|
|
|
|
EventsHelper.Listen("comment_deleted", async (e) => await UserWebSocketActions.DeletedComment(e.comment));
|