mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-22 13:29:22 +00:00
Refactor project
This commit is contained in:
parent
99af649cfe
commit
d17ef3c9b2
74
src/controllers/UserWebSocketActions.ts
Normal file
74
src/controllers/UserWebSocketActions.ts
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
/**
|
||||||
|
* 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";
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
UserWebSocketController.Send(userID, "", new WsMessage({
|
||||||
|
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
|
||||||
|
UserWebSocketController.Send(userID, "", new WsMessage({
|
||||||
|
title: "number_unread_conversations",
|
||||||
|
id: "",
|
||||||
|
data: await ConversationsHelper.CountUnreadForUser(userID)
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 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
|
||||||
|
EventsHelper.Listen("updated_number_unread_conversations", async (e) => await UserWebSocketActions.SendNewUnreadConversationsCount(e.usersID));
|
@ -9,8 +9,6 @@ import { RequestHandler } from '../entities/RequestHandler';
|
|||||||
import { time } from '../utils/DateUtils';
|
import { time } from '../utils/DateUtils';
|
||||||
import { randomStr } from '../utils/CryptUtils';
|
import { randomStr } from '../utils/CryptUtils';
|
||||||
import { EventsHelper } from '../helpers/EventsHelper';
|
import { EventsHelper } from '../helpers/EventsHelper';
|
||||||
import { NotificationsHelper } from '../helpers/NotificationsHelper';
|
|
||||||
import { ConversationsHelper } from '../helpers/ConversationsHelper';
|
|
||||||
import { UserWebSocketRoutes } from './UserWebSocketRoutes';
|
import { UserWebSocketRoutes } from './UserWebSocketRoutes';
|
||||||
import { UserWebSocketRequestsHandler } from '../entities/WebSocketRequestHandler';
|
import { UserWebSocketRequestsHandler } from '../entities/WebSocketRequestHandler';
|
||||||
import { WsMessage } from '../entities/WsMessage';
|
import { WsMessage } from '../entities/WsMessage';
|
||||||
@ -242,54 +240,7 @@ export class UserWebSocketController {
|
|||||||
&& this.active_clients.find(e => e.userID == userID && !e.incognito) == undefined;
|
&& this.active_clients.find(e => e.userID == userID && !e.incognito) == undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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(!this.IsConnected(userID))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Notify user
|
|
||||||
this.Send(userID, "", new WsMessage({
|
|
||||||
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(!this.IsConnected(userID))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Notify user
|
|
||||||
this.Send(userID, "", new WsMessage({
|
|
||||||
title: "number_unread_conversations",
|
|
||||||
id: "",
|
|
||||||
data: await ConversationsHelper.CountUnreadForUser(userID)
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// When user sign out
|
// When user sign out
|
||||||
EventsHelper.Listen("destroyed_login_tokens", (e) => UserWebSocketController.CloseClientSockets(e.client.id, e.userID));
|
EventsHelper.Listen("destroyed_login_tokens", (e) => UserWebSocketController.CloseClientSockets(e.client.id, e.userID));
|
||||||
|
|
||||||
// When we get a new number of notifications
|
|
||||||
EventsHelper.Listen("updated_number_notifications", async (e) => await UserWebSocketController.SendNewNotificationsNumber(e.usersID));
|
|
||||||
|
|
||||||
// When we get a new number of unread conversations
|
|
||||||
EventsHelper.Listen("updated_number_unread_conversations", async (e) => await UserWebSocketController.SendNewUnreadConversationsCount(e.usersID));
|
|
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
import { UserWebSocketRequestsHandler } from "../entities/WebSocketRequestHandler";
|
import { UserWebSocketRequestsHandler } from "../entities/WebSocketRequestHandler";
|
||||||
import { LikesController } from "./LikesController";
|
import { LikesController } from "./LikesController";
|
||||||
|
import { UserWebSocketActions } from "./UserWebSocketActions";
|
||||||
|
|
||||||
export interface UserWebSocketRoute {
|
export interface UserWebSocketRoute {
|
||||||
title: string,
|
title: string,
|
||||||
@ -17,6 +18,9 @@ export interface UserWebSocketRoute {
|
|||||||
|
|
||||||
export const UserWebSocketRoutes: UserWebSocketRoute[] = [
|
export const UserWebSocketRoutes: UserWebSocketRoute[] = [
|
||||||
|
|
||||||
|
// Main controller
|
||||||
|
{title: "$main/set_incognito", handler: (h) => UserWebSocketActions.SetIncognito(h)},
|
||||||
|
|
||||||
// Likes controller
|
// Likes controller
|
||||||
{title: "likes/update", handler: (h) => LikesController.Update(h)},
|
{title: "likes/update", handler: (h) => LikesController.Update(h)},
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ export class UserWebSocketRequestsHandler extends BaseRequestsHandler {
|
|||||||
|
|
||||||
private sentResponse = false;
|
private sentResponse = false;
|
||||||
|
|
||||||
constructor(private wsClient: ActiveClient, private req: WsMessage) {
|
constructor(public wsClient: ActiveClient, private req: WsMessage) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user