From 361732a46370255a1a23c35adde9c75c66149ad0 Mon Sep 17 00:00:00 2001 From: advplyr Date: Thu, 17 Aug 2023 17:26:12 -0500 Subject: [PATCH] Update get User API endpoint to load media progress from db --- client/components/covers/PreviewCover.vue | 7 +++- client/pages/config/users/_id/index.vue | 20 +++------ server/controllers/UserController.js | 49 ++++++++++++++++++++++- server/routers/ApiRouter.js | 28 ------------- 4 files changed, 59 insertions(+), 45 deletions(-) diff --git a/client/components/covers/PreviewCover.vue b/client/components/covers/PreviewCover.vue index f25d655d..daf579ac 100644 --- a/client/components/covers/PreviewCover.vue +++ b/client/components/covers/PreviewCover.vue @@ -13,8 +13,8 @@
- -

Invalid Cover

+ +

Invalid Cover

@@ -58,6 +58,9 @@ export default { sizeMultiplier() { return this.width / 120 }, + invalidCoverFontSize() { + return Math.max(this.sizeMultiplier * 0.8, 0.5) + }, placeholderCoverPadding() { return 0.8 * this.sizeMultiplier }, diff --git a/client/pages/config/users/_id/index.vue b/client/pages/config/users/_id/index.vue index 3477a5be..e45ce76f 100644 --- a/client/pages/config/users/_id/index.vue +++ b/client/pages/config/users/_id/index.vue @@ -47,7 +47,7 @@

{{ $strings.HeaderSavedMediaProgress }}

- +
@@ -55,19 +55,14 @@ - +
{{ $strings.LabelItem }}
- + +
No Cover
- - +

{{ item.displayTitle || 'Unknown' }}

+

{{ item.displaySubtitle }}

{{ Math.floor(item.progress * 100) }}%

@@ -124,9 +119,6 @@ export default { mediaProgress() { return this.user.mediaProgress.sort((a, b) => b.lastUpdate - a.lastUpdate) }, - mediaProgressWithMedia() { - return this.mediaProgress.filter((mp) => mp.media) - }, totalListeningTime() { return this.listeningStats.totalTime || 0 }, diff --git a/server/controllers/UserController.js b/server/controllers/UserController.js index 0928b46f..190792d0 100644 --- a/server/controllers/UserController.js +++ b/server/controllers/UserController.js @@ -32,13 +32,60 @@ class UserController { }) } + /** + * GET: /api/users/:id + * Get a single user toJSONForBrowser + * Media progress items include: `displayTitle`, `displaySubtitle` (for podcasts), `coverPath` and `mediaUpdatedAt` + * + * @param {import("express").Request} req + * @param {import("express").Response} res + */ async findOne(req, res) { if (!req.user.isAdminOrUp) { Logger.error('User other than admin attempting to get user', req.user) return res.sendStatus(403) } - res.json(this.userJsonWithItemProgressDetails(req.reqUser, !req.user.isRoot)) + // Get user media progress with associated mediaItem + const mediaProgresses = await Database.models.mediaProgress.findAll({ + where: { + userId: req.reqUser.id + }, + include: [ + { + model: Database.models.book, + attributes: ['id', 'title', 'coverPath', 'updatedAt'] + }, + { + model: Database.models.podcastEpisode, + attributes: ['id', 'title'], + include: { + model: Database.models.podcast, + attributes: ['id', 'title', 'coverPath', 'updatedAt'] + } + } + ] + }) + + const oldMediaProgresses = mediaProgresses.map(mp => { + const oldMediaProgress = mp.getOldMediaProgress() + oldMediaProgress.displayTitle = mp.mediaItem?.title + if (mp.mediaItem?.podcast) { + oldMediaProgress.displaySubtitle = mp.mediaItem.podcast?.title + oldMediaProgress.coverPath = mp.mediaItem.podcast?.coverPath + oldMediaProgress.mediaUpdatedAt = mp.mediaItem.podcast?.updatedAt + } else if (mp.mediaItem) { + oldMediaProgress.coverPath = mp.mediaItem.coverPath + oldMediaProgress.mediaUpdatedAt = mp.mediaItem.updatedAt + } + return oldMediaProgress + }) + + const userJson = req.reqUser.toJSONForBrowser(!req.user.isRoot) + + userJson.mediaProgress = oldMediaProgresses + + res.json(userJson) } async create(req, res) { diff --git a/server/routers/ApiRouter.js b/server/routers/ApiRouter.js index 5499713a..c4046688 100644 --- a/server/routers/ApiRouter.js +++ b/server/routers/ApiRouter.js @@ -352,34 +352,6 @@ class ApiRouter { // // Helper Methods // - userJsonWithItemProgressDetails(user, hideRootToken = false) { - const json = user.toJSONForBrowser(hideRootToken) - - json.mediaProgress = json.mediaProgress.map(lip => { - const libraryItem = Database.libraryItems.find(li => li.id === lip.libraryItemId) - if (!libraryItem) { - Logger.warn('[ApiRouter] Library item not found for users progress ' + lip.libraryItemId) - lip.media = null - } else { - if (lip.episodeId) { - const episode = libraryItem.mediaType === 'podcast' ? libraryItem.media.getEpisode(lip.episodeId) : null - if (!episode) { - Logger.warn(`[ApiRouter] Episode ${lip.episodeId} not found for user media progress, podcast: ${libraryItem.media.metadata.title}`) - lip.media = null - } else { - lip.media = libraryItem.media.toJSONExpanded() - lip.episode = episode.toJSON() - } - } else { - lip.media = libraryItem.media.toJSONExpanded() - } - } - return lip - }).filter(lip => !!lip) - - return json - } - /** * Remove library item and associated entities * @param {string} mediaType