Update:API endpoint for get all users to use minimal payload

This commit is contained in:
advplyr 2023-03-29 14:56:50 -05:00
parent d2e0844493
commit 575a162f8b
3 changed files with 11 additions and 9 deletions

View File

@ -11,9 +11,9 @@ class UserController {
findAll(req, res) { findAll(req, res) {
if (!req.user.isAdminOrUp) return res.sendStatus(403) if (!req.user.isAdminOrUp) return res.sendStatus(403)
const hideRootToken = !req.user.isRoot const hideRootToken = !req.user.isRoot
const users = this.db.users.map(u => this.userJsonWithItemProgressDetails(u, hideRootToken))
res.json({ res.json({
users: users // Minimal toJSONForBrowser does not include mediaProgress and bookmarks
users: this.db.users.map(u => u.toJSONForBrowser(hideRootToken, true))
}) })
} }

View File

@ -101,12 +101,12 @@ class User {
} }
} }
toJSONForBrowser() { toJSONForBrowser(hideRootToken = false, minimal = false) {
return { const json = {
id: this.id, id: this.id,
username: this.username, username: this.username,
type: this.type, type: this.type,
token: this.token, token: (this.type === 'root' && hideRootToken) ? '' : this.token,
mediaProgress: this.mediaProgress ? this.mediaProgress.map(li => li.toJSON()) : [], mediaProgress: this.mediaProgress ? this.mediaProgress.map(li => li.toJSON()) : [],
seriesHideFromContinueListening: [...this.seriesHideFromContinueListening], seriesHideFromContinueListening: [...this.seriesHideFromContinueListening],
bookmarks: this.bookmarks ? this.bookmarks.map(b => b.toJSON()) : [], bookmarks: this.bookmarks ? this.bookmarks.map(b => b.toJSON()) : [],
@ -119,6 +119,11 @@ class User {
librariesAccessible: [...this.librariesAccessible], librariesAccessible: [...this.librariesAccessible],
itemTagsAccessible: [...this.itemTagsAccessible] itemTagsAccessible: [...this.itemTagsAccessible]
} }
if (minimal) {
delete json.mediaProgress
delete json.bookmarks
}
return json
} }
// Data broadcasted // Data broadcasted

View File

@ -339,10 +339,7 @@ class ApiRouter {
// Helper Methods // Helper Methods
// //
userJsonWithItemProgressDetails(user, hideRootToken = false) { userJsonWithItemProgressDetails(user, hideRootToken = false) {
const json = user.toJSONForBrowser() const json = user.toJSONForBrowser(hideRootToken)
if (json.type === 'root' && hideRootToken) {
json.token = ''
}
json.mediaProgress = json.mediaProgress.map(lip => { json.mediaProgress = json.mediaProgress.map(lip => {
const libraryItem = this.db.libraryItems.find(li => li.id === lip.libraryItemId) const libraryItem = this.db.libraryItems.find(li => li.id === lip.libraryItemId)