Remove 3 more

This commit is contained in:
Romaric Mourgues
2023-04-20 15:00:26 +02:00
parent c74ce7799a
commit 335301612a
9 changed files with 6 additions and 307 deletions
@@ -3,7 +3,6 @@ import Logger from 'app/features/global/framework/logger-service';
import AccessRightsService from 'app/features/workspace-members/services/workspace-members-access-rights-service';
import CurrentUser from 'app/deprecated/user/CurrentUser';
import JWT from 'app/features/auth/jwt-storage-service';
import WorkspacesListener from '../../workspaces/services/workspaces-listener-service';
import LocalStorage from '../../global/framework/local-storage-service';
import WebSocket from '../../global/types/websocket-types';
@@ -29,14 +28,11 @@ class Application {
const ws = WebSocket.get();
ws.connect();
WorkspacesListener.startListen();
AccessRightsService.resetLevels();
CurrentUser.start();
}
stop(): void {
WorkspacesListener.cancelListen();
LocalStorage.clear();
JWT.clear();
}
@@ -1,26 +0,0 @@
import { useState, useEffect } from 'react';
import { isArray } from 'lodash';
import UserListenerService from '@features/users/services/listen-users-service';
import Collections from '@deprecated/CollectionsV1/Collections/Collections';
import UsersService from '@features/users/services/current-user-service';
import userAsyncGet from '@features/users/utils/async-get';
export const useUsersListener = (usersIds: string[] = []) => {
const users = (isArray(usersIds) ? usersIds : []).filter(
e => (usersIds.length || 0) === 1 || e !== UsersService.getCurrentUserId(),
);
Collections.get('users').useListener(useState, users);
useEffect(() => {
users.forEach(userId => {
UserListenerService.listenUser(userId);
userAsyncGet(userId);
});
return () => {
users.forEach(userId => UserListenerService.cancelListenUser(userId));
};
}, [users]);
return users;
};
@@ -1,156 +0,0 @@
/* eslint-disable @typescript-eslint/no-this-alias */
/* eslint-disable @typescript-eslint/no-explicit-any */
import ws from '@deprecated/websocket/websocket';
import Collections from '@deprecated/CollectionsV1/Collections/Collections';
import UserService from './current-user-service';
import Globals from '@features/global/services/globals-tdrive-app-service';
import userAsyncGet from '@features/users/utils/async-get';
type Timeout = ReturnType<typeof setTimeout>;
class ListenUsers {
users_repository: any;
connectedPing: { [key: string]: Timeout };
listenerCount: { [key: string]: number };
pingTimeouts: { [key: string]: Timeout };
was_connected_last_check: { [key: string]: boolean };
lastPong: number;
constructor() {
this.users_repository = Collections.get('users');
this.listenerCount = {};
this.connectedPing = {};
this.pingTimeouts = {};
this.was_connected_last_check = {};
this.lastPong = 0;
(Globals.window as any).listenUsers = this;
}
/**
* Check if the given user is active: new Date().getTime() - ws.lastAlive < 1000*60*5
*
* @param idUser
*/
ping(idUser: string): void {
ws.publish(`users/${idUser}`, {
ping: true,
user: { connected: true, id: UserService.getCurrentUserId() },
});
if (this.pingTimeouts[idUser]) {
clearTimeout(this.pingTimeouts[idUser]);
}
this.pingTimeouts[idUser] = setTimeout(() => {
//Only say this to me !
const user = Collections.get('users').find(idUser);
if (user) {
if (user.connected) {
user.connected = false;
this.users_repository.updateObject(user);
}
}
this.was_connected_last_check[idUser] = false;
}, 5000);
}
pong(): void {
this.lastPong = new Date().getTime();
ws.publish(`users/${UserService.getCurrentUserId()}`, {
user: { connected: true, id: UserService.getCurrentUserId() },
});
}
listenUser(idUser: string): void {
if (!idUser) {
return;
}
if (!this.listenerCount[idUser]) {
this.listenerCount[idUser] = 0;
}
this.listenerCount[idUser] += 1;
const that = this;
if (this.listenerCount[idUser] === 1) {
ws.subscribe(
`users/${idUser}`,
(_uri: string, data: any) => {
/*if (idUser == UserServiceImpl.getCurrentUserId()) {
if (data.ping) {
that.pong();
}
}*/
if (data.user && data.user.id) {
/*that.setUserPingTimeout(data.user.id);
if (data.user.connected && that.pingTimeouts[idUser]) {
clearTimeout(that.pingTimeouts[idUser]);
}*/
if (that.users_repository.find(data.user.id)) {
that.was_connected_last_check[idUser] = data.user.connected;
if (
data.user.username ||
data.user.notifications_preferences ||
data.user.connected !== that.users_repository.find(data.user.id).connected
) {
that.users_repository.updateObject(data.user);
}
} else if (data.user.id) {
userAsyncGet(data.user.id);
}
}
},
null,
);
if (idUser !== UserService.getCurrentUserId()) {
/*setTimeout(() => {
if (idUser != UserServiceImpl.getCurrentUserId()) {
this.setUserPingTimeout(idUser);
this.ping(idUser);
}
}, 1000);
if (new Date().getTime() - this.lastPong > 60000) {
this.pong();
}*/
}
}
}
setUserPingTimeout(idUser: string): void {
if (this.connectedPing[idUser]) {
clearTimeout(this.connectedPing[idUser]);
}
this.connectedPing[idUser] = setTimeout(() => {
this.ping(idUser);
this.setUserPingTimeout(idUser);
}, 600000);
}
cancelListenUser(idUser: string) {
if (!idUser) {
return;
}
if (this.listenerCount[idUser]) {
this.listenerCount[idUser] += -1;
}
if (!this.listenerCount[idUser] || this.listenerCount[idUser] === 0) {
ws.unsubscribe(`users/${idUser}`, null, null);
if (this.connectedPing[idUser]) {
clearInterval(this.connectedPing[idUser]);
}
}
}
}
const service = new ListenUsers();
(Globals.services as any) = service;
export default service;
@@ -2,18 +2,14 @@
/* eslint-disable @typescript-eslint/no-this-alias */
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/no-explicit-any */
import Languages from '@features/global/services/languages-service';
import Observable from '@deprecated/CollectionsV1/observable.js';
import User from '@features/users/services/current-user-service';
import Api from '@features/global/framework/api-service';
import ws from '@deprecated/websocket/websocket.js';
import Collections from '@deprecated/CollectionsV1/Collections/Collections.js';
import workspaceService from '@deprecated/workspaces/workspaces.jsx';
import Numbers from '@features/global/utils/Numbers';
import WorkspaceUserRights from '@features/workspaces/services/workspace-user-rights-service';
import CurrentUser from '@deprecated/user/CurrentUser';
import Api from '@features/global/framework/api-service';
import AlertManager from '@features/global/services/alert-manager-service';
import Globals from '@features/global/services/globals-tdrive-app-service';
import Languages from '@features/global/services/languages-service';
import User from '@features/users/services/current-user-service';
import WorkspaceUserRights from '@features/workspaces/services/workspace-user-rights-service';
const prefixRoute = '/internal/services/workspaces/v1';
@@ -68,11 +64,6 @@ class WorkspacesUsers extends Observable {
return (this.users_by_workspace || {})[workspace_id] || {};
}
unload(workspace_id: string) {
ws.unsubscribe('workspace_users/' + workspace_id, null, null);
}
load(workspace_id: string, reset_offset: string, options: any) {}
canShowUserInWorkspaceList(member: any) {
// if user is interne or wexterne => no restriction
if (!WorkspaceUserRights.isInvite()) {
@@ -1,53 +0,0 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import ws from '@deprecated/websocket/websocket.js';
import Workspaces from '@deprecated/workspaces/workspaces.jsx';
import User from '@features/users/services/current-user-service';
import LoginService from '@features/auth/login-service';
import { RightsOrNone } from '../../workspace-members/services/workspace-members-access-rights-service';
import Logger from '@features/global/framework/logger-service';
type WebsocketWorkspace = {
type: 'remove' | 'add' | 'update_group_privileges' | 'update_workspace_level';
group_id: string;
workspace_id: string;
level: RightsOrNone;
workspace: any; // TODO
privileges: any; // TODO
};
class WorkspacesListener {
private logger: Logger.Logger;
constructor() {
this.logger = Logger.getLogger('WorkspacesListener');
}
startListen() {
this.logger.debug('Start listener');
ws.subscribe(
`workspaces_of_user/${User.getCurrentUserId()}`,
(_uri: any, data: WebsocketWorkspace) => {
this.logger.debug('Got a message', data);
LoginService.updateUser();
if (data.workspace) {
if (data.type === 'remove') {
Workspaces.removeFromUser(data.workspace);
Workspaces.notify();
} else if (data.type === 'add') {
Workspaces.notify();
}
}
},
null,
);
}
cancelListen() {
this.logger.debug('Cancel listener');
ws.unsubscribe(`workspaces_of_user/${User.getCurrentUserId()}`, null, null);
}
}
export default new WorkspacesListener();