From f78d287b595fb2f0bfdda665c6da601f4d2fe238 Mon Sep 17 00:00:00 2001 From: advplyr Date: Fri, 13 May 2022 18:11:54 -0500 Subject: [PATCH] Update:Matching authors uses the ASIN if set #572, Fix:Purge author image cache when updating author --- client/components/cards/AuthorCard.vue | 9 ++- client/components/cards/SearchAuthorCard.vue | 73 ------------------- .../components/modals/authors/EditModal.vue | 9 ++- server/controllers/AuthorController.js | 11 ++- server/finders/AuthorFinder.js | 5 ++ server/objects/entities/Author.js | 2 +- server/providers/Audnexus.js | 13 ++++ 7 files changed, 43 insertions(+), 79 deletions(-) delete mode 100644 client/components/cards/SearchAuthorCard.vue diff --git a/client/components/cards/AuthorCard.vue b/client/components/cards/AuthorCard.vue index 143c0546..9f5803ed 100644 --- a/client/components/cards/AuthorCard.vue +++ b/client/components/cards/AuthorCard.vue @@ -65,6 +65,9 @@ export default { name() { return this._author.name || '' }, + asin() { + return this._author.asin || '' + }, numBooks() { return this._author.numBooks || 0 }, @@ -81,7 +84,11 @@ export default { }, async searchAuthor() { this.searching = true - var response = await this.$axios.$post(`/api/authors/${this.authorId}/match`, { q: this.name }).catch((error) => { + const payload = {} + if (this.asin) payload.asin = this.asin + else payload.q = this.name + + var response = await this.$axios.$post(`/api/authors/${this.authorId}/match`, payload).catch((error) => { console.error('Failed', error) return null }) diff --git a/client/components/cards/SearchAuthorCard.vue b/client/components/cards/SearchAuthorCard.vue deleted file mode 100644 index 62c68750..00000000 --- a/client/components/cards/SearchAuthorCard.vue +++ /dev/null @@ -1,73 +0,0 @@ - - - \ No newline at end of file diff --git a/client/components/modals/authors/EditModal.vue b/client/components/modals/authors/EditModal.vue index 4ff04e8d..503f8b12 100644 --- a/client/components/modals/authors/EditModal.vue +++ b/client/components/modals/authors/EditModal.vue @@ -139,12 +139,17 @@ export default { this.processing = false }, async searchAuthor() { - if (!this.authorCopy.name) { + if (!this.authorCopy.name && !this.authorCopy.asin) { this.$toast.error('Must enter an author name') return } this.processing = true - var response = await this.$axios.$post(`/api/authors/${this.authorId}/match`, { q: this.authorCopy.name }).catch((error) => { + + const payload = {} + if (this.authorCopy.asin) payload.asin = this.authorCopy.asin + else payload.q = this.authorCopy.name + + var response = await this.$axios.$post(`/api/authors/${this.authorId}/match`, payload).catch((error) => { console.error('Failed', error) return null }) diff --git a/server/controllers/AuthorController.js b/server/controllers/AuthorController.js index d353a10e..856f8822 100644 --- a/server/controllers/AuthorController.js +++ b/server/controllers/AuthorController.js @@ -107,11 +107,16 @@ class AuthorController { } async match(req, res) { - var authorData = await this.authorFinder.findAuthorByName(req.body.q) + var authorData = null + if (req.body.asin) { + authorData = await this.authorFinder.findAuthorByASIN(req.body.asin) + } else { + authorData = await this.authorFinder.findAuthorByName(req.body.q) + } if (!authorData) { return res.status(404).send('Author not found') } - Logger.debug(`[AuthorController] match author with "${req.body.q}"`, authorData) + Logger.debug(`[AuthorController] match author with "${req.body.q || req.body.asin}"`, authorData) var hasUpdates = false if (authorData.asin && req.author.asin !== authorData.asin) { @@ -121,6 +126,8 @@ class AuthorController { // Only updates image if there was no image before or the author ASIN was updated if (authorData.image && (!req.author.imagePath || hasUpdates)) { + this.cacheManager.purgeImageCache(req.author.id) + var imageData = await this.authorFinder.saveAuthorImage(req.author.id, authorData.image) if (imageData) { req.author.imagePath = imageData.path diff --git a/server/finders/AuthorFinder.js b/server/finders/AuthorFinder.js index f1c668a7..04308012 100644 --- a/server/finders/AuthorFinder.js +++ b/server/finders/AuthorFinder.js @@ -19,6 +19,11 @@ class AuthorFinder { }) } + findAuthorByASIN(asin) { + if (!asin) return null + return this.audnexus.findAuthorByASIN(asin) + } + async findAuthorByName(name, options = {}) { if (!name) return null const maxLevenshtein = !isNaN(options.maxLevenshtein) ? Number(options.maxLevenshtein) : 3 diff --git a/server/objects/entities/Author.js b/server/objects/entities/Author.js index 2c7f54fd..56f9233d 100644 --- a/server/objects/entities/Author.js +++ b/server/objects/entities/Author.js @@ -37,7 +37,7 @@ class Author { imagePath: this.imagePath, relImagePath: this.relImagePath, addedAt: this.addedAt, - lastUpdate: this.updatedAt + updatedAt: this.updatedAt } } diff --git a/server/providers/Audnexus.js b/server/providers/Audnexus.js index 23f9ba4f..10518ec8 100644 --- a/server/providers/Audnexus.js +++ b/server/providers/Audnexus.js @@ -27,6 +27,19 @@ class Audnexus { }) } + async findAuthorByASIN(asin) { + var author = await this.authorRequest(asin) + if (!author) { + return null + } + return { + asin: author.asin, + description: author.description, + image: author.image, + name: author.name + } + } + async findAuthorByName(name, maxLevenshtein = 3) { Logger.debug(`[Audnexus] Looking up author by name ${name}`) var asins = await this.authorASINsRequest(name)