mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-12-06 06:56:27 +01:00
Merge branch 'master' into v3
This commit is contained in:
commit
ab05d7fb5f
7 files changed
+460
-32
No files matched your search
@@ -11,11 +11,24 @@ const CoverManager = require('../managers/CoverManager')
|
||||
const AuthorFinder = require('../finders/AuthorFinder')
|
||||
|
||||
const { reqSupportsWebp, isValidASIN, clampPositiveInt } = require('../utils/index')
|
||||
const htmlSanitizer = require('../utils/htmlSanitizer')
|
||||
|
||||
const naturalSort = createNewSortInstance({
|
||||
comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare
|
||||
})
|
||||
|
||||
/**
|
||||
* @param {string} description
|
||||
* @returns {string|null}
|
||||
*/
|
||||
function sanitizeAuthorDescription(description) {
|
||||
const sanitizedDescription = htmlSanitizer.sanitize(description)
|
||||
if (sanitizedDescription !== description) {
|
||||
Logger.debug(`[AuthorController] Sanitized description from "${description}" to "${sanitizedDescription}"`)
|
||||
}
|
||||
return sanitizedDescription || null
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef RequestUserObject
|
||||
* @property {import('../models/User')} user
|
||||
@@ -101,6 +114,10 @@ class AuthorController {
|
||||
payload[key] = req.body[key]
|
||||
}
|
||||
}
|
||||
if (payload.description) {
|
||||
payload.description = sanitizeAuthorDescription(payload.description)
|
||||
}
|
||||
|
||||
if (!Object.keys(payload).length) {
|
||||
Logger.error(`[AuthorController] Invalid request payload. No valid keys found`, req.body)
|
||||
return res.status(400).send('Invalid request payload. No valid keys found')
|
||||
@@ -364,9 +381,12 @@ class AuthorController {
|
||||
}
|
||||
}
|
||||
|
||||
if (authorData.description && req.author.description !== authorData.description) {
|
||||
req.author.description = authorData.description
|
||||
hasUpdates = true
|
||||
if (authorData.description) {
|
||||
const description = sanitizeAuthorDescription(authorData.description)
|
||||
if (req.author.description !== description) {
|
||||
req.author.description = description
|
||||
hasUpdates = true
|
||||
}
|
||||
}
|
||||
|
||||
if (hasUpdates) {
|
||||
|
||||
@@ -247,12 +247,17 @@ class CollectionController {
|
||||
return res.status(400).send('Book already in collection')
|
||||
}
|
||||
|
||||
// Create collectionBook record
|
||||
await Database.collectionBookModel.create({
|
||||
collectionId: req.collection.id,
|
||||
bookId: libraryItem.mediaId,
|
||||
order: collectionBooks.length + 1
|
||||
})
|
||||
// Create collectionBook record. A second add can pass the check above and hit the unique pair.
|
||||
try {
|
||||
await Database.collectionBookModel.create({
|
||||
collectionId: req.collection.id,
|
||||
bookId: libraryItem.mediaId,
|
||||
order: collectionBooks.length + 1
|
||||
})
|
||||
} catch (error) {
|
||||
if (error.name !== 'SequelizeUniqueConstraintError') throw error
|
||||
return res.status(400).send('Book already in collection')
|
||||
}
|
||||
const jsonExpanded = await req.collection.getOldJsonExpanded(req.user)
|
||||
if (jsonExpanded) SocketAuthority.clientEmitter(req.user.id, 'collection_updated', jsonExpanded)
|
||||
res.json(jsonExpanded)
|
||||
@@ -358,7 +363,7 @@ class CollectionController {
|
||||
|
||||
let jsonExpanded = null
|
||||
if (hasUpdated) {
|
||||
await Database.collectionBookModel.bulkCreate(collectionBooksToAdd)
|
||||
await Database.collectionBookModel.bulkCreate(collectionBooksToAdd, { ignoreDuplicates: true })
|
||||
|
||||
jsonExpanded = await req.collection.getOldJsonExpanded(req.user)
|
||||
if (jsonExpanded) SocketAuthority.clientEmitter(req.user.id, 'collection_updated', jsonExpanded)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const { DataTypes, Model, where, fn, col } = require('sequelize')
|
||||
const parseNameString = require('../utils/parsers/parseNameString')
|
||||
const htmlSanitizer = require('../utils/htmlSanitizer')
|
||||
|
||||
class Author extends Model {
|
||||
constructor(values, options) {
|
||||
@@ -175,11 +176,16 @@ class Author extends Model {
|
||||
}
|
||||
|
||||
toOldJSON() {
|
||||
let description = this.description
|
||||
if (description) {
|
||||
description = htmlSanitizer.sanitize(description) || null
|
||||
}
|
||||
|
||||
return {
|
||||
id: this.id,
|
||||
asin: this.asin,
|
||||
name: this.name,
|
||||
description: this.description,
|
||||
description,
|
||||
imagePath: this.imagePath,
|
||||
libraryId: this.libraryId,
|
||||
addedAt: this.createdAt.valueOf(),
|
||||
|
||||
+46
-21
@@ -260,16 +260,21 @@ class Scanner {
|
||||
// Update filter data
|
||||
Database.addAuthorToFilterData(libraryItem.libraryId, author.name, author.id)
|
||||
}
|
||||
await Database.bookAuthorModel
|
||||
.create({
|
||||
let bookAuthorCreated = true
|
||||
try {
|
||||
await Database.bookAuthorModel.create({
|
||||
authorId: author.id,
|
||||
bookId: libraryItem.media.id
|
||||
})
|
||||
.then(() => {
|
||||
Logger.info(`[Scanner] quickMatchBookBuildUpdatePayload: Added author "${author.name}" to "${libraryItem.media.title}"`)
|
||||
libraryItem.media.authors.push(author)
|
||||
hasAuthorUpdates = true
|
||||
})
|
||||
} catch (error) {
|
||||
if (error.name !== 'SequelizeUniqueConstraintError') throw error
|
||||
bookAuthorCreated = false
|
||||
}
|
||||
libraryItem.media.authors.push(author)
|
||||
hasAuthorUpdates = true
|
||||
if (bookAuthorCreated) {
|
||||
Logger.info(`[Scanner] quickMatchBookBuildUpdatePayload: Added author "${author.name}" to "${libraryItem.media.title}"`)
|
||||
}
|
||||
}
|
||||
const authorsRemoved = libraryItem.media.authors.filter((a) => !matchData.author.find((ma) => ma.toLowerCase() === a.name.toLowerCase()))
|
||||
if (authorsRemoved.length) {
|
||||
@@ -306,23 +311,43 @@ class Scanner {
|
||||
} else {
|
||||
let seriesItem = await Database.seriesModel.getByNameAndLibrary(seriesMatchItem.series, libraryItem.libraryId)
|
||||
if (!seriesItem) {
|
||||
seriesItem = await Database.seriesModel.create({
|
||||
name: seriesMatchItem.series,
|
||||
nameIgnorePrefix: getTitleIgnorePrefix(seriesMatchItem.series),
|
||||
libraryId: libraryItem.libraryId
|
||||
})
|
||||
// Update filter data
|
||||
Database.addSeriesToFilterData(libraryItem.libraryId, seriesItem.name, seriesItem.id)
|
||||
SocketAuthority.emitter('series_added', seriesItem.toOldJSON())
|
||||
let seriesCreated = false
|
||||
try {
|
||||
seriesItem = await Database.seriesModel.create({
|
||||
name: seriesMatchItem.series,
|
||||
nameIgnorePrefix: getTitleIgnorePrefix(seriesMatchItem.series),
|
||||
libraryId: libraryItem.libraryId
|
||||
})
|
||||
seriesCreated = true
|
||||
} catch (error) {
|
||||
if (error.name !== 'SequelizeUniqueConstraintError') throw error
|
||||
seriesItem = await Database.seriesModel.findOne({
|
||||
where: { name: seriesMatchItem.series, libraryId: libraryItem.libraryId }
|
||||
})
|
||||
if (!seriesItem) throw error
|
||||
}
|
||||
if (seriesCreated) {
|
||||
// Update filter data
|
||||
Database.addSeriesToFilterData(libraryItem.libraryId, seriesItem.name, seriesItem.id)
|
||||
SocketAuthority.emitter('series_added', seriesItem.toOldJSON())
|
||||
}
|
||||
}
|
||||
let bookSeries
|
||||
try {
|
||||
bookSeries = await Database.bookSeriesModel.create({
|
||||
seriesId: seriesItem.id,
|
||||
bookId: libraryItem.media.id,
|
||||
sequence: seriesMatchItem.sequence
|
||||
})
|
||||
Logger.info(`[Scanner] quickMatchBookBuildUpdatePayload: Added series "${seriesItem.name}" to "${libraryItem.media.title}"`)
|
||||
} catch (error) {
|
||||
if (error.name !== 'SequelizeUniqueConstraintError') throw error
|
||||
bookSeries = await Database.bookSeriesModel.findOne({
|
||||
where: { seriesId: seriesItem.id, bookId: libraryItem.media.id }
|
||||
})
|
||||
}
|
||||
const bookSeries = await Database.bookSeriesModel.create({
|
||||
seriesId: seriesItem.id,
|
||||
bookId: libraryItem.media.id,
|
||||
sequence: seriesMatchItem.sequence
|
||||
})
|
||||
seriesItem.bookSeries = bookSeries
|
||||
libraryItem.media.series.push(seriesItem)
|
||||
Logger.info(`[Scanner] quickMatchBookBuildUpdatePayload: Added series "${seriesItem.name}" to "${libraryItem.media.title}"`)
|
||||
hasSeriesUpdates = true
|
||||
}
|
||||
const seriesRemoved = libraryItem.media.series.filter((s) => !matchData.series.find((ms) => ms.series.toLowerCase() === s.name.toLowerCase()))
|
||||
|
||||
Reference in new issue
Block a user