From f1130eb63a9cb1c671ca821eb2a8aa4ea93a336a Mon Sep 17 00:00:00 2001 From: advplyr Date: Sat, 12 Aug 2023 15:52:09 -0500 Subject: [PATCH] Update MeController api endpoints to load library items from DB --- server/Server.js | 2 +- server/controllers/MeController.js | 33 ++++++++++++---------- server/managers/CronManager.js | 44 +++++++++++++++++++----------- server/models/LibraryItem.js | 9 ++++++ 4 files changed, 56 insertions(+), 32 deletions(-) diff --git a/server/Server.js b/server/Server.js index bf055aee..87b8e167 100644 --- a/server/Server.js +++ b/server/Server.js @@ -118,7 +118,7 @@ class Server { await this.rssFeedManager.init() const libraries = await Database.models.library.getAllOldLibraries() - this.cronManager.init(libraries) + await this.cronManager.init(libraries) if (Database.serverSettings.scannerDisableWatcher) { Logger.info(`[Server] Watcher is disabled`) diff --git a/server/controllers/MeController.js b/server/controllers/MeController.js index 8cb6b8bd..ec2d146a 100644 --- a/server/controllers/MeController.js +++ b/server/controllers/MeController.js @@ -59,7 +59,7 @@ class MeController { // PATCH: api/me/progress/:id async createUpdateMediaProgress(req, res) { - const libraryItem = Database.libraryItems.find(ab => ab.id === req.params.id) + const libraryItem = await Database.models.libraryItem.getOldById(req.params.id) if (!libraryItem) { return res.status(404).send('Item not found') } @@ -75,7 +75,7 @@ class MeController { // PATCH: api/me/progress/:id/:episodeId async createUpdateEpisodeMediaProgress(req, res) { const episodeId = req.params.episodeId - const libraryItem = Database.libraryItems.find(ab => ab.id === req.params.id) + const libraryItem = await Database.models.libraryItem.getOldById(req.params.id) if (!libraryItem) { return res.status(404).send('Item not found') } @@ -101,7 +101,7 @@ class MeController { let shouldUpdate = false for (const itemProgress of itemProgressPayloads) { - const libraryItem = Database.libraryItems.find(li => li.id === itemProgress.libraryItemId) // Make sure this library item exists + const libraryItem = await Database.models.libraryItem.getOldById(itemProgress.libraryItemId) if (libraryItem) { if (req.user.createUpdateMediaProgress(libraryItem, itemProgress, itemProgress.episodeId)) { const mediaProgress = req.user.getMediaProgress(libraryItem.id, itemProgress.episodeId) @@ -122,10 +122,10 @@ class MeController { // POST: api/me/item/:id/bookmark async createBookmark(req, res) { - var libraryItem = Database.libraryItems.find(li => li.id === req.params.id) - if (!libraryItem) return res.sendStatus(404) + if (!await Database.models.libraryItem.checkExistsById(req.params.id)) return res.sendStatus(404) + const { time, title } = req.body - var bookmark = req.user.createBookmark(libraryItem.id, time, title) + const bookmark = req.user.createBookmark(req.params.id, time, title) await Database.updateUser(req.user) SocketAuthority.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser()) res.json(bookmark) @@ -133,15 +133,17 @@ class MeController { // PATCH: api/me/item/:id/bookmark async updateBookmark(req, res) { - var libraryItem = Database.libraryItems.find(li => li.id === req.params.id) - if (!libraryItem) return res.sendStatus(404) + if (!await Database.models.libraryItem.checkExistsById(req.params.id)) return res.sendStatus(404) + const { time, title } = req.body - if (!req.user.findBookmark(libraryItem.id, time)) { + if (!req.user.findBookmark(req.params.id, time)) { Logger.error(`[MeController] updateBookmark not found`) return res.sendStatus(404) } - var bookmark = req.user.updateBookmark(libraryItem.id, time, title) + + const bookmark = req.user.updateBookmark(req.params.id, time, title) if (!bookmark) return res.sendStatus(500) + await Database.updateUser(req.user) SocketAuthority.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser()) res.json(bookmark) @@ -149,16 +151,17 @@ class MeController { // DELETE: api/me/item/:id/bookmark/:time async removeBookmark(req, res) { - var libraryItem = Database.libraryItems.find(li => li.id === req.params.id) - if (!libraryItem) return res.sendStatus(404) - var time = Number(req.params.time) + if (!await Database.models.libraryItem.checkExistsById(req.params.id)) return res.sendStatus(404) + + const time = Number(req.params.time) if (isNaN(time)) return res.sendStatus(500) - if (!req.user.findBookmark(libraryItem.id, time)) { + if (!req.user.findBookmark(req.params.id, time)) { Logger.error(`[MeController] removeBookmark not found`) return res.sendStatus(404) } - req.user.removeBookmark(libraryItem.id, time) + + req.user.removeBookmark(req.params.id, time) await Database.updateUser(req.user) SocketAuthority.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser()) res.sendStatus(200) diff --git a/server/managers/CronManager.js b/server/managers/CronManager.js index b5d17cb1..4be27754 100644 --- a/server/managers/CronManager.js +++ b/server/managers/CronManager.js @@ -1,3 +1,4 @@ +const Sequelize = require('sequelize') const cron = require('../libs/nodeCron') const Logger = require('../Logger') const Database = require('../Database') @@ -17,9 +18,9 @@ class CronManager { * Initialize library scan crons & podcast download crons * @param {oldLibrary[]} libraries */ - init(libraries) { + async init(libraries) { this.initLibraryScanCrons(libraries) - this.initPodcastCrons() + await this.initPodcastCrons() } /** @@ -70,23 +71,34 @@ class CronManager { } } - initPodcastCrons() { + /** + * Init cron jobs for auto-download podcasts + */ + async initPodcastCrons() { const cronExpressionMap = {} - Database.libraryItems.forEach((li) => { - if (li.mediaType === 'podcast' && li.media.autoDownloadEpisodes) { - if (!li.media.autoDownloadSchedule) { - Logger.error(`[CronManager] Podcast auto download schedule is not set for ${li.media.metadata.title}`) - } else { - if (!cronExpressionMap[li.media.autoDownloadSchedule]) { - cronExpressionMap[li.media.autoDownloadSchedule] = { - expression: li.media.autoDownloadSchedule, - libraryItemIds: [] - } - } - cronExpressionMap[li.media.autoDownloadSchedule].libraryItemIds.push(li.id) + + const podcastsWithAutoDownload = await Database.models.podcast.findAll({ + where: { + autoDownloadEpisodes: true, + autoDownloadSchedule: { + [Sequelize.Op.not]: null } + }, + include: { + model: Database.models.libraryItem } }) + + for (const podcast of podcastsWithAutoDownload) { + if (!cronExpressionMap[podcast.autoDownloadSchedule]) { + cronExpressionMap[podcast.autoDownloadSchedule] = { + expression: podcast.autoDownloadSchedule, + libraryItemIds: [] + } + } + cronExpressionMap[podcast.autoDownloadSchedule].libraryItemIds.push(podcast.libraryItem.id) + } + if (!Object.keys(cronExpressionMap).length) return Logger.debug(`[CronManager] Found ${Object.keys(cronExpressionMap).length} podcast episode schedules to start`) @@ -127,7 +139,7 @@ class CronManager { // Get podcast library items to check const libraryItems = [] for (const libraryItemId of libraryItemIds) { - const libraryItem = Database.libraryItems.find(li => li.id === libraryItemId) + const libraryItem = await Database.models.libraryItem.getOldById(libraryItemId) if (!libraryItem) { Logger.error(`[CronManager] Library item ${libraryItemId} not found for episode check cron ${expression}`) podcastCron.libraryItemIds = podcastCron.libraryItemIds.filter(lid => lid !== libraryItemId) // Filter it out diff --git a/server/models/LibraryItem.js b/server/models/LibraryItem.js index 59d46431..f96d38cb 100644 --- a/server/models/LibraryItem.js +++ b/server/models/LibraryItem.js @@ -678,6 +678,15 @@ module.exports = (sequelize) => { return libraryItems.map(li => this.getOldLibraryItem(li)) } + /** + * Check if library item exists + * @param {string} libraryItemId + * @returns {Promise} + */ + static async checkExistsById(libraryItemId) { + return (await this.count({ where: { id: libraryItemId } })) > 0 + } + getMedia(options) { if (!this.mediaType) return Promise.resolve(null) const mixinMethodName = `get${sequelize.uppercaseFirst(this.mediaType)}`