From 953283247b4197d6b49ecb41a14fc8f1f6a32033 Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Sun, 6 Feb 2022 22:33:38 +0300 Subject: [PATCH] refactor: user online --- CHANGELOG.md | 1 + app/Controllers/Ws/OnlineController.ts | 11 +++++------ app/Services/StatService.ts | 4 ++-- app/Services/WsService.ts | 2 +- app/Store/SessionStore.ts | 8 ++++---- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29ade0f..76a1971 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ## Added - About description for user and `last_login` column date +- Updating info api route of authorized user ### Fix diff --git a/app/Controllers/Ws/OnlineController.ts b/app/Controllers/Ws/OnlineController.ts index 9f59b86..1d2c1fc 100644 --- a/app/Controllers/Ws/OnlineController.ts +++ b/app/Controllers/Ws/OnlineController.ts @@ -23,9 +23,8 @@ export default class OnlineController { this.socket.on('disconnect', reason => this.onDisconnected(reason)); } - public sendOnlineCount(online: number) { - this.socket.emit('user:online', online); - this.socket.broadcast.emit('user:online', online); + public sendOnline(online: string[]) { + this.io.sockets.emit('user:online', online); } public setUserOnline() { @@ -37,8 +36,8 @@ export default class OnlineController { */ this.sessionStore .saveSession(sessionId, { userId, userName, connected: true }) - .then(() => this.sessionStore.getOnlineSessionsCount()) - .then(online => this.sendOnlineCount(online)); + .then(() => this.sessionStore.getOnlineSessions()) + .then(online => this.sendOnline(online)); } } @@ -54,7 +53,7 @@ export default class OnlineController { */ this.sessionStore .saveSession(sessionId, { userId, userName, connected: false }) - .then(() => this.sessionStore.getOnlineSessionsCount()) + .then(() => this.sessionStore.getOnlineSessions()) .then(online => this.socket.broadcast.emit('user:online', online)); } } diff --git a/app/Services/StatService.ts b/app/Services/StatService.ts index d50ff32..8815509 100644 --- a/app/Services/StatService.ts +++ b/app/Services/StatService.ts @@ -20,7 +20,7 @@ export default class StatService { ` ); - const online = await Ws.sessionStore.getOnlineSessionsCount(); + const online = await Ws.sessionStore.getOnlineSessions(); return { success: true, @@ -28,7 +28,7 @@ export default class StatService { message: 'Fetched stat.', data: { ...count.rows[0], - online, + online: online.length, }, }; } diff --git a/app/Services/WsService.ts b/app/Services/WsService.ts index 7d29e51..84c05dc 100644 --- a/app/Services/WsService.ts +++ b/app/Services/WsService.ts @@ -19,7 +19,7 @@ export interface ServerToClientEvents { */ 'user:session': (data: { sessionId: string | undefined; userId: string | undefined }) => void; 'user:connected': (data: { userId: string; userName: string }) => void; - 'user:online': (data: number) => void; + 'user:online': (data: string[]) => void; } export interface ClientToServerEvents { diff --git a/app/Store/SessionStore.ts b/app/Store/SessionStore.ts index afa715c..9f6a935 100644 --- a/app/Store/SessionStore.ts +++ b/app/Store/SessionStore.ts @@ -57,7 +57,7 @@ class RedisSessionStore { const keys = await this.scanSessionKeys(); const commands = Array.from(keys).map(key => { // eslint-disable-next-line @typescript-eslint/no-unused-vars - const [prefix, ...id] = key.split('-'); + const [, ...id] = key.split('-'); return ['hmget', id.join('-'), 'userId', 'userName', 'connected']; }); @@ -70,11 +70,11 @@ class RedisSessionStore { /** * Returns the unique sessions with connected flag `true` */ - public async getOnlineSessionsCount() { + public async getOnlineSessions() { const sessions = await this.getSessions(); - const online = new Set(sessions.filter(s => s?.connected === true).map(s => s?.userId)); + const online = new Set(sessions.filter((s): s is SessionDataType => s?.connected === true).map(s => s?.userId)); - return online.size; + return Array.from(online); } /**