mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-02-09 15:00:04 +01:00
Update MeController api endpoints to load library items from DB
This commit is contained in:
parent
db80cec168
commit
f1130eb63a
@ -118,7 +118,7 @@ class Server {
|
|||||||
await this.rssFeedManager.init()
|
await this.rssFeedManager.init()
|
||||||
|
|
||||||
const libraries = await Database.models.library.getAllOldLibraries()
|
const libraries = await Database.models.library.getAllOldLibraries()
|
||||||
this.cronManager.init(libraries)
|
await this.cronManager.init(libraries)
|
||||||
|
|
||||||
if (Database.serverSettings.scannerDisableWatcher) {
|
if (Database.serverSettings.scannerDisableWatcher) {
|
||||||
Logger.info(`[Server] Watcher is disabled`)
|
Logger.info(`[Server] Watcher is disabled`)
|
||||||
|
@ -59,7 +59,7 @@ class MeController {
|
|||||||
|
|
||||||
// PATCH: api/me/progress/:id
|
// PATCH: api/me/progress/:id
|
||||||
async createUpdateMediaProgress(req, res) {
|
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) {
|
if (!libraryItem) {
|
||||||
return res.status(404).send('Item not found')
|
return res.status(404).send('Item not found')
|
||||||
}
|
}
|
||||||
@ -75,7 +75,7 @@ class MeController {
|
|||||||
// PATCH: api/me/progress/:id/:episodeId
|
// PATCH: api/me/progress/:id/:episodeId
|
||||||
async createUpdateEpisodeMediaProgress(req, res) {
|
async createUpdateEpisodeMediaProgress(req, res) {
|
||||||
const episodeId = req.params.episodeId
|
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) {
|
if (!libraryItem) {
|
||||||
return res.status(404).send('Item not found')
|
return res.status(404).send('Item not found')
|
||||||
}
|
}
|
||||||
@ -101,7 +101,7 @@ class MeController {
|
|||||||
|
|
||||||
let shouldUpdate = false
|
let shouldUpdate = false
|
||||||
for (const itemProgress of itemProgressPayloads) {
|
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 (libraryItem) {
|
||||||
if (req.user.createUpdateMediaProgress(libraryItem, itemProgress, itemProgress.episodeId)) {
|
if (req.user.createUpdateMediaProgress(libraryItem, itemProgress, itemProgress.episodeId)) {
|
||||||
const mediaProgress = req.user.getMediaProgress(libraryItem.id, itemProgress.episodeId)
|
const mediaProgress = req.user.getMediaProgress(libraryItem.id, itemProgress.episodeId)
|
||||||
@ -122,10 +122,10 @@ class MeController {
|
|||||||
|
|
||||||
// POST: api/me/item/:id/bookmark
|
// POST: api/me/item/:id/bookmark
|
||||||
async createBookmark(req, res) {
|
async createBookmark(req, res) {
|
||||||
var libraryItem = Database.libraryItems.find(li => li.id === req.params.id)
|
if (!await Database.models.libraryItem.checkExistsById(req.params.id)) return res.sendStatus(404)
|
||||||
if (!libraryItem) return res.sendStatus(404)
|
|
||||||
const { time, title } = req.body
|
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)
|
await Database.updateUser(req.user)
|
||||||
SocketAuthority.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser())
|
SocketAuthority.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser())
|
||||||
res.json(bookmark)
|
res.json(bookmark)
|
||||||
@ -133,15 +133,17 @@ class MeController {
|
|||||||
|
|
||||||
// PATCH: api/me/item/:id/bookmark
|
// PATCH: api/me/item/:id/bookmark
|
||||||
async updateBookmark(req, res) {
|
async updateBookmark(req, res) {
|
||||||
var libraryItem = Database.libraryItems.find(li => li.id === req.params.id)
|
if (!await Database.models.libraryItem.checkExistsById(req.params.id)) return res.sendStatus(404)
|
||||||
if (!libraryItem) return res.sendStatus(404)
|
|
||||||
const { time, title } = req.body
|
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`)
|
Logger.error(`[MeController] updateBookmark not found`)
|
||||||
return res.sendStatus(404)
|
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)
|
if (!bookmark) return res.sendStatus(500)
|
||||||
|
|
||||||
await Database.updateUser(req.user)
|
await Database.updateUser(req.user)
|
||||||
SocketAuthority.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser())
|
SocketAuthority.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser())
|
||||||
res.json(bookmark)
|
res.json(bookmark)
|
||||||
@ -149,16 +151,17 @@ class MeController {
|
|||||||
|
|
||||||
// DELETE: api/me/item/:id/bookmark/:time
|
// DELETE: api/me/item/:id/bookmark/:time
|
||||||
async removeBookmark(req, res) {
|
async removeBookmark(req, res) {
|
||||||
var libraryItem = Database.libraryItems.find(li => li.id === req.params.id)
|
if (!await Database.models.libraryItem.checkExistsById(req.params.id)) return res.sendStatus(404)
|
||||||
if (!libraryItem) return res.sendStatus(404)
|
|
||||||
var time = Number(req.params.time)
|
const time = Number(req.params.time)
|
||||||
if (isNaN(time)) return res.sendStatus(500)
|
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`)
|
Logger.error(`[MeController] removeBookmark not found`)
|
||||||
return res.sendStatus(404)
|
return res.sendStatus(404)
|
||||||
}
|
}
|
||||||
req.user.removeBookmark(libraryItem.id, time)
|
|
||||||
|
req.user.removeBookmark(req.params.id, time)
|
||||||
await Database.updateUser(req.user)
|
await Database.updateUser(req.user)
|
||||||
SocketAuthority.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser())
|
SocketAuthority.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser())
|
||||||
res.sendStatus(200)
|
res.sendStatus(200)
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
const Sequelize = require('sequelize')
|
||||||
const cron = require('../libs/nodeCron')
|
const cron = require('../libs/nodeCron')
|
||||||
const Logger = require('../Logger')
|
const Logger = require('../Logger')
|
||||||
const Database = require('../Database')
|
const Database = require('../Database')
|
||||||
@ -17,9 +18,9 @@ class CronManager {
|
|||||||
* Initialize library scan crons & podcast download crons
|
* Initialize library scan crons & podcast download crons
|
||||||
* @param {oldLibrary[]} libraries
|
* @param {oldLibrary[]} libraries
|
||||||
*/
|
*/
|
||||||
init(libraries) {
|
async init(libraries) {
|
||||||
this.initLibraryScanCrons(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 = {}
|
const cronExpressionMap = {}
|
||||||
Database.libraryItems.forEach((li) => {
|
|
||||||
if (li.mediaType === 'podcast' && li.media.autoDownloadEpisodes) {
|
const podcastsWithAutoDownload = await Database.models.podcast.findAll({
|
||||||
if (!li.media.autoDownloadSchedule) {
|
where: {
|
||||||
Logger.error(`[CronManager] Podcast auto download schedule is not set for ${li.media.metadata.title}`)
|
autoDownloadEpisodes: true,
|
||||||
} else {
|
autoDownloadSchedule: {
|
||||||
if (!cronExpressionMap[li.media.autoDownloadSchedule]) {
|
[Sequelize.Op.not]: null
|
||||||
cronExpressionMap[li.media.autoDownloadSchedule] = {
|
|
||||||
expression: li.media.autoDownloadSchedule,
|
|
||||||
libraryItemIds: []
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cronExpressionMap[li.media.autoDownloadSchedule].libraryItemIds.push(li.id)
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
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
|
if (!Object.keys(cronExpressionMap).length) return
|
||||||
|
|
||||||
Logger.debug(`[CronManager] Found ${Object.keys(cronExpressionMap).length} podcast episode schedules to start`)
|
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
|
// Get podcast library items to check
|
||||||
const libraryItems = []
|
const libraryItems = []
|
||||||
for (const libraryItemId of libraryItemIds) {
|
for (const libraryItemId of libraryItemIds) {
|
||||||
const libraryItem = Database.libraryItems.find(li => li.id === libraryItemId)
|
const libraryItem = await Database.models.libraryItem.getOldById(libraryItemId)
|
||||||
if (!libraryItem) {
|
if (!libraryItem) {
|
||||||
Logger.error(`[CronManager] Library item ${libraryItemId} not found for episode check cron ${expression}`)
|
Logger.error(`[CronManager] Library item ${libraryItemId} not found for episode check cron ${expression}`)
|
||||||
podcastCron.libraryItemIds = podcastCron.libraryItemIds.filter(lid => lid !== libraryItemId) // Filter it out
|
podcastCron.libraryItemIds = podcastCron.libraryItemIds.filter(lid => lid !== libraryItemId) // Filter it out
|
||||||
|
@ -678,6 +678,15 @@ module.exports = (sequelize) => {
|
|||||||
return libraryItems.map(li => this.getOldLibraryItem(li))
|
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) {
|
getMedia(options) {
|
||||||
if (!this.mediaType) return Promise.resolve(null)
|
if (!this.mediaType) return Promise.resolve(null)
|
||||||
const mixinMethodName = `get${sequelize.uppercaseFirst(this.mediaType)}`
|
const mixinMethodName = `get${sequelize.uppercaseFirst(this.mediaType)}`
|
||||||
|
Loading…
Reference in New Issue
Block a user