From fdf67e17a0241301035ec7333f7f9f99e398fd2b Mon Sep 17 00:00:00 2001 From: advplyr Date: Thu, 10 Nov 2022 17:42:20 -0600 Subject: [PATCH] Add:API endpoint to get users online and open listening sessions #1125 --- server/Server.js | 7 +++---- server/controllers/UserController.js | 13 +++++++++++++ server/routers/ApiRouter.js | 4 +++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/server/Server.js b/server/Server.js index b51e0cd8..7983809f 100644 --- a/server/Server.js +++ b/server/Server.js @@ -83,7 +83,7 @@ class Server { this.cronManager = new CronManager(this.db, this.scanner, this.podcastManager) // Routers - this.apiRouter = new ApiRouter(this.db, this.auth, this.scanner, this.playbackSessionManager, this.abMergeManager, this.coverManager, this.backupManager, this.watcher, this.cacheManager, this.podcastManager, this.audioMetadataManager, this.rssFeedManager, this.cronManager, this.notificationManager, this.taskManager, this.emitter.bind(this), this.clientEmitter.bind(this)) + this.apiRouter = new ApiRouter(this.db, this.auth, this.scanner, this.playbackSessionManager, this.abMergeManager, this.coverManager, this.backupManager, this.watcher, this.cacheManager, this.podcastManager, this.audioMetadataManager, this.rssFeedManager, this.cronManager, this.notificationManager, this.taskManager, this.getUsersOnline.bind(this), this.emitter.bind(this), this.clientEmitter.bind(this)) this.hlsRouter = new HlsRouter(this.db, this.auth, this.playbackSessionManager, this.emitter.bind(this)) this.staticRouter = new StaticRouter(this.db) @@ -95,8 +95,7 @@ class Server { this.clients = {} } - get usersOnline() { - // TODO: Map open user sessions + getUsersOnline() { return Object.values(this.clients).filter(c => c.user).map(client => { return client.user.toJSONForPublic(this.playbackSessionManager.sessions, this.db.libraryItems) }) @@ -481,7 +480,7 @@ class Server { backups: (this.backupManager.backups || []).map(b => b.toJSON()) } if (user.type === 'root') { - initialPayload.usersOnline = this.usersOnline + initialPayload.usersOnline = this.getUsersOnline() } client.socket.emit('init', initialPayload) } diff --git a/server/controllers/UserController.js b/server/controllers/UserController.js index e7e96926..662afcbf 100644 --- a/server/controllers/UserController.js +++ b/server/controllers/UserController.js @@ -183,6 +183,19 @@ class UserController { res.json(this.userJsonWithItemProgressDetails(user, !req.user.isRoot)) } + // POST: api/users/online (admin) + async getOnlineUsers(req, res) { + if (!req.user.isAdminOrUp) { + return res.sendStatus(404) + } + const usersOnline = this.getUsersOnline() + + res.json({ + usersOnline, + openSessions: this.playbackSessionManager.sessions + }) + } + middleware(req, res, next) { if (!req.user.isAdminOrUp && req.user.id !== req.params.id) { return res.sendStatus(403) diff --git a/server/routers/ApiRouter.js b/server/routers/ApiRouter.js index 03fc9149..9f6f7abe 100644 --- a/server/routers/ApiRouter.js +++ b/server/routers/ApiRouter.js @@ -26,7 +26,7 @@ const Series = require('../objects/entities/Series') const FileSystemController = require('../controllers/FileSystemController') class ApiRouter { - constructor(db, auth, scanner, playbackSessionManager, abMergeManager, coverManager, backupManager, watcher, cacheManager, podcastManager, audioMetadataManager, rssFeedManager, cronManager, notificationManager, taskManager, emitter, clientEmitter) { + constructor(db, auth, scanner, playbackSessionManager, abMergeManager, coverManager, backupManager, watcher, cacheManager, podcastManager, audioMetadataManager, rssFeedManager, cronManager, notificationManager, taskManager, getUsersOnline, emitter, clientEmitter) { this.db = db this.auth = auth this.scanner = scanner @@ -42,6 +42,7 @@ class ApiRouter { this.cronManager = cronManager this.notificationManager = notificationManager this.taskManager = taskManager + this.getUsersOnline = getUsersOnline this.emitter = emitter this.clientEmitter = clientEmitter @@ -113,6 +114,7 @@ class ApiRouter { // this.router.post('/users', UserController.middleware.bind(this), UserController.create.bind(this)) this.router.get('/users', UserController.middleware.bind(this), UserController.findAll.bind(this)) + this.router.get('/users/online', UserController.getOnlineUsers.bind(this)) this.router.get('/users/:id', UserController.middleware.bind(this), UserController.findOne.bind(this)) this.router.patch('/users/:id', UserController.middleware.bind(this), UserController.update.bind(this)) this.router.delete('/users/:id', UserController.middleware.bind(this), UserController.delete.bind(this))