diff --git a/server/models/LibraryItem.js b/server/models/LibraryItem.js index e6525f45..e031c177 100644 --- a/server/models/LibraryItem.js +++ b/server/models/LibraryItem.js @@ -498,6 +498,18 @@ module.exports = (sequelize) => { }) } + const seriesMostRecentPayload = await libraryFilters.getSeriesMostRecentlyAdded(library, include, 5) + if (seriesMostRecentPayload.series.length) { + shelves.push({ + id: 'recent-series', + label: 'Recent Series', + labelStringKey: 'LabelRecentSeries', + type: 'series', + entities: seriesMostRecentPayload.series, + total: seriesMostRecentPayload.count + }) + } + return shelves } diff --git a/server/utils/queries/libraryFilters.js b/server/utils/queries/libraryFilters.js index 5a3db24b..9712e55e 100644 --- a/server/utils/queries/libraryFilters.js +++ b/server/utils/queries/libraryFilters.js @@ -1,4 +1,5 @@ const Database = require('../../Database') +const Logger = require('../../Logger') const libraryItemsBookFilters = require('./libraryItemsBookFilters') const libraryItemsPodcastFilters = require('./libraryItemsPodcastFilters') @@ -129,5 +130,80 @@ module.exports = { }), count } + }, + + /** + * Get series for recent series shelf + * @param {oldLibrary} library + * @param {string[]} include + * @param {number} limit + * @returns {object} { series:oldSeries[], count:number} + */ + async getSeriesMostRecentlyAdded(library, include, limit) { + const seriesIncludes = [] + if (include.includes('rssfeed')) { + seriesIncludes.push({ + model: Database.models.feed + }) + } + const { rows: series, count } = await Database.models.series.findAndCountAll({ + where: { + libraryId: library.id + }, + limit, + offset: 0, + distinct: true, + subQuery: false, + include: [ + { + model: Database.models.bookSeries, + include: { + model: Database.models.book, + include: { + model: Database.models.libraryItem + } + }, + separate: true + }, + ...seriesIncludes + ], + order: [ + ['createdAt', 'DESC'] + ] + }) + + Logger.debug(`Found ${series.length} series recently added (${count} total)`) + + const allOldSeries = [] + for (const s of series) { + const oldSeries = s.getOldSeries().toJSON() + + if (s.feeds?.length) { + oldSeries.rssFeed = Database.models.feed.getOldFeed(s.feeds[0]).toJSONMinified() + } + + // TODO: Sort books by sequence in query + s.bookSeries.sort((a, b) => { + if (!a.sequence) return 1 + if (!b.sequence) return -1 + return a.sequence.localeCompare(b.sequence, undefined, { + numeric: true, + sensitivity: 'base' + }) + }) + oldSeries.books = s.bookSeries.map(bs => { + const libraryItem = bs.book.libraryItem.toJSON() + delete bs.book.libraryItem + libraryItem.media = bs.book + const oldLibraryItem = Database.models.libraryItem.getOldLibraryItem(libraryItem).toJSONMinified() + return oldLibraryItem + }) + allOldSeries.push(oldSeries) + } + + return { + series: allOldSeries, + count + } } } \ No newline at end of file