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
{{ $strings.LabelItem }} | @@ -55,19 +55,14 @@ | |
---|---|---|
- No Cover
|
-
- {{ item.episode.title || 'Unknown' }} -{{ item.media.metadata.title }} - - -{{ item.media.metadata.title || 'Unknown' }} -by {{ item.media.metadata.authorName }} - +{{ 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 |