Update podcast controller to load library items from db

This commit is contained in:
advplyr 2023-08-12 17:29:08 -05:00
parent f465193b9c
commit 6056c14926
6 changed files with 95 additions and 80 deletions

View File

@ -309,18 +309,22 @@ class LibraryItemController {
const hardDelete = req.query.hard == 1 // Delete files from filesystem const hardDelete = req.query.hard == 1 // Delete files from filesystem
const { libraryItemIds } = req.body const { libraryItemIds } = req.body
if (!libraryItemIds || !libraryItemIds.length) { if (!libraryItemIds?.length) {
return res.sendStatus(500) return res.status(400).send('Invalid request body')
} }
const itemsToDelete = Database.libraryItems.filter(li => libraryItemIds.includes(li.id)) const itemsToDelete = await Database.models.libraryItem.getAllOldLibraryItems({
id: libraryItemIds
})
if (!itemsToDelete.length) { if (!itemsToDelete.length) {
return res.sendStatus(404) return res.sendStatus(404)
} }
for (let i = 0; i < itemsToDelete.length; i++) {
const libraryItemPath = itemsToDelete[i].path for (const libraryItem of itemsToDelete) {
Logger.info(`[LibraryItemController] Deleting Library Item "${itemsToDelete[i].media.metadata.title}"`) const libraryItemPath = libraryItem.path
await this.handleDeleteLibraryItem(itemsToDelete[i]) Logger.info(`[LibraryItemController] Deleting Library Item "${libraryItem.media.metadata.title}"`)
await this.handleDeleteLibraryItem(libraryItem)
if (hardDelete) { if (hardDelete) {
Logger.info(`[LibraryItemController] Deleting library item from file system at "${libraryItemPath}"`) Logger.info(`[LibraryItemController] Deleting library item from file system at "${libraryItemPath}"`)
await fs.remove(libraryItemPath).catch((error) => { await fs.remove(libraryItemPath).catch((error) => {
@ -333,22 +337,21 @@ class LibraryItemController {
// POST: api/items/batch/update // POST: api/items/batch/update
async batchUpdate(req, res) { async batchUpdate(req, res) {
var updatePayloads = req.body const updatePayloads = req.body
if (!updatePayloads || !updatePayloads.length) { if (!updatePayloads?.length) {
return res.sendStatus(500) return res.sendStatus(500)
} }
var itemsUpdated = 0 let itemsUpdated = 0
for (let i = 0; i < updatePayloads.length; i++) { for (const updatePayload of updatePayloads) {
var mediaPayload = updatePayloads[i].mediaPayload const mediaPayload = updatePayload.mediaPayload
var libraryItem = Database.libraryItems.find(_li => _li.id === updatePayloads[i].id) const libraryItem = await Database.models.libraryItem.getOldById(updatePayload.id)
if (!libraryItem) return null if (!libraryItem) return null
await this.createAuthorsAndSeriesForItemUpdate(mediaPayload, libraryItem.libraryId) await this.createAuthorsAndSeriesForItemUpdate(mediaPayload, libraryItem.libraryId)
var hasUpdates = libraryItem.media.update(mediaPayload) if (libraryItem.media.update(mediaPayload)) {
if (hasUpdates) {
Logger.debug(`[LibraryItemController] Updated library item media ${libraryItem.media.metadata.title}`) Logger.debug(`[LibraryItemController] Updated library item media ${libraryItem.media.metadata.title}`)
await Database.updateLibraryItem(libraryItem) await Database.updateLibraryItem(libraryItem)
SocketAuthority.emitter('item_updated', libraryItem.toJSONExpanded()) SocketAuthority.emitter('item_updated', libraryItem.toJSONExpanded())
@ -368,13 +371,11 @@ class LibraryItemController {
if (!libraryItemIds.length) { if (!libraryItemIds.length) {
return res.status(403).send('Invalid payload') return res.status(403).send('Invalid payload')
} }
const libraryItems = [] const libraryItems = await Database.models.libraryItem.getAllOldLibraryItems({
libraryItemIds.forEach((lid) => { id: libraryItemIds
const li = Database.libraryItems.find(_li => _li.id === lid)
if (li) libraryItems.push(li.toJSONExpanded())
}) })
res.json({ res.json({
libraryItems libraryItems: libraryItems.map(li => li.toJSONExpanded())
}) })
} }

View File

@ -133,7 +133,7 @@ class PlaylistController {
const itemsToAdd = req.body.items const itemsToAdd = req.body.items
let hasUpdated = false let hasUpdated = false
let order = playlist.items.length let order = playlist.items.length + 1
const playlistMediaItems = [] const playlistMediaItems = []
for (const item of itemsToAdd) { for (const item of itemsToAdd) {
if (!item.libraryItemId) { if (!item.libraryItemId) {

View File

@ -34,9 +34,13 @@ class PodcastController {
const podcastPath = filePathToPOSIX(payload.path) const podcastPath = filePathToPOSIX(payload.path)
// Check if a library item with this podcast folder exists already // Check if a library item with this podcast folder exists already
const existingLibraryItem = Database.libraryItems.find(li => li.path === podcastPath && li.libraryId === library.id) const existingLibraryItem = (await Database.models.libraryItem.count({
where: {
path: podcastPath
}
})) > 0
if (existingLibraryItem) { if (existingLibraryItem) {
Logger.error(`[PodcastController] Podcast already exists with name "${existingLibraryItem.media.metadata.title}" at path "${podcastPath}"`) Logger.error(`[PodcastController] Podcast already exists at path "${podcastPath}"`)
return res.status(400).send('Podcast already exists') return res.status(400).send('Podcast already exists')
} }
@ -268,18 +272,27 @@ class PodcastController {
} }
// Update/remove playlists that had this podcast episode // Update/remove playlists that had this podcast episode
const playlistsWithEpisode = await Database.models.playlist.getPlaylistsForMediaItemIds([episodeId]) const playlistMediaItems = await Database.models.playlistMediaItem.findAll({
for (const playlist of playlistsWithEpisode) { where: {
playlist.removeItem(libraryItem.id, episodeId) mediaItemId: episodeId
},
include: {
model: Database.models.playlist,
include: Database.models.playlistMediaItem
}
})
for (const pmi of playlistMediaItems) {
const numItems = pmi.playlist.playlistMediaItems.length - 1
// If playlist is now empty then remove it if (!numItems) {
if (!playlist.items.length) {
Logger.info(`[PodcastController] Playlist "${playlist.name}" has no more items - removing it`) Logger.info(`[PodcastController] Playlist "${playlist.name}" has no more items - removing it`)
await Database.removePlaylist(playlist.id) const jsonExpanded = await pmi.playlist.getOldJsonExpanded()
SocketAuthority.clientEmitter(playlist.userId, 'playlist_removed', playlist.toJSONExpanded(Database.libraryItems)) SocketAuthority.clientEmitter(pmi.playlist.userId, 'playlist_removed', jsonExpanded)
await pmi.playlist.destroy()
} else { } else {
await Database.updatePlaylist(playlist) await pmi.destroy()
SocketAuthority.clientEmitter(playlist.userId, 'playlist_updated', playlist.toJSONExpanded(Database.libraryItems)) const jsonExpanded = await pmi.playlist.getOldJsonExpanded()
SocketAuthority.clientEmitter(pmi.playlist.userId, 'playlist_updated', jsonExpanded)
} }
} }
@ -298,9 +311,9 @@ class PodcastController {
res.json(libraryItem.toJSON()) res.json(libraryItem.toJSON())
} }
middleware(req, res, next) { async middleware(req, res, next) {
const item = Database.libraryItems.find(li => li.id === req.params.id) const item = await Database.models.libraryItem.getOldById(req.params.id)
if (!item || !item.media) return res.sendStatus(404) if (!item?.media) return res.sendStatus(404)
if (!item.isPodcast) { if (!item.isPodcast) {
return res.sendStatus(500) return res.sendStatus(500)

View File

@ -205,48 +205,6 @@ module.exports = (sequelize) => {
return this.create(collection) return this.create(collection)
} }
static async fullUpdateFromOld(oldCollection, collectionBooks) {
const existingCollection = await this.findByPk(oldCollection.id, {
include: sequelize.models.collectionBook
})
if (!existingCollection) return false
let hasUpdates = false
const collection = this.getFromOld(oldCollection)
for (const cb of collectionBooks) {
const existingCb = existingCollection.collectionBooks.find(i => i.bookId === cb.bookId)
if (!existingCb) {
await sequelize.models.collectionBook.create(cb)
hasUpdates = true
} else if (existingCb.order != cb.order) {
await existingCb.update({ order: cb.order })
hasUpdates = true
}
}
for (const cb of existingCollection.collectionBooks) {
// collectionBook was removed
if (!collectionBooks.some(i => i.bookId === cb.bookId)) {
await cb.destroy()
hasUpdates = true
}
}
let hasCollectionUpdates = false
for (const key in collection) {
let existingValue = existingCollection[key]
if (existingValue instanceof Date) existingValue = existingValue.valueOf()
if (!areEquivalent(collection[key], existingValue)) {
hasCollectionUpdates = true
}
}
if (hasCollectionUpdates) {
existingCollection.update(collection)
hasUpdates = true
}
return hasUpdates
}
static getFromOld(oldCollection) { static getFromOld(oldCollection) {
return { return {
id: oldCollection.id, id: oldCollection.id,

View File

@ -1,4 +1,4 @@
const { DataTypes, Model } = require('sequelize') const { DataTypes, Model, WhereOptions } = require('sequelize')
const Logger = require('../Logger') const Logger = require('../Logger')
const oldLibraryItem = require('../objects/LibraryItem') const oldLibraryItem = require('../objects/LibraryItem')
const libraryFilters = require('../utils/queries/libraryFilters') const libraryFilters = require('../utils/queries/libraryFilters')
@ -102,11 +102,12 @@ module.exports = (sequelize) => {
/** /**
* Currently unused because this is too slow and uses too much mem * Currently unused because this is too slow and uses too much mem
* * @param {[WhereOptions]} where
* @returns {Array<objects.LibraryItem>} old library items * @returns {Array<objects.LibraryItem>} old library items
*/ */
static async getAllOldLibraryItems() { static async getAllOldLibraryItems(where = null) {
let libraryItems = await this.findAll({ let libraryItems = await this.findAll({
where,
include: [ include: [
{ {
model: sequelize.models.book, model: sequelize.models.book,

View File

@ -54,6 +54,48 @@ module.exports = (sequelize) => {
}) })
} }
/**
* Get old playlist toJSONExpanded
* @param {[string[]]} include
* @returns {Promise<object>} oldPlaylist.toJSONExpanded
*/
async getOldJsonExpanded(include) {
this.playlistMediaItems = await this.getPlaylistMediaItems({
include: [
{
model: sequelize.models.book,
include: sequelize.models.libraryItem
},
{
model: sequelize.models.podcastEpisode,
include: {
model: sequelize.models.podcast,
include: sequelize.models.libraryItem
}
}
],
order: [['order', 'ASC']]
}) || []
const oldPlaylist = sequelize.models.playlist.getOldPlaylist(this)
const libraryItemIds = oldPlaylist.items.map(i => i.libraryItemId)
let libraryItems = await sequelize.models.libraryItem.getAllOldLibraryItems({
id: libraryItemIds
})
const playlistExpanded = oldCollection.toJSONExpanded(libraryItems)
if (include?.includes('rssfeed')) {
const feeds = await this.getFeeds()
if (feeds?.length) {
playlistExpanded.rssFeed = sequelize.models.feed.getOldFeed(feeds[0])
}
}
return playlistExpanded
}
static createFromOld(oldPlaylist) { static createFromOld(oldPlaylist) {
const playlist = this.getFromOld(oldPlaylist) const playlist = this.getFromOld(oldPlaylist)
return this.create(playlist) return this.create(playlist)