2022-03-12 02:46:32 +01:00
|
|
|
const Logger = require('../../Logger')
|
|
|
|
const { areEquivalent, copyValue } = require('../../utils/index')
|
|
|
|
|
2022-03-09 02:31:44 +01:00
|
|
|
class BookMetadata {
|
|
|
|
constructor(metadata) {
|
|
|
|
this.title = null
|
|
|
|
this.subtitle = null
|
|
|
|
this.authors = []
|
|
|
|
this.narrators = [] // Array of strings
|
|
|
|
this.series = []
|
|
|
|
this.genres = [] // Array of strings
|
|
|
|
this.publishedYear = null
|
|
|
|
this.publishedDate = null
|
|
|
|
this.publisher = null
|
|
|
|
this.description = null
|
|
|
|
this.isbn = null
|
|
|
|
this.asin = null
|
|
|
|
this.language = null
|
2022-03-11 01:45:02 +01:00
|
|
|
this.explicit = false
|
2022-03-09 02:31:44 +01:00
|
|
|
|
|
|
|
if (metadata) {
|
|
|
|
this.construct(metadata)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(metadata) {
|
|
|
|
this.title = metadata.title
|
|
|
|
this.subtitle = metadata.subtitle
|
2022-03-10 02:23:17 +01:00
|
|
|
this.authors = (metadata.authors && metadata.authors.map) ? metadata.authors.map(a => ({ ...a })) : []
|
|
|
|
this.narrators = metadata.narrators ? [...metadata.narrators] : []
|
|
|
|
this.series = (metadata.series && metadata.series.map) ? metadata.series.map(s => ({ ...s })) : []
|
|
|
|
this.genres = metadata.genres ? [...metadata.genres] : []
|
|
|
|
this.publishedYear = metadata.publishedYear || null
|
|
|
|
this.publishedDate = metadata.publishedDate || null
|
2022-03-09 02:31:44 +01:00
|
|
|
this.publisher = metadata.publisher
|
|
|
|
this.description = metadata.description
|
|
|
|
this.isbn = metadata.isbn
|
|
|
|
this.asin = metadata.asin
|
|
|
|
this.language = metadata.language
|
2022-03-11 01:45:02 +01:00
|
|
|
this.explicit = metadata.explicit
|
2022-03-09 02:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
title: this.title,
|
|
|
|
subtitle: this.subtitle,
|
|
|
|
authors: this.authors.map(a => ({ ...a })), // Author JSONMinimal with name and id
|
|
|
|
narrators: [...this.narrators],
|
|
|
|
series: this.series.map(s => ({ ...s })),
|
|
|
|
genres: [...this.genres],
|
|
|
|
publishedYear: this.publishedYear,
|
|
|
|
publishedDate: this.publishedDate,
|
|
|
|
publisher: this.publisher,
|
|
|
|
description: this.description,
|
|
|
|
isbn: this.isbn,
|
|
|
|
asin: this.asin,
|
2022-03-11 01:45:02 +01:00
|
|
|
language: this.language,
|
|
|
|
explicit: this.explicit
|
2022-03-09 02:31:44 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-11 01:45:02 +01:00
|
|
|
|
|
|
|
toJSONExpanded() {
|
|
|
|
return {
|
|
|
|
title: this.title,
|
|
|
|
subtitle: this.subtitle,
|
|
|
|
authors: this.authors.map(a => ({ ...a })), // Author JSONMinimal with name and id
|
|
|
|
narrators: [...this.narrators],
|
|
|
|
series: this.series.map(s => ({ ...s })),
|
|
|
|
genres: [...this.genres],
|
|
|
|
publishedYear: this.publishedYear,
|
|
|
|
publishedDate: this.publishedDate,
|
|
|
|
publisher: this.publisher,
|
|
|
|
description: this.description,
|
|
|
|
isbn: this.isbn,
|
|
|
|
asin: this.asin,
|
|
|
|
language: this.language,
|
|
|
|
explicit: this.explicit,
|
|
|
|
authorName: this.authorName,
|
|
|
|
narratorName: this.narratorName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get titleIgnorePrefix() {
|
|
|
|
if (!this.title) return ''
|
|
|
|
if (this.title.toLowerCase().startsWith('the ')) {
|
|
|
|
return this.title.substr(4) + ', The'
|
|
|
|
}
|
|
|
|
return this.title
|
|
|
|
}
|
|
|
|
get authorName() {
|
|
|
|
return this.authors.map(au => au.name).join(', ')
|
|
|
|
}
|
|
|
|
get narratorName() {
|
|
|
|
return this.narrators.join(', ')
|
|
|
|
}
|
|
|
|
|
|
|
|
hasAuthor(authorName) {
|
|
|
|
return !!this.authors.find(au => au.name == authorName)
|
|
|
|
}
|
|
|
|
hasSeries(seriesName) {
|
|
|
|
return !!this.series.find(se => se.name == seriesName)
|
|
|
|
}
|
|
|
|
hasNarrator(narratorName) {
|
|
|
|
return this.narrators.includes(narratorName)
|
|
|
|
}
|
2022-03-12 02:46:32 +01:00
|
|
|
|
|
|
|
update(payload) {
|
|
|
|
var json = this.toJSON()
|
|
|
|
var hasUpdates = false
|
|
|
|
for (const key in json) {
|
|
|
|
if (payload[key] !== undefined) {
|
|
|
|
if (!areEquivalent(payload[key], json[key])) {
|
|
|
|
this[key] = copyValue(payload[key])
|
|
|
|
Logger.debug('[BookMetadata] Key updated', key, this[key])
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasUpdates
|
|
|
|
}
|
2022-03-09 02:31:44 +01:00
|
|
|
}
|
|
|
|
module.exports = BookMetadata
|