mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-01-08 00:08:14 +01:00
Update AuthorController library item usage and remove unused
This commit is contained in:
parent
726a9eaea5
commit
1e9470b840
@ -401,17 +401,6 @@ class Database {
|
|||||||
return this.models.setting.updateSettingObj(settings.toJSON())
|
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
|
* Save metadata file and update library item
|
||||||
*
|
*
|
||||||
@ -429,17 +418,6 @@ class Database {
|
|||||||
return updated
|
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) {
|
getPlaybackSessions(where = null) {
|
||||||
if (!this.sequelize) return false
|
if (!this.sequelize) return false
|
||||||
return this.models.playbackSession.getOldPlaybackSessions(where)
|
return this.models.playbackSession.getOldPlaybackSessions(where)
|
||||||
|
@ -44,16 +44,21 @@ class AuthorController {
|
|||||||
|
|
||||||
// Used on author landing page to include library items and items grouped in series
|
// Used on author landing page to include library items and items grouped in series
|
||||||
if (include.includes('items')) {
|
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')) {
|
if (include.includes('series')) {
|
||||||
const seriesMap = {}
|
const seriesMap = {}
|
||||||
// Group items into series
|
// Group items into series
|
||||||
authorJson.libraryItems.forEach((li) => {
|
libraryItems.forEach((li) => {
|
||||||
if (li.media.metadata.series) {
|
if (li.media.series?.length) {
|
||||||
li.media.metadata.series.forEach((series) => {
|
li.media.series.forEach((series) => {
|
||||||
const itemWithSeries = li.toJSONMinified()
|
const itemWithSeries = li.toOldJSONMinified()
|
||||||
itemWithSeries.media.metadata.series = series
|
itemWithSeries.media.metadata.series = {
|
||||||
|
id: series.id,
|
||||||
|
name: series.name,
|
||||||
|
nameIgnorePrefix: series.nameIgnorePrefix,
|
||||||
|
sequence: series.bookSeries.sequence
|
||||||
|
}
|
||||||
|
|
||||||
if (seriesMap[series.id]) {
|
if (seriesMap[series.id]) {
|
||||||
seriesMap[series.id].items.push(itemWithSeries)
|
seriesMap[series.id].items.push(itemWithSeries)
|
||||||
@ -76,7 +81,7 @@ class AuthorController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Minify library items
|
// Minify library items
|
||||||
authorJson.libraryItems = authorJson.libraryItems.map((li) => li.toJSONMinified())
|
authorJson.libraryItems = libraryItems.map((li) => li.toOldJSONMinified())
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.json(authorJson)
|
return res.json(authorJson)
|
||||||
@ -142,8 +147,8 @@ class AuthorController {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
if (libraryItems.length) {
|
if (libraryItems.length) {
|
||||||
await Database.removeBulkBookAuthors(req.author.id) // Remove all old BookAuthor
|
await Database.bookAuthorModel.removeByIds(req.author.id) // Remove all old BookAuthor
|
||||||
await Database.createBulkBookAuthors(bookAuthorsToCreate) // Create all new BookAuthor
|
await Database.bookAuthorModel.bulkCreate(bookAuthorsToCreate) // Create all new BookAuthor
|
||||||
for (const libraryItem of libraryItems) {
|
for (const libraryItem of libraryItems) {
|
||||||
await libraryItem.saveMetadataFile()
|
await libraryItem.saveMetadataFile()
|
||||||
}
|
}
|
||||||
|
@ -160,40 +160,6 @@ class LibraryItem extends Model {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
static async fullCreateFromOld(oldLibraryItem) {
|
|
||||||
const newLibraryItem = await this.create(this.getFromOld(oldLibraryItem))
|
|
||||||
|
|
||||||
if (oldLibraryItem.mediaType === 'book') {
|
|
||||||
const bookObj = this.sequelize.models.book.getFromOld(oldLibraryItem.media)
|
|
||||||
bookObj.libraryItemId = newLibraryItem.id
|
|
||||||
const newBook = await this.sequelize.models.book.create(bookObj)
|
|
||||||
|
|
||||||
const oldBookAuthors = oldLibraryItem.media.metadata.authors || []
|
|
||||||
const oldBookSeriesAll = oldLibraryItem.media.metadata.series || []
|
|
||||||
|
|
||||||
for (const oldBookAuthor of oldBookAuthors) {
|
|
||||||
await this.sequelize.models.bookAuthor.create({ authorId: oldBookAuthor.id, bookId: newBook.id })
|
|
||||||
}
|
|
||||||
for (const oldSeries of oldBookSeriesAll) {
|
|
||||||
await this.sequelize.models.bookSeries.create({ seriesId: oldSeries.id, bookId: newBook.id, sequence: oldSeries.sequence })
|
|
||||||
}
|
|
||||||
} else if (oldLibraryItem.mediaType === 'podcast') {
|
|
||||||
const podcastObj = this.sequelize.models.podcast.getFromOld(oldLibraryItem.media)
|
|
||||||
podcastObj.libraryItemId = newLibraryItem.id
|
|
||||||
const newPodcast = await this.sequelize.models.podcast.create(podcastObj)
|
|
||||||
|
|
||||||
const oldEpisodes = oldLibraryItem.media.episodes || []
|
|
||||||
for (const oldEpisode of oldEpisodes) {
|
|
||||||
const episodeObj = this.sequelize.models.podcastEpisode.getFromOld(oldEpisode)
|
|
||||||
episodeObj.libraryItemId = newLibraryItem.id
|
|
||||||
episodeObj.podcastId = newPodcast.id
|
|
||||||
await this.sequelize.models.podcastEpisode.create(episodeObj)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return newLibraryItem
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates libraryItem, book, authors and series from old library item
|
* Updates libraryItem, book, authors and series from old library item
|
||||||
*
|
*
|
||||||
@ -819,21 +785,11 @@ class LibraryItem extends Model {
|
|||||||
* Get book library items for author, optional use user permissions
|
* Get book library items for author, optional use user permissions
|
||||||
* @param {import('./Author')} author
|
* @param {import('./Author')} author
|
||||||
* @param {import('./User')} user
|
* @param {import('./User')} user
|
||||||
* @returns {Promise<oldLibraryItem[]>}
|
* @returns {Promise<LibraryItemExpanded[]>}
|
||||||
*/
|
*/
|
||||||
static async getForAuthor(author, user = null) {
|
static async getForAuthor(author, user = null) {
|
||||||
const { libraryItems } = await libraryFilters.getLibraryItemsForAuthor(author, user, undefined, undefined)
|
const { libraryItems } = await libraryFilters.getLibraryItemsForAuthor(author, user, undefined, undefined)
|
||||||
return libraryItems.map((li) => this.getOldLibraryItem(li))
|
return libraryItems
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get book library items in a collection
|
|
||||||
* @param {oldCollection} collection
|
|
||||||
* @returns {Promise<oldLibraryItem[]>}
|
|
||||||
*/
|
|
||||||
static async getForCollection(collection) {
|
|
||||||
const libraryItems = await libraryFilters.getLibraryItemsForCollection(collection)
|
|
||||||
return libraryItems.map((li) => this.getOldLibraryItem(li))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -269,16 +269,5 @@ class LibraryItem {
|
|||||||
this.isSavingMetadata = false
|
this.isSavingMetadata = false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
removeLibraryFile(ino) {
|
|
||||||
if (!ino) return false
|
|
||||||
const libraryFile = this.libraryFiles.find((lf) => lf.ino === ino)
|
|
||||||
if (libraryFile) {
|
|
||||||
this.libraryFiles = this.libraryFiles.filter((lf) => lf.ino !== ino)
|
|
||||||
this.updatedAt = Date.now()
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
module.exports = LibraryItem
|
module.exports = LibraryItem
|
||||||
|
Loading…
Reference in New Issue
Block a user