From 575a162f8b0d2e3f47fe3731fceb504e53b7fcfc Mon Sep 17 00:00:00 2001 From: advplyr Date: Wed, 29 Mar 2023 14:56:50 -0500 Subject: [PATCH] Update:API endpoint for get all users to use minimal payload --- server/controllers/UserController.js | 4 ++-- server/objects/user/User.js | 11 ++++++++--- server/routers/ApiRouter.js | 5 +---- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/server/controllers/UserController.js b/server/controllers/UserController.js index af0ce0e1..14635bb1 100644 --- a/server/controllers/UserController.js +++ b/server/controllers/UserController.js @@ -11,9 +11,9 @@ class UserController { findAll(req, res) { if (!req.user.isAdminOrUp) return res.sendStatus(403) const hideRootToken = !req.user.isRoot - const users = this.db.users.map(u => this.userJsonWithItemProgressDetails(u, hideRootToken)) res.json({ - users: users + // Minimal toJSONForBrowser does not include mediaProgress and bookmarks + users: this.db.users.map(u => u.toJSONForBrowser(hideRootToken, true)) }) } diff --git a/server/objects/user/User.js b/server/objects/user/User.js index 745cade2..461b1855 100644 --- a/server/objects/user/User.js +++ b/server/objects/user/User.js @@ -101,12 +101,12 @@ class User { } } - toJSONForBrowser() { - return { + toJSONForBrowser(hideRootToken = false, minimal = false) { + const json = { id: this.id, username: this.username, type: this.type, - token: this.token, + token: (this.type === 'root' && hideRootToken) ? '' : this.token, mediaProgress: this.mediaProgress ? this.mediaProgress.map(li => li.toJSON()) : [], seriesHideFromContinueListening: [...this.seriesHideFromContinueListening], bookmarks: this.bookmarks ? this.bookmarks.map(b => b.toJSON()) : [], @@ -119,6 +119,11 @@ class User { librariesAccessible: [...this.librariesAccessible], itemTagsAccessible: [...this.itemTagsAccessible] } + if (minimal) { + delete json.mediaProgress + delete json.bookmarks + } + return json } // Data broadcasted diff --git a/server/routers/ApiRouter.js b/server/routers/ApiRouter.js index 4c8569cf..79e531e2 100644 --- a/server/routers/ApiRouter.js +++ b/server/routers/ApiRouter.js @@ -339,10 +339,7 @@ class ApiRouter { // Helper Methods // userJsonWithItemProgressDetails(user, hideRootToken = false) { - const json = user.toJSONForBrowser() - if (json.type === 'root' && hideRootToken) { - json.token = '' - } + const json = user.toJSONForBrowser(hideRootToken) json.mediaProgress = json.mediaProgress.map(lip => { const libraryItem = this.db.libraryItems.find(li => li.id === lip.libraryItemId)