Skip to content

Commit

Permalink
Merge pull request #3798 from advplyr/migrate-new-library-items
Browse files Browse the repository at this point in the history
Migrate controllers to use new toOldJSON functions
  • Loading branch information
advplyr authored Jan 4, 2025
2 parents 7fae25a + 1e9470b commit 0ed4ea9
Show file tree
Hide file tree
Showing 23 changed files with 299 additions and 298 deletions.
46 changes: 23 additions & 23 deletions server/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,17 +401,6 @@ class Database {
return this.models.setting.updateSettingObj(settings.toJSON())
}

updateBulkBooks(oldBooks) {
if (!this.sequelize) return false
return Promise.all(oldBooks.map((oldBook) => this.models.book.saveFromOld(oldBook)))
}

async createLibraryItem(oldLibraryItem) {
if (!this.sequelize) return false
await oldLibraryItem.saveMetadata()
await this.models.libraryItem.fullCreateFromOld(oldLibraryItem)
}

/**
* Save metadata file and update library item
*
Expand All @@ -429,17 +418,6 @@ class Database {
return updated
}

async createBulkBookAuthors(bookAuthors) {
if (!this.sequelize) return false
await this.models.bookAuthor.bulkCreate(bookAuthors)
}

async removeBulkBookAuthors(authorId = null, bookId = null) {
if (!this.sequelize) return false
if (!authorId && !bookId) return
await this.models.bookAuthor.removeByIds(authorId, bookId)
}

getPlaybackSessions(where = null) {
if (!this.sequelize) return false
return this.models.playbackSession.getOldPlaybackSessions(where)
Expand Down Expand Up @@ -665,7 +643,7 @@ class Database {
/**
* Clean invalid records in database
* Series should have atleast one Book
* Book and Podcast must have an associated LibraryItem
* Book and Podcast must have an associated LibraryItem (and vice versa)
* Remove playback sessions that are 3 seconds or less
*/
async cleanDatabase() {
Expand Down Expand Up @@ -695,6 +673,28 @@ class Database {
await book.destroy()
}

// Remove invalid LibraryItem records
const libraryItemsWithNoMedia = await this.libraryItemModel.findAll({
include: [
{
model: this.bookModel,
attributes: ['id']
},
{
model: this.podcastModel,
attributes: ['id']
}
],
where: {
'$book.id$': null,
'$podcast.id$': null
}
})
for (const libraryItem of libraryItemsWithNoMedia) {
Logger.warn(`Found libraryItem "${libraryItem.id}" with no media - removing it`)
await libraryItem.destroy()
}

const playlistMediaItemsWithNoMediaItem = await this.playlistMediaItemModel.findAll({
include: [
{
Expand Down
44 changes: 24 additions & 20 deletions server/controllers/AuthorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,21 @@ class AuthorController {

// Used on author landing page to include library items and items grouped in series
if (include.includes('items')) {
authorJson.libraryItems = await Database.libraryItemModel.getForAuthor(req.author, req.user)
const libraryItems = await Database.libraryItemModel.getForAuthor(req.author, req.user)

if (include.includes('series')) {
const seriesMap = {}
// Group items into series
authorJson.libraryItems.forEach((li) => {
if (li.media.metadata.series) {
li.media.metadata.series.forEach((series) => {
const itemWithSeries = li.toJSONMinified()
itemWithSeries.media.metadata.series = series
libraryItems.forEach((li) => {
if (li.media.series?.length) {
li.media.series.forEach((series) => {
const itemWithSeries = li.toOldJSONMinified()
itemWithSeries.media.metadata.series = {
id: series.id,
name: series.name,
nameIgnorePrefix: series.nameIgnorePrefix,
sequence: series.bookSeries.sequence
}

if (seriesMap[series.id]) {
seriesMap[series.id].items.push(itemWithSeries)
Expand All @@ -76,7 +81,7 @@ class AuthorController {
}

// Minify library items
authorJson.libraryItems = authorJson.libraryItems.map((li) => li.toJSONMinified())
authorJson.libraryItems = libraryItems.map((li) => li.toOldJSONMinified())
}

return res.json(authorJson)
Expand Down Expand Up @@ -125,7 +130,7 @@ class AuthorController {
const bookAuthorsToCreate = []
const allItemsWithAuthor = await Database.authorModel.getAllLibraryItemsForAuthor(req.author.id)

const oldLibraryItems = []
const libraryItems = []
allItemsWithAuthor.forEach((libraryItem) => {
// Replace old author with merging author for each book
libraryItem.media.authors = libraryItem.media.authors.filter((au) => au.id !== req.author.id)
Expand All @@ -134,23 +139,22 @@ class AuthorController {
name: existingAuthor.name
})

const oldLibraryItem = Database.libraryItemModel.getOldLibraryItem(libraryItem)
oldLibraryItems.push(oldLibraryItem)
libraryItems.push(libraryItem)

bookAuthorsToCreate.push({
bookId: libraryItem.media.id,
authorId: existingAuthor.id
})
})
if (oldLibraryItems.length) {
await Database.removeBulkBookAuthors(req.author.id) // Remove all old BookAuthor
await Database.createBulkBookAuthors(bookAuthorsToCreate) // Create all new BookAuthor
for (const libraryItem of allItemsWithAuthor) {
if (libraryItems.length) {
await Database.bookAuthorModel.removeByIds(req.author.id) // Remove all old BookAuthor
await Database.bookAuthorModel.bulkCreate(bookAuthorsToCreate) // Create all new BookAuthor
for (const libraryItem of libraryItems) {
await libraryItem.saveMetadataFile()
}
SocketAuthority.emitter(
'items_updated',
oldLibraryItems.map((li) => li.toJSONExpanded())
libraryItems.map((li) => li.toOldJSONExpanded())
)
}

Expand Down Expand Up @@ -190,7 +194,7 @@ class AuthorController {
const allItemsWithAuthor = await Database.authorModel.getAllLibraryItemsForAuthor(req.author.id)

numBooksForAuthor = allItemsWithAuthor.length
const oldLibraryItems = []
const libraryItems = []
// Update author name on all books
for (const libraryItem of allItemsWithAuthor) {
libraryItem.media.authors = libraryItem.media.authors.map((au) => {
Expand All @@ -199,16 +203,16 @@ class AuthorController {
}
return au
})
const oldLibraryItem = Database.libraryItemModel.getOldLibraryItem(libraryItem)
oldLibraryItems.push(oldLibraryItem)

libraryItems.push(libraryItem)

await libraryItem.saveMetadataFile()
}

if (oldLibraryItems.length) {
if (libraryItems.length) {
SocketAuthority.emitter(
'items_updated',
oldLibraryItems.map((li) => li.toJSONExpanded())
libraryItems.map((li) => li.toOldJSONExpanded())
)
}
} else {
Expand Down
16 changes: 10 additions & 6 deletions server/controllers/CollectionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ class CollectionController {
* @param {Response} res
*/
async addBook(req, res) {
const libraryItem = await Database.libraryItemModel.getOldById(req.body.id)
const libraryItem = await Database.libraryItemModel.findByPk(req.body.id, {
attributes: ['libraryId', 'mediaId']
})
if (!libraryItem) {
return res.status(404).send('Book not found')
}
Expand All @@ -231,14 +233,14 @@ class CollectionController {

// Check if book is already in collection
const collectionBooks = await req.collection.getCollectionBooks()
if (collectionBooks.some((cb) => cb.bookId === libraryItem.media.id)) {
if (collectionBooks.some((cb) => cb.bookId === libraryItem.mediaId)) {
return res.status(400).send('Book already in collection')
}

// Create collectionBook record
await Database.collectionBookModel.create({
collectionId: req.collection.id,
bookId: libraryItem.media.id,
bookId: libraryItem.mediaId,
order: collectionBooks.length + 1
})
const jsonExpanded = await req.collection.getOldJsonExpanded()
Expand All @@ -255,7 +257,9 @@ class CollectionController {
* @param {Response} res
*/
async removeBook(req, res) {
const libraryItem = await Database.libraryItemModel.getOldById(req.params.bookId)
const libraryItem = await Database.libraryItemModel.findByPk(req.params.bookId, {
attributes: ['mediaId']
})
if (!libraryItem) {
return res.sendStatus(404)
}
Expand All @@ -266,15 +270,15 @@ class CollectionController {
})

let jsonExpanded = null
const collectionBookToRemove = collectionBooks.find((cb) => cb.bookId === libraryItem.media.id)
const collectionBookToRemove = collectionBooks.find((cb) => cb.bookId === libraryItem.mediaId)
if (collectionBookToRemove) {
// Remove collection book record
await collectionBookToRemove.destroy()

// Update order on collection books
let order = 1
for (const collectionBook of collectionBooks) {
if (collectionBook.bookId === libraryItem.media.id) continue
if (collectionBook.bookId === libraryItem.mediaId) continue
if (collectionBook.order !== order) {
await collectionBook.update({
order
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/EmailController.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class EmailController {
return res.sendStatus(403)
}

const libraryItem = await Database.libraryItemModel.getOldById(req.body.libraryItemId)
const libraryItem = await Database.libraryItemModel.getExpandedById(req.body.libraryItemId)
if (!libraryItem) {
return res.status(404).send('Library item not found')
}
Expand Down
12 changes: 6 additions & 6 deletions server/controllers/LibraryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1145,14 +1145,14 @@ class LibraryController {
await libraryItem.media.update({
narrators: libraryItem.media.narrators
})
const oldLibraryItem = Database.libraryItemModel.getOldLibraryItem(libraryItem)
itemsUpdated.push(oldLibraryItem)

itemsUpdated.push(libraryItem)
}

if (itemsUpdated.length) {
SocketAuthority.emitter(
'items_updated',
itemsUpdated.map((li) => li.toJSONExpanded())
itemsUpdated.map((li) => li.toOldJSONExpanded())
)
}

Expand Down Expand Up @@ -1189,14 +1189,14 @@ class LibraryController {
await libraryItem.media.update({
narrators: libraryItem.media.narrators
})
const oldLibraryItem = Database.libraryItemModel.getOldLibraryItem(libraryItem)
itemsUpdated.push(oldLibraryItem)

itemsUpdated.push(libraryItem)
}

if (itemsUpdated.length) {
SocketAuthority.emitter(
'items_updated',
itemsUpdated.map((li) => li.toJSONExpanded())
itemsUpdated.map((li) => li.toOldJSONExpanded())
)
}

Expand Down
41 changes: 22 additions & 19 deletions server/controllers/LibraryItemController.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,11 @@ class LibraryItemController {
const hardDelete = req.query.hard == 1 // Delete files from filesystem

const { libraryItemIds } = req.body
if (!libraryItemIds?.length) {
if (!libraryItemIds?.length || !Array.isArray(libraryItemIds)) {
return res.status(400).send('Invalid request body')
}

const itemsToDelete = await Database.libraryItemModel.getAllOldLibraryItems({
const itemsToDelete = await Database.libraryItemModel.findAllExpandedWhere({
id: libraryItemIds
})

Expand All @@ -566,19 +566,19 @@ class LibraryItemController {
const libraryId = itemsToDelete[0].libraryId
for (const libraryItem of itemsToDelete) {
const libraryItemPath = libraryItem.path
Logger.info(`[LibraryItemController] (${hardDelete ? 'Hard' : 'Soft'}) deleting Library Item "${libraryItem.media.metadata.title}" with id "${libraryItem.id}"`)
Logger.info(`[LibraryItemController] (${hardDelete ? 'Hard' : 'Soft'}) deleting Library Item "${libraryItem.media.title}" with id "${libraryItem.id}"`)
const mediaItemIds = []
const seriesIds = []
const authorIds = []
if (libraryItem.isPodcast) {
mediaItemIds.push(...libraryItem.media.episodes.map((ep) => ep.id))
mediaItemIds.push(...libraryItem.media.podcastEpisodes.map((ep) => ep.id))
} else {
mediaItemIds.push(libraryItem.media.id)
if (libraryItem.media.metadata.series?.length) {
seriesIds.push(...libraryItem.media.metadata.series.map((se) => se.id))
if (libraryItem.media.series?.length) {
seriesIds.push(...libraryItem.media.series.map((se) => se.id))
}
if (libraryItem.media.metadata.authors?.length) {
authorIds.push(...libraryItem.media.metadata.authors.map((au) => au.id))
if (libraryItem.media.authors?.length) {
authorIds.push(...libraryItem.media.authors.map((au) => au.id))
}
}
await this.handleDeleteLibraryItem(libraryItem.id, mediaItemIds)
Expand Down Expand Up @@ -623,7 +623,7 @@ class LibraryItemController {
}

// Get all library items to update
const libraryItems = await Database.libraryItemModel.getAllOldLibraryItems({
const libraryItems = await Database.libraryItemModel.findAllExpandedWhere({
id: libraryItemIds
})
if (updatePayloads.length !== libraryItems.length) {
Expand All @@ -645,21 +645,23 @@ class LibraryItemController {
if (libraryItem.isBook) {
if (Array.isArray(mediaPayload.metadata?.series)) {
const seriesIdsInUpdate = mediaPayload.metadata.series.map((se) => se.id)
const seriesRemoved = libraryItem.media.metadata.series.filter((se) => !seriesIdsInUpdate.includes(se.id))
const seriesRemoved = libraryItem.media.series.filter((se) => !seriesIdsInUpdate.includes(se.id))
seriesIdsRemoved.push(...seriesRemoved.map((se) => se.id))
}
if (Array.isArray(mediaPayload.metadata?.authors)) {
const authorIdsInUpdate = mediaPayload.metadata.authors.map((au) => au.id)
const authorsRemoved = libraryItem.media.metadata.authors.filter((au) => !authorIdsInUpdate.includes(au.id))
const authorsRemoved = libraryItem.media.authors.filter((au) => !authorIdsInUpdate.includes(au.id))
authorIdsRemoved.push(...authorsRemoved.map((au) => au.id))
}
}

if (libraryItem.media.update(mediaPayload)) {
Logger.debug(`[LibraryItemController] Updated library item media ${libraryItem.media.metadata.title}`)
const hasUpdates = await libraryItem.media.updateFromRequest(mediaPayload)
if (hasUpdates) {
libraryItem.changed('updatedAt', true)
await libraryItem.save()

await Database.updateLibraryItem(libraryItem)
SocketAuthority.emitter('item_updated', libraryItem.toJSONExpanded())
Logger.debug(`[LibraryItemController] Updated library item media "${libraryItem.media.title}"`)
SocketAuthority.emitter('item_updated', libraryItem.toOldJSONExpanded())
itemsUpdated++
}
}
Expand Down Expand Up @@ -688,11 +690,11 @@ class LibraryItemController {
if (!libraryItemIds.length) {
return res.status(403).send('Invalid payload')
}
const libraryItems = await Database.libraryItemModel.getAllOldLibraryItems({
const libraryItems = await Database.libraryItemModel.findAllExpandedWhere({
id: libraryItemIds
})
res.json({
libraryItems: libraryItems.map((li) => li.toJSONExpanded())
libraryItems: libraryItems.map((li) => li.toOldJSONExpanded())
})
}

Expand All @@ -715,7 +717,7 @@ class LibraryItemController {
return res.sendStatus(400)
}

const libraryItems = await Database.libraryItemModel.getAllOldLibraryItems({
const libraryItems = await Database.libraryItemModel.findAllExpandedWhere({
id: req.body.libraryItemIds
})
if (!libraryItems?.length) {
Expand All @@ -737,7 +739,8 @@ class LibraryItemController {
}

for (const libraryItem of libraryItems) {
const matchResult = await Scanner.quickMatchLibraryItem(this, libraryItem, options)
const oldLibraryItem = Database.libraryItemModel.getOldLibraryItem(libraryItem)
const matchResult = await Scanner.quickMatchLibraryItem(this, oldLibraryItem, options)
if (matchResult.updated) {
itemsUpdated++
} else if (matchResult.warning) {
Expand Down
Loading

0 comments on commit 0ed4ea9

Please sign in to comment.