Compare commits

..

No commits in common. "c4c8b8d0f2e57e4f6861f75ee36e788dd1e84fac" and "5de92d08f9018ed6e0b3af686f00c54b43d0fa27" have entirely different histories.

2 changed files with 4 additions and 16 deletions

View file

@ -111,17 +111,16 @@ class Author extends Model {
* *
* @param {string} name * @param {string} name
* @param {string} libraryId * @param {string} libraryId
* @returns {Promise<{ author: Author, created: boolean }>} * @returns {Promise<Author>}
*/ */
static async findOrCreateByNameAndLibrary(name, libraryId) { static async findOrCreateByNameAndLibrary(name, libraryId) {
const author = await this.getByNameAndLibrary(name, libraryId) const author = await this.getByNameAndLibrary(name, libraryId)
if (author) return { author, created: false } if (author) return author
const newAuthor = await this.create({ return this.create({
name, name,
lastFirst: this.getLastFirst(name), lastFirst: this.getLastFirst(name),
libraryId libraryId
}) })
return { author: newAuthor, created: true }
} }
/** /**

View file

@ -4,7 +4,6 @@ const { getTitlePrefixAtEnd, getTitleIgnorePrefix } = require('../utils')
const parseNameString = require('../utils/parsers/parseNameString') const parseNameString = require('../utils/parsers/parseNameString')
const htmlSanitizer = require('../utils/htmlSanitizer') const htmlSanitizer = require('../utils/htmlSanitizer')
const libraryItemsBookFilters = require('../utils/queries/libraryItemsBookFilters') const libraryItemsBookFilters = require('../utils/queries/libraryItemsBookFilters')
const SocketAuthority = require('../SocketAuthority')
/** /**
* @typedef EBookFileObject * @typedef EBookFileObject
@ -471,23 +470,13 @@ class Book extends Model {
for (const author of authorsRemoved) { for (const author of authorsRemoved) {
await bookAuthorModel.removeByIds(author.id, this.id) await bookAuthorModel.removeByIds(author.id, this.id)
const numBooks = await bookAuthorModel.getCountForAuthor(author.id)
if (numBooks > 0) {
SocketAuthority.emitter('author_updated', author.toOldJSONExpanded(numBooks))
}
Logger.debug(`[Book] "${this.title}" Removed author "${author.name}"`) Logger.debug(`[Book] "${this.title}" Removed author "${author.name}"`)
this.authors = this.authors.filter((au) => au.id !== author.id) this.authors = this.authors.filter((au) => au.id !== author.id)
} }
const authorsAdded = [] const authorsAdded = []
for (const authorName of newAuthorNames) { for (const authorName of newAuthorNames) {
const { author, created } = await authorModel.findOrCreateByNameAndLibrary(authorName, libraryId) const author = await authorModel.findOrCreateByNameAndLibrary(authorName, libraryId)
await bookAuthorModel.create({ bookId: this.id, authorId: author.id }) await bookAuthorModel.create({ bookId: this.id, authorId: author.id })
if (created) {
SocketAuthority.emitter('author_added', author.toOldJSON())
} else {
const numBooks = await bookAuthorModel.getCountForAuthor(author.id)
SocketAuthority.emitter('author_updated', author.toOldJSONExpanded(numBooks))
}
Logger.debug(`[Book] "${this.title}" Added author "${author.name}"`) Logger.debug(`[Book] "${this.title}" Added author "${author.name}"`)
this.authors.push(author) this.authors.push(author)
authorsAdded.push(author) authorsAdded.push(author)