mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-03-10 00:17:21 +01:00
Merge pull request #4088 from nichwall/checkRemoveAuthors_fix
Fix empty series delete check
This commit is contained in:
commit
ddd8f15f2b
@ -392,21 +392,51 @@ class ApiRouter {
|
|||||||
async checkRemoveEmptySeries(seriesIds) {
|
async checkRemoveEmptySeries(seriesIds) {
|
||||||
if (!seriesIds?.length) return
|
if (!seriesIds?.length) return
|
||||||
|
|
||||||
const series = await Database.seriesModel.findAll({
|
const transaction = await Database.sequelize.transaction()
|
||||||
where: {
|
try {
|
||||||
|
const seriesToRemove = (
|
||||||
|
await Database.seriesModel.findAll({
|
||||||
|
where: [
|
||||||
|
{
|
||||||
id: seriesIds
|
id: seriesIds
|
||||||
},
|
},
|
||||||
|
sequelize.where(sequelize.literal('(SELECT count(*) FROM bookSeries bs WHERE bs.seriesId = series.id)'), 0)
|
||||||
|
],
|
||||||
attributes: ['id', 'name', 'libraryId'],
|
attributes: ['id', 'name', 'libraryId'],
|
||||||
include: {
|
include: {
|
||||||
model: Database.bookModel,
|
model: Database.bookModel,
|
||||||
attributes: ['id']
|
attributes: ['id'],
|
||||||
}
|
required: false // Ensure it includes series even if no books exist
|
||||||
|
},
|
||||||
|
transaction
|
||||||
})
|
})
|
||||||
|
).map((s) => ({ id: s.id, name: s.name, libraryId: s.libraryId }))
|
||||||
|
|
||||||
for (const s of series) {
|
if (seriesToRemove.length) {
|
||||||
if (!s.books.length) {
|
await Database.seriesModel.destroy({
|
||||||
await this.removeEmptySeries(s)
|
where: {
|
||||||
|
id: seriesToRemove.map((s) => s.id)
|
||||||
|
},
|
||||||
|
transaction
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await transaction.commit()
|
||||||
|
|
||||||
|
seriesToRemove.forEach(({ id, name, libraryId }) => {
|
||||||
|
Logger.info(`[ApiRouter] Series "${name}" is now empty. Removing series`)
|
||||||
|
|
||||||
|
// Remove series from library filter data
|
||||||
|
Database.removeSeriesFromFilterData(libraryId, id)
|
||||||
|
SocketAuthority.emitter('series_removed', { id: id, libraryId: libraryId })
|
||||||
|
})
|
||||||
|
// Close rss feeds - remove from db and emit socket event
|
||||||
|
if (seriesToRemove.length) {
|
||||||
|
await RssFeedManager.closeFeedsForEntityIds(seriesToRemove.map((s) => s.id))
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
await transaction.rollback()
|
||||||
|
Logger.error(`[ApiRouter] Error removing empty series: ${error.message}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -420,6 +450,9 @@ class ApiRouter {
|
|||||||
async checkRemoveAuthorsWithNoBooks(authorIds) {
|
async checkRemoveAuthorsWithNoBooks(authorIds) {
|
||||||
if (!authorIds?.length) return
|
if (!authorIds?.length) return
|
||||||
|
|
||||||
|
const transaction = await Database.sequelize.transaction()
|
||||||
|
try {
|
||||||
|
// Select authors with locking to prevent concurrent updates
|
||||||
const bookAuthorsToRemove = (
|
const bookAuthorsToRemove = (
|
||||||
await Database.authorModel.findAll({
|
await Database.authorModel.findAll({
|
||||||
where: [
|
where: [
|
||||||
@ -438,7 +471,8 @@ class ApiRouter {
|
|||||||
sequelize.where(sequelize.literal('(SELECT count(*) FROM bookAuthors ba WHERE ba.authorId = author.id)'), 0)
|
sequelize.where(sequelize.literal('(SELECT count(*) FROM bookAuthors ba WHERE ba.authorId = author.id)'), 0)
|
||||||
],
|
],
|
||||||
attributes: ['id', 'name', 'libraryId'],
|
attributes: ['id', 'name', 'libraryId'],
|
||||||
raw: true
|
raw: true,
|
||||||
|
transaction
|
||||||
})
|
})
|
||||||
).map((au) => ({ id: au.id, name: au.name, libraryId: au.libraryId }))
|
).map((au) => ({ id: au.id, name: au.name, libraryId: au.libraryId }))
|
||||||
|
|
||||||
@ -446,35 +480,26 @@ class ApiRouter {
|
|||||||
await Database.authorModel.destroy({
|
await Database.authorModel.destroy({
|
||||||
where: {
|
where: {
|
||||||
id: bookAuthorsToRemove.map((au) => au.id)
|
id: bookAuthorsToRemove.map((au) => au.id)
|
||||||
}
|
},
|
||||||
|
transaction
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
await transaction.commit()
|
||||||
|
|
||||||
|
// Remove all book authors after completing remove from database
|
||||||
bookAuthorsToRemove.forEach(({ id, name, libraryId }) => {
|
bookAuthorsToRemove.forEach(({ id, name, libraryId }) => {
|
||||||
Database.removeAuthorFromFilterData(libraryId, id)
|
Database.removeAuthorFromFilterData(libraryId, id)
|
||||||
// TODO: Clients were expecting full author in payload but its unnecessary
|
// TODO: Clients were expecting full author in payload but its unnecessary
|
||||||
SocketAuthority.emitter('author_removed', { id, libraryId })
|
SocketAuthority.emitter('author_removed', { id, libraryId })
|
||||||
Logger.info(`[ApiRouter] Removed author "${name}" with no books`)
|
Logger.info(`[ApiRouter] Removed author "${name}" with no books`)
|
||||||
})
|
})
|
||||||
|
} catch (error) {
|
||||||
|
await transaction.rollback()
|
||||||
|
Logger.error(`[ApiRouter] Error removing authors: ${error.message}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove an empty series & close an open RSS feed
|
|
||||||
* @param {import('../models/Series')} series
|
|
||||||
*/
|
|
||||||
async removeEmptySeries(series) {
|
|
||||||
await RssFeedManager.closeFeedForEntityId(series.id)
|
|
||||||
Logger.info(`[ApiRouter] Series "${series.name}" is now empty. Removing series`)
|
|
||||||
|
|
||||||
// Remove series from library filter data
|
|
||||||
Database.removeSeriesFromFilterData(series.libraryId, series.id)
|
|
||||||
SocketAuthority.emitter('series_removed', {
|
|
||||||
id: series.id,
|
|
||||||
libraryId: series.libraryId
|
|
||||||
})
|
|
||||||
|
|
||||||
await series.destroy()
|
|
||||||
}
|
|
||||||
|
|
||||||
async getUserListeningSessionsHelper(userId) {
|
async getUserListeningSessionsHelper(userId) {
|
||||||
const userSessions = await Database.getPlaybackSessions({ userId })
|
const userSessions = await Database.getPlaybackSessions({ userId })
|
||||||
return userSessions.sort((a, b) => b.updatedAt - a.updatedAt)
|
return userSessions.sort((a, b) => b.updatedAt - a.updatedAt)
|
||||||
|
Loading…
Reference in New Issue
Block a user