diff --git a/server/controllers/LibraryController.js b/server/controllers/LibraryController.js index b2461a3a..a1c14282 100644 --- a/server/controllers/LibraryController.js +++ b/server/controllers/LibraryController.js @@ -485,7 +485,7 @@ class LibraryController { res.sendStatus(200) } - // GET: api/scan (Root) + // GET: api/libraries/:id/scan async scan(req, res) { if (!req.user.isAdminOrUp) { Logger.error(`[LibraryController] Non-root user attempted to scan library`, req.user) @@ -499,6 +499,40 @@ class LibraryController { Logger.info('[LibraryController] Scan complete') } + // GET: api/libraries/:id/recent-episode + async getRecentEpisodes(req, res) { + if (!req.library.isPodcast) { + return res.sendStatus(404) + } + + const payload = { + episodes: [], + total: 0, + limit: req.query.limit && !isNaN(req.query.limit) ? Number(req.query.limit) : 0, + page: req.query.page && !isNaN(req.query.page) ? Number(req.query.page) : 0, + } + + var allUnfinishedEpisodes = [] + for (const libraryItem of req.libraryItems) { + const unfinishedEpisodes = libraryItem.media.episodes.filter(ep => { + const userProgress = req.user.getMediaProgress(libraryItem.id, ep.id) + return !userProgress || !userProgress.isFinished + }) + allUnfinishedEpisodes.push(...unfinishedEpisodes) + } + + payload.total = allUnfinishedEpisodes.length + + allUnfinishedEpisodes = sort(allUnfinishedEpisodes).desc(ep => ep.publishedAt) + + if (payload.limit) { + var startIndex = payload.page * payload.limit + allUnfinishedEpisodes = allUnfinishedEpisodes.slice(startIndex, startIndex + payload.limit) + } + payload.episodes = allUnfinishedEpisodes + res.json(payload) + } + middleware(req, res, next) { if (!req.user.checkCanAccessLibrary(req.params.id)) { Logger.warn(`[LibraryController] Library ${req.params.id} not accessible to user ${req.user.username}`) diff --git a/server/routers/ApiRouter.js b/server/routers/ApiRouter.js index 2ad782f0..edb293df 100644 --- a/server/routers/ApiRouter.js +++ b/server/routers/ApiRouter.js @@ -70,7 +70,8 @@ class ApiRouter { this.router.get('/libraries/:id/stats', LibraryController.middleware.bind(this), LibraryController.stats.bind(this)) this.router.get('/libraries/:id/authors', LibraryController.middleware.bind(this), LibraryController.getAuthors.bind(this)) this.router.get('/libraries/:id/matchall', LibraryController.middleware.bind(this), LibraryController.matchAll.bind(this)) - this.router.get('/libraries/:id/scan', LibraryController.middleware.bind(this), LibraryController.scan.bind(this)) // Root only + this.router.get('/libraries/:id/scan', LibraryController.middleware.bind(this), LibraryController.scan.bind(this)) + this.router.get('/libraries/:id/recent-episodes', LibraryController.middleware.bind(this), LibraryController.getRecentEpisodes.bind(this)) this.router.post('/libraries/order', LibraryController.reorder.bind(this))