mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-07-09 03:04:08 -04:00
Fix: empty series delete flakiness
This commit is contained in:
parent
167617cce0
commit
84e20e041c
@ -392,21 +392,47 @@ 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 {
|
||||||
id: seriesIds
|
const seriesBooksToRemove = (
|
||||||
},
|
await Database.seriesModel.findAll({
|
||||||
attributes: ['id', 'name', 'libraryId'],
|
where: [
|
||||||
include: {
|
{
|
||||||
model: Database.bookModel,
|
id: seriesIds
|
||||||
attributes: ['id']
|
},
|
||||||
}
|
sequelize.where(sequelize.literal('(SELECT count(*) FROM bookSeries bs WHERE bs.seriesId = series.id)'), 0)
|
||||||
})
|
],
|
||||||
|
attributes: ['id', 'name', 'libraryId'],
|
||||||
|
include: {
|
||||||
|
model: Database.bookModel,
|
||||||
|
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 (seriesBooksToRemove.length) {
|
||||||
if (!s.books.length) {
|
await Database.seriesModel.destroy({
|
||||||
await this.removeEmptySeries(s)
|
where: {
|
||||||
|
id: seriesBooksToRemove.map((s) => s.id)
|
||||||
|
},
|
||||||
|
transaction
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await transaction.commit()
|
||||||
|
|
||||||
|
seriesBooksToRemove.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 })
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
await transaction.rollback()
|
||||||
|
Logger.error(`[ApiRouter] Error removing empty series: ${error.message}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -470,24 +496,6 @@ class ApiRouter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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…
x
Reference in New Issue
Block a user