Skip to content

Commit

Permalink
Update home page queries merging listen/read shelves, limit recent sh…
Browse files Browse the repository at this point in the history
…elves to 60 days out
  • Loading branch information
advplyr committed Aug 7, 2023
1 parent fbb5fd4 commit 33e04d0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 43 deletions.
49 changes: 25 additions & 24 deletions server/models/LibraryItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,33 +493,34 @@ module.exports = (sequelize) => {
// "Continue Listening" shelf
const itemsInProgressPayload = await libraryFilters.getMediaItemsInProgress(library, user, include, limit, false)
if (itemsInProgressPayload.items.length) {
const ebookOnlyItemsInProgress = itemsInProgressPayload.items.filter(li => li.media.isEBookOnly)
const audioOnlyItemsInProgress = itemsInProgressPayload.items.filter(li => !li.media.isEBookOnly)

shelves.push({
id: 'continue-listening',
label: 'Continue Listening',
labelStringKey: 'LabelContinueListening',
type: library.isPodcast ? 'episode' : 'book',
entities: itemsInProgressPayload.items,
entities: audioOnlyItemsInProgress,
total: itemsInProgressPayload.count
})
}
Logger.debug(`Loaded ${itemsInProgressPayload.items.length} of ${itemsInProgressPayload.count} items for "Continue Listening" in ${((Date.now() - fullStart) / 1000).toFixed(2)}s`)

let start = Date.now()
if (library.isBook) {
// "Continue Reading" shelf
const ebooksInProgressPayload = await libraryFilters.getMediaItemsInProgress(library, user, include, limit, true)
if (ebooksInProgressPayload.items.length) {
if (ebookOnlyItemsInProgress.length) {
// "Continue Reading" shelf
shelves.push({
id: 'continue-reading',
label: 'Continue Reading',
labelStringKey: 'LabelContinueReading',
type: 'book',
entities: ebooksInProgressPayload.items,
total: ebooksInProgressPayload.count
entities: ebookOnlyItemsInProgress,
total: itemsInProgressPayload.count
})
}
Logger.debug(`Loaded ${ebooksInProgressPayload.items.length} of ${ebooksInProgressPayload.count} items for "Continue Reading" in ${((Date.now() - start) / 1000).toFixed(2)}s`)
}
Logger.debug(`Loaded ${itemsInProgressPayload.items.length} of ${itemsInProgressPayload.count} items for "Continue Listening/Reading" in ${((Date.now() - fullStart) / 1000).toFixed(2)}s`)

let start = Date.now()
if (library.isBook) {
start = Date.now()
// "Continue Series" shelf
const continueSeriesPayload = await libraryFilters.getLibraryItemsContinueSeries(library, user, include, limit)
Expand Down Expand Up @@ -599,35 +600,35 @@ module.exports = (sequelize) => {

start = Date.now()
// "Listen Again" shelf
const listenAgainPayload = await libraryFilters.getMediaFinished(library, user, include, limit, false)
if (listenAgainPayload.items.length) {
const mediaFinishedPayload = await libraryFilters.getMediaFinished(library, user, include, limit)
if (mediaFinishedPayload.items.length) {
const ebookOnlyItemsInProgress = mediaFinishedPayload.items.filter(li => li.media.isEBookOnly)
const audioOnlyItemsInProgress = mediaFinishedPayload.items.filter(li => !li.media.isEBookOnly)

shelves.push({
id: 'listen-again',
label: 'Listen Again',
labelStringKey: 'LabelListenAgain',
type: library.isPodcast ? 'episode' : 'book',
entities: listenAgainPayload.items,
total: listenAgainPayload.count
entities: audioOnlyItemsInProgress,
total: mediaFinishedPayload.count
})
}
Logger.debug(`Loaded ${listenAgainPayload.items.length} of ${listenAgainPayload.count} items for "Listen Again" in ${((Date.now() - start) / 1000).toFixed(2)}s`)

if (library.isBook) {
start = Date.now()
// "Read Again" shelf
const readAgainPayload = await libraryFilters.getMediaFinished(library, user, include, limit, true)
if (readAgainPayload.items.length) {
if (ebookOnlyItemsInProgress.length) {
shelves.push({
id: 'read-again',
label: 'Read Again',
labelStringKey: 'LabelReadAgain',
type: 'book',
entities: readAgainPayload.items,
total: readAgainPayload.count
entities: ebookOnlyItemsInProgress,
total: mediaFinishedPayload.count
})
}
Logger.debug(`Loaded ${readAgainPayload.items.length} of ${readAgainPayload.count} items for "Read Again" in ${((Date.now() - start) / 1000).toFixed(2)}s`)
}
Logger.debug(`Loaded ${mediaFinishedPayload.items.length} of ${mediaFinishedPayload.count} items for "Listen/Read Again" in ${((Date.now() - start) / 1000).toFixed(2)}s`)

if (library.isBook) {
start = Date.now()
// "Newest Authors" shelf
const newestAuthorsPayload = await libraryFilters.getNewestAuthors(library, user, limit)
Expand Down
32 changes: 16 additions & 16 deletions server/utils/queries/libraryFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ module.exports = {
* @param {oldUser} user
* @param {string[]} include
* @param {number} limit
* @param {boolean} ebook true if continue reading shelf
* @returns {object} { items:LibraryItem[], count:number }
*/
async getMediaItemsInProgress(library, user, include, limit, ebook = false) {
async getMediaItemsInProgress(library, user, include, limit) {
if (library.mediaType === 'book') {
const filterValue = ebook ? 'ebook-in-progress' : 'audio-in-progress'
const { libraryItems, count } = await libraryItemsBookFilters.getFilteredLibraryItems(library.id, user, 'progress', filterValue, 'progress', true, false, include, limit, 0)
const { libraryItems, count } = await libraryItemsBookFilters.getFilteredLibraryItems(library.id, user, 'progress', 'in-progress', 'progress', true, false, include, limit, 0)
return {
items: libraryItems.map(li => {
const oldLibraryItem = Database.models.libraryItem.getOldLibraryItem(li).toJSONMinified()
Expand Down Expand Up @@ -81,7 +79,7 @@ module.exports = {
*/
async getLibraryItemsMostRecentlyAdded(library, user, include, limit) {
if (library.mediaType === 'book') {
const { libraryItems, count } = await libraryItemsBookFilters.getFilteredLibraryItems(library.id, user, null, null, 'addedAt', true, false, include, limit, 0)
const { libraryItems, count } = await libraryItemsBookFilters.getFilteredLibraryItems(library.id, user, 'recent', null, 'addedAt', true, false, include, limit, 0)
return {
libraryItems: libraryItems.map(li => {
const oldLibraryItem = Database.models.libraryItem.getOldLibraryItem(li).toJSONMinified()
Expand All @@ -96,7 +94,7 @@ module.exports = {
count
}
} else {
const { libraryItems, count } = await libraryItemsPodcastFilters.getFilteredLibraryItems(library.id, user, null, null, 'addedAt', true, include, limit, 0)
const { libraryItems, count } = await libraryItemsPodcastFilters.getFilteredLibraryItems(library.id, user, 'recent', null, 'addedAt', true, include, limit, 0)
return {
libraryItems: libraryItems.map(li => {
const oldLibraryItem = Database.models.libraryItem.getOldLibraryItem(li).toJSONMinified()
Expand Down Expand Up @@ -139,20 +137,16 @@ module.exports = {
},

/**
* Get library items or podcast episodes for the "Listen Again" or "Read Again" shelf
* Get library items or podcast episodes for the "Listen Again" and "Read Again" shelf
* @param {oldLibrary} library
* @param {oldUser} user
* @param {string[]} include
* @param {number} limit
* @param {boolean} ebook true if "Read Again" shelf
* @returns {object} { items:object[], count:number }
*/
async getMediaFinished(library, user, include, limit, ebook = false) {
if (ebook && library.mediaType !== 'book') return { items: [], count: 0 }

async getMediaFinished(library, user, include, limit) {
if (library.mediaType === 'book') {
const filterValue = ebook ? 'ebook-finished' : 'finished'
const { libraryItems, count } = await libraryItemsBookFilters.getFilteredLibraryItems(library.id, user, 'progress', filterValue, 'progress', true, false, include, limit, 0)
const { libraryItems, count } = await libraryItemsBookFilters.getFilteredLibraryItems(library.id, user, 'progress', 'finished', 'progress', true, false, include, limit, 0)
return {
items: libraryItems.map(li => {
const oldLibraryItem = Database.models.libraryItem.getOldLibraryItem(li).toJSONMinified()
Expand Down Expand Up @@ -198,7 +192,10 @@ module.exports = {

const seriesWhere = [
{
libraryId: library.id
libraryId: library.id,
createdAt: {
[Sequelize.Op.gte]: new Date(new Date() - (60 * 24 * 60 * 60 * 1000)) // 60 days ago
}
}
]
// Handle user permissions to only include series with at least 1 book
Expand Down Expand Up @@ -292,7 +289,10 @@ module.exports = {

const { rows: authors, count } = await Database.models.author.findAndCountAll({
where: {
libraryId: library.id
libraryId: library.id,
createdAt: {
[Sequelize.Op.gte]: new Date(new Date() - (60 * 24 * 60 * 60 * 1000)) // 60 days ago
}
},
include: {
model: Database.models.bookAuthor,
Expand Down Expand Up @@ -348,7 +348,7 @@ module.exports = {
async getNewestPodcastEpisodes(library, user, limit) {
if (library.mediaType !== 'podcast') return { libraryItems: [], count: 0 }

const { libraryItems, count } = await libraryItemsPodcastFilters.getFilteredPodcastEpisodes(library.id, user, null, null, 'createdAt', true, limit, 0)
const { libraryItems, count } = await libraryItemsPodcastFilters.getFilteredPodcastEpisodes(library.id, user, 'recent', null, 'createdAt', true, limit, 0)
return {
count,
libraryItems: libraryItems.map(li => {
Expand Down
4 changes: 4 additions & 0 deletions server/utils/queries/libraryItemsBookFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,10 @@ module.exports = {
},
required: false
})
} else if (filterGroup === 'recent') {
libraryItemWhere['createdAt'] = {
[Sequelize.Op.gte]: new Date(new Date() - (60 * 24 * 60 * 60 * 1000)) // 60 days ago
}
}

let { mediaWhere, replacements } = this.getMediaGroupQuery(filterGroup, filterValue)
Expand Down
15 changes: 12 additions & 3 deletions server/utils/queries/libraryItemsPodcastFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ module.exports = {
isInvalid: true
}
]
} else if (filterGroup === 'recent') {
libraryItemWhere['createdAt'] = {
[Sequelize.Op.gte]: new Date(new Date() - (60 * 24 * 60 * 60 * 1000)) // 60 days ago
}
}

const podcastIncludes = []
Expand Down Expand Up @@ -210,6 +214,9 @@ module.exports = {

const podcastEpisodeIncludes = []
let podcastEpisodeWhere = {}
let libraryItemWhere = {
libraryId
}
if (filterGroup === 'progress') {
podcastEpisodeIncludes.push({
model: Database.models.mediaProgress,
Expand All @@ -233,6 +240,10 @@ module.exports = {
} else if (filterValue === 'finished') {
podcastEpisodeWhere['$mediaProgresses.isFinished$'] = true
}
} else if (filterGroup === 'recent') {
libraryItemWhere['createdAt'] = {
[Sequelize.Op.gte]: new Date(new Date() - (60 * 24 * 60 * 60 * 1000)) // 60 days ago
}
}

const podcastEpisodeOrder = []
Expand All @@ -254,9 +265,7 @@ module.exports = {
include: [
{
model: Database.models.libraryItem,
where: {
libraryId
}
where: libraryItemWhere
}
]
},
Expand Down

0 comments on commit 33e04d0

Please sign in to comment.