Update get User API endpoint to load media progress from db

This commit is contained in:
advplyr 2023-08-17 17:26:12 -05:00
parent 1ebe8a6f4c
commit 361732a463
4 changed files with 59 additions and 45 deletions

View file

@ -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) {

View file

@ -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