From 093124aac694fbcb18184f84ecbb8dc8aaca7554 Mon Sep 17 00:00:00 2001 From: mikiher Date: Mon, 30 Mar 2026 22:02:56 +0300 Subject: [PATCH 1/2] Emit proper author_updated/added events when updating book media --- server/models/Author.js | 5 +++-- server/models/Book.js | 13 ++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/server/models/Author.js b/server/models/Author.js index 287b66976..d83eef15f 100644 --- a/server/models/Author.js +++ b/server/models/Author.js @@ -115,12 +115,13 @@ class Author extends Model { */ static async findOrCreateByNameAndLibrary(name, libraryId) { const author = await this.getByNameAndLibrary(name, libraryId) - if (author) return author - return this.create({ + if (author) return { author, created: false } + const newAuthor = await this.create({ name, lastFirst: this.getLastFirst(name), libraryId }) + return { author: newAuthor, created: true } } /** diff --git a/server/models/Book.js b/server/models/Book.js index 96371f3a2..d9f2ff132 100644 --- a/server/models/Book.js +++ b/server/models/Book.js @@ -4,6 +4,7 @@ const { getTitlePrefixAtEnd, getTitleIgnorePrefix } = require('../utils') const parseNameString = require('../utils/parsers/parseNameString') const htmlSanitizer = require('../utils/htmlSanitizer') const libraryItemsBookFilters = require('../utils/queries/libraryItemsBookFilters') +const SocketAuthority = require('../SocketAuthority') /** * @typedef EBookFileObject @@ -470,13 +471,23 @@ class Book extends Model { for (const author of authorsRemoved) { 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}"`) this.authors = this.authors.filter((au) => au.id !== author.id) } const authorsAdded = [] for (const authorName of newAuthorNames) { - const author = await authorModel.findOrCreateByNameAndLibrary(authorName, libraryId) + const { author, created } = await authorModel.findOrCreateByNameAndLibrary(authorName, libraryId) 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}"`) this.authors.push(author) authorsAdded.push(author) From ab3bd6f4a170d7ddf6e50dbeb4e5794f1a011e41 Mon Sep 17 00:00:00 2001 From: advplyr Date: Mon, 30 Mar 2026 16:22:27 -0500 Subject: [PATCH 2/2] Update JS docs --- server/models/Author.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/models/Author.js b/server/models/Author.js index d83eef15f..65561e211 100644 --- a/server/models/Author.js +++ b/server/models/Author.js @@ -111,7 +111,7 @@ class Author extends Model { * * @param {string} name * @param {string} libraryId - * @returns {Promise} + * @returns {Promise<{ author: Author, created: boolean }>} */ static async findOrCreateByNameAndLibrary(name, libraryId) { const author = await this.getByNameAndLibrary(name, libraryId)