mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-22 05:19:22 +00:00
Notify through websocket new number of notifications
This commit is contained in:
parent
d18914066b
commit
9816caf4db
@ -9,6 +9,7 @@ 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';
|
||||||
|
|
||||||
interface PendingRequests {
|
interface PendingRequests {
|
||||||
time: number,
|
time: number,
|
||||||
@ -18,11 +19,18 @@ interface PendingRequests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface ActiveClient {
|
interface ActiveClient {
|
||||||
|
socketID: string,
|
||||||
clientID: number,
|
clientID: number,
|
||||||
userID: number,
|
userID: number,
|
||||||
ws: ws
|
ws: ws
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface WsMessage {
|
||||||
|
id: string,
|
||||||
|
title: string,
|
||||||
|
data: any
|
||||||
|
}
|
||||||
|
|
||||||
// Tokens are valid only 10 seconds after they are generated
|
// Tokens are valid only 10 seconds after they are generated
|
||||||
const TOKENS_DURATION = 10
|
const TOKENS_DURATION = 10
|
||||||
const TOKEN_LENGTH = 20
|
const TOKEN_LENGTH = 20
|
||||||
@ -105,6 +113,7 @@ export class UserWebSocketController {
|
|||||||
|
|
||||||
// Add the client to the list of active clients
|
// Add the client to the list of active clients
|
||||||
const client: ActiveClient = {
|
const client: ActiveClient = {
|
||||||
|
socketID: randomStr(30),
|
||||||
clientID: entry.clientID,
|
clientID: entry.clientID,
|
||||||
userID: entry.userID,
|
userID: entry.userID,
|
||||||
ws: ws
|
ws: ws
|
||||||
@ -150,7 +159,59 @@ export class UserWebSocketController {
|
|||||||
for(const entry of this.active_clients.filter((f) => f.clientID == clientID && f.userID == userID))
|
for(const entry of this.active_clients.filter((f) => f.clientID == clientID && f.userID == userID))
|
||||||
entry.ws.close();
|
entry.ws.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a message to a socket
|
||||||
|
*
|
||||||
|
* @param userID Target user ID
|
||||||
|
* @param socketID Target socket ID (if null the message is sent to
|
||||||
|
* all the active sockets of the user)
|
||||||
|
* @param message The message to send
|
||||||
|
*/
|
||||||
|
public static Send(userID: number, socketID: string, message: WsMessage) {
|
||||||
|
for(const entry of this.active_clients.filter(
|
||||||
|
(e) => e.userID == userID
|
||||||
|
&& (socketID.length == 0 || e.socketID == socketID)))
|
||||||
|
{
|
||||||
|
if(entry.ws.readyState == ws.OPEN)
|
||||||
|
entry.ws.send(JSON.stringify(message));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check out whether a user has an active websocket or not
|
||||||
|
*
|
||||||
|
* @param userID Target user ID
|
||||||
|
*/
|
||||||
|
public static IsConnected(userID: number) : boolean {
|
||||||
|
return this.active_clients.find((e) => e.userID == userID) != 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, "", {
|
||||||
|
title: "number_notifs",
|
||||||
|
id: "",
|
||||||
|
data: await NotificationsHelper.CountUnread(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));
|
@ -13,11 +13,17 @@ export interface DestroyedLoginTokensEvent {
|
|||||||
client: APIClient
|
client: APIClient
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// When some user might have an updated number of notifications
|
||||||
|
export interface UpdatedNotificationsNumberEvent {
|
||||||
|
usersID: number[]
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Global map of all possible events
|
* Global map of all possible events
|
||||||
*/
|
*/
|
||||||
export interface EventsMap {
|
export interface EventsMap {
|
||||||
"destroyed_login_tokens": DestroyedLoginTokensEvent
|
"destroyed_login_tokens": DestroyedLoginTokensEvent,
|
||||||
|
"updated_number_notifications": UpdatedNotificationsNumberEvent,
|
||||||
}
|
}
|
||||||
|
|
||||||
export class EventsHelper {
|
export class EventsHelper {
|
||||||
|
@ -6,6 +6,7 @@ import { PostPageKind, PostVisibilityLevel } from "../entities/Post";
|
|||||||
import { FriendsHelper } from "./FriendsHelper";
|
import { FriendsHelper } from "./FriendsHelper";
|
||||||
import { GroupsHelper } from "./GroupsHelper";
|
import { GroupsHelper } from "./GroupsHelper";
|
||||||
import { GroupMembershipLevels } from "../entities/GroupMember";
|
import { GroupMembershipLevels } from "../entities/GroupMember";
|
||||||
|
import { EventsHelper } from "./EventsHelper";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notifications helper
|
* Notifications helper
|
||||||
@ -308,6 +309,9 @@ export class NotificationsHelper {
|
|||||||
NOTIFICATIONS_TABLE,
|
NOTIFICATIONS_TABLE,
|
||||||
this.NotifToDB(n, true)
|
this.NotifToDB(n, true)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Trigger notify system
|
||||||
|
await EventsHelper.Emit("updated_number_notifications", {usersID: [n.destUserID]});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user