Skip to content

Commit

Permalink
Add published decade filter option (advplyr#3489)
Browse files Browse the repository at this point in the history
* Add strings for PublishedDecade and PublishedDecades

* Add publishedDecades filter options to LibraryFilterSelect

* Add publishedDecades to libraries store

* Add publishedDecades to getFilterData

* Add database method to add published decades to filter data

* Add published decade in BookScanner

* Add 'publishedDecades' to invalidFilters in user.js

* Add publishedDecades filter group to MediaGroupQuery

* Update client/strings/en-us.json

* Auto formatting

---------

Co-authored-by: advplyr <[email protected]>
Co-authored-by: advplyr <[email protected]>
  • Loading branch information
3 people authored Oct 8, 2024
1 parent e42db12 commit f38b663
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 4 deletions.
9 changes: 9 additions & 0 deletions client/components/controls/LibraryFilterSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ export default {
value: 'publishers',
sublist: true
},
{
text: this.$strings.LabelPublishedDecade,
textPlural: this.$strings.LabelPublishedDecades,
value: 'publishedDecades',
sublist: true
},
{
text: this.$strings.LabelLanguage,
textPlural: this.$strings.LabelLanguages,
Expand Down Expand Up @@ -338,6 +344,9 @@ export default {
publishers() {
return this.filterData.publishers || []
},
publishedDecades() {
return this.filterData.publishedDecades || []
},
progress() {
return [
{
Expand Down
13 changes: 12 additions & 1 deletion client/store/libraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ export const mutations = {
series: [],
narrators: [],
languages: [],
publishers: []
publishers: [],
publishedDecades: []
}
*/
const mediaMetadata = libraryItem.media.metadata
Expand Down Expand Up @@ -307,6 +308,16 @@ export const mutations = {
state.filterData.publishers.sort((a, b) => a.localeCompare(b))
}

// Add publishedDecades
if (mediaMetadata.publishedYear) {
const publishedYear = parseInt(mediaMetadata.publishedYear, 10)
const decade = Math.floor(publishedYear / 10) * 10
if (!state.filterData.publishedDecades.includes(decade)) {
state.filterData.publishedDecades.push(decade)
state.filterData.publishedDecades.sort((a, b) => a - b)
}
}

// Add language
if (mediaMetadata.language && !state.filterData.languages.includes(mediaMetadata.language)) {
state.filterData.languages.push(mediaMetadata.language)
Expand Down
2 changes: 1 addition & 1 deletion client/store/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const actions = {
if (state.settings.orderBy == 'media.metadata.publishedYear') {
settingsUpdate.orderBy = 'media.metadata.title'
}
const invalidFilters = ['series', 'authors', 'narrators', 'publishers', 'languages', 'progress', 'issues', 'ebooks', 'abridged']
const invalidFilters = ['series', 'authors', 'narrators', 'publishers', 'publishedDecades', 'languages', 'progress', 'issues', 'ebooks', 'abridged']
const filterByFirstPart = (state.settings.filterBy || '').split('.').shift()
if (invalidFilters.includes(filterByFirstPart)) {
settingsUpdate.filterBy = 'all'
Expand Down
2 changes: 2 additions & 0 deletions client/strings/en-us.json
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,8 @@
"LabelPubDate": "Pub Date",
"LabelPublishYear": "Publish Year",
"LabelPublishedDate": "Published {0}",
"LabelPublishedDecade": "Published Decade",
"LabelPublishedDecades": "Published Decades",
"LabelPublisher": "Publisher",
"LabelPublishers": "Publishers",
"LabelRSSFeedCustomOwnerEmail": "Custom owner Email",
Expand Down
5 changes: 5 additions & 0 deletions server/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,11 @@ class Database {
this.libraryFilterData[libraryId].publishers.push(publisher)
}

addPublishedDecadeToFilterData(libraryId, decade) {
if (!this.libraryFilterData[libraryId] || !decade || this.libraryFilterData[libraryId].publishedDecades.includes(decade)) return
this.libraryFilterData[libraryId].publishedDecades.push(decade)
}

addLanguageToFilterData(libraryId, language) {
if (!this.libraryFilterData[libraryId] || !language || this.libraryFilterData[libraryId].languages.includes(language)) return
this.libraryFilterData[libraryId].languages.push(language)
Expand Down
4 changes: 4 additions & 0 deletions server/scanner/BookScanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,10 @@ class BookScanner {
Database.addPublisherToFilterData(libraryItemData.libraryId, libraryItem.book.publisher)
Database.addLanguageToFilterData(libraryItemData.libraryId, libraryItem.book.language)

const publishedYear = libraryItem.book.publishedYear
const decade = publishedYear ? `${Math.floor(publishedYear / 10) * 10}` : null
Database.addPublishedDecadeToFilterData(libraryItemData.libraryId, decade)

// Load for emitting to client
libraryItem.media = await libraryItem.getMedia({
include: [
Expand Down
11 changes: 9 additions & 2 deletions server/utils/queries/libraryFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {
let filterValue = null
let filterGroup = null
if (filterBy) {
const searchGroups = ['genres', 'tags', 'series', 'authors', 'progress', 'narrators', 'publishers', 'missing', 'languages', 'tracks', 'ebooks']
const searchGroups = ['genres', 'tags', 'series', 'authors', 'progress', 'narrators', 'publishers', 'publishedDecades', 'missing', 'languages', 'tracks', 'ebooks']
const group = searchGroups.find((_group) => filterBy.startsWith(_group + '.'))
filterGroup = group || filterBy
filterValue = group ? this.decode(filterBy.replace(`${group}.`, '')) : null
Expand Down Expand Up @@ -458,6 +458,7 @@ module.exports = {
narrators: new Set(),
languages: new Set(),
publishers: new Set(),
publishedDecades: new Set(),
numIssues: 0
}

Expand Down Expand Up @@ -492,7 +493,7 @@ module.exports = {
libraryId: libraryId
}
},
attributes: ['tags', 'genres', 'publisher', 'narrators', 'language']
attributes: ['tags', 'genres', 'publisher', 'publishedYear', 'narrators', 'language']
})
for (const book of books) {
if (book.libraryItem.isMissing || book.libraryItem.isInvalid) data.numIssues++
Expand All @@ -506,6 +507,11 @@ module.exports = {
book.narrators.forEach((narrator) => data.narrators.add(narrator))
}
if (book.publisher) data.publishers.add(book.publisher)
// Check if published year exists and is valid
if (book.publishedYear && !isNaN(book.publishedYear) && book.publishedYear > 0 && book.publishedYear < 3000 && book.publishedYear.toString().length === 4) {
const decade = Math.floor(book.publishedYear / 10) * 10
data.publishedDecades.add(decade.toString())
}
if (book.language) data.languages.add(book.language)
}

Expand All @@ -532,6 +538,7 @@ module.exports = {
data.series = naturalSort(data.series).asc((se) => se.name)
data.narrators = naturalSort([...data.narrators]).asc()
data.publishers = naturalSort([...data.publishers]).asc()
data.publishedDecades = naturalSort([...data.publishedDecades]).asc()
data.languages = naturalSort([...data.languages]).asc()
data.loadedAt = Date.now()
Database.libraryFilterData[libraryId] = data
Expand Down
5 changes: 5 additions & 0 deletions server/utils/queries/libraryItemsBookFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ module.exports = {
} else if (value === 'series') {
mediaWhere['$series.id$'] = null
}
} else if (group === 'publishedDecades') {
const year = parseInt(value, 10)
mediaWhere['publishedYear'] = {
[Sequelize.Op.between]: year >= 1000 ? [year, year + 9] : [year * 10, (year + 1) * 10 - 1]
}
}

return { mediaWhere, replacements }
Expand Down

0 comments on commit f38b663

Please sign in to comment.