Update MeController api endpoints to load library items from DB

This commit is contained in:
advplyr 2023-08-12 15:52:09 -05:00
parent db80cec168
commit f1130eb63a
4 changed files with 56 additions and 32 deletions

View File

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

View File

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

View File

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

View File

@ -678,6 +678,15 @@ module.exports = (sequelize) => {
return libraryItems.map(li => this.getOldLibraryItem(li))
}
/**
* Check if library item exists
* @param {string} libraryItemId
* @returns {Promise<boolean>}
*/
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)}`