mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-08-04 02:05:06 +02:00
Refactor Feed model to create new feed for series
This commit is contained in:
parent
d576625cb7
commit
e50bd93958
6 changed files with 130 additions and 91 deletions
|
@ -388,7 +388,73 @@ class Feed extends Model {
|
|||
const transaction = await this.sequelize.transaction()
|
||||
try {
|
||||
const feed = await this.create(feedObj, { transaction })
|
||||
feed.feedEpisodes = await feedEpisodeModel.createFromCollectionBooks(collectionExpanded, feed, slug, transaction)
|
||||
feed.feedEpisodes = await feedEpisodeModel.createFromBooks(booksWithTracks, feed, slug, transaction)
|
||||
|
||||
await transaction.commit()
|
||||
|
||||
return feed
|
||||
} catch (error) {
|
||||
Logger.error(`[Feed] Error creating feed for collection ${collectionExpanded.id}`, error)
|
||||
await transaction.rollback()
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} userId
|
||||
* @param {import('./Series')} seriesExpanded
|
||||
* @param {string} slug
|
||||
* @param {string} serverAddress
|
||||
* @param {FeedOptions} feedOptions
|
||||
*
|
||||
* @returns {Promise<FeedExpanded>}
|
||||
*/
|
||||
static async createFeedForSeries(userId, seriesExpanded, slug, serverAddress, feedOptions) {
|
||||
const booksWithTracks = seriesExpanded.books.filter((book) => book.includedAudioFiles.length)
|
||||
const libraryItemMostRecentlyUpdatedAt = booksWithTracks.reduce((mostRecent, book) => {
|
||||
return book.libraryItem.updatedAt > mostRecent.libraryItem.updatedAt ? book : mostRecent
|
||||
}).libraryItem.updatedAt
|
||||
|
||||
const firstBookWithCover = booksWithTracks.find((book) => book.coverPath)
|
||||
|
||||
const allBookAuthorNames = booksWithTracks.reduce((authorNames, book) => {
|
||||
const bookAuthorsToAdd = book.authors.filter((author) => !authorNames.includes(author.name)).map((author) => author.name)
|
||||
return authorNames.concat(bookAuthorsToAdd)
|
||||
}, [])
|
||||
let author = allBookAuthorNames.slice(0, 3).join(', ')
|
||||
if (allBookAuthorNames.length > 3) {
|
||||
author += ' & more'
|
||||
}
|
||||
|
||||
const feedObj = {
|
||||
slug,
|
||||
entityType: 'series',
|
||||
entityId: seriesExpanded.id,
|
||||
entityUpdatedAt: libraryItemMostRecentlyUpdatedAt,
|
||||
serverAddress,
|
||||
feedURL: `/feed/${slug}`,
|
||||
imageURL: firstBookWithCover?.coverPath ? `/feed/${slug}/cover${Path.extname(firstBookWithCover.coverPath)}` : `/Logo.png`,
|
||||
siteURL: `/library/${booksWithTracks[0].libraryItem.libraryId}/series/${seriesExpanded.id}`,
|
||||
title: seriesExpanded.name,
|
||||
description: seriesExpanded.description || '',
|
||||
author,
|
||||
podcastType: 'serial',
|
||||
preventIndexing: feedOptions.preventIndexing,
|
||||
ownerName: feedOptions.ownerName,
|
||||
ownerEmail: feedOptions.ownerEmail,
|
||||
explicit: booksWithTracks.some((book) => book.explicit), // If any book is explicit, the feed is explicit
|
||||
coverPath: firstBookWithCover?.coverPath || null,
|
||||
userId
|
||||
}
|
||||
|
||||
/** @type {typeof import('./FeedEpisode')} */
|
||||
const feedEpisodeModel = this.sequelize.models.feedEpisode
|
||||
|
||||
const transaction = await this.sequelize.transaction()
|
||||
try {
|
||||
const feed = await this.create(feedObj, { transaction })
|
||||
feed.feedEpisodes = await feedEpisodeModel.createFromBooks(booksWithTracks, feed, slug, transaction)
|
||||
|
||||
await transaction.commit()
|
||||
|
||||
|
|
|
@ -216,21 +216,19 @@ class FeedEpisode extends Model {
|
|||
|
||||
/**
|
||||
*
|
||||
* @param {import('./Collection')} collectionExpanded
|
||||
* @param {import('./Book')[]} books
|
||||
* @param {import('./Feed')} feed
|
||||
* @param {string} slug
|
||||
* @param {import('sequelize').Transaction} transaction
|
||||
* @returns {Promise<FeedEpisode[]>}
|
||||
*/
|
||||
static async createFromCollectionBooks(collectionExpanded, feed, slug, transaction) {
|
||||
const booksWithTracks = collectionExpanded.books.filter((book) => book.includedAudioFiles.length)
|
||||
|
||||
const earliestLibraryItemCreatedAt = collectionExpanded.books.reduce((earliest, book) => {
|
||||
static async createFromBooks(books, feed, slug, transaction) {
|
||||
const earliestLibraryItemCreatedAt = books.reduce((earliest, book) => {
|
||||
return book.libraryItem.createdAt < earliest.libraryItem.createdAt ? book : earliest
|
||||
}).libraryItem.createdAt
|
||||
|
||||
const feedEpisodeObjs = []
|
||||
for (const book of booksWithTracks) {
|
||||
for (const book of books) {
|
||||
const useChapterTitles = this.checkUseChapterTitlesForEpisodes(book)
|
||||
for (const track of book.trackList) {
|
||||
feedEpisodeObjs.push(this.getFeedEpisodeObjFromAudiobookTrack(book, earliestLibraryItemCreatedAt, feed, slug, track, useChapterTitles))
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
const { DataTypes, Model, where, fn, col } = require('sequelize')
|
||||
const { DataTypes, Model, where, fn, col, literal } = require('sequelize')
|
||||
|
||||
const { getTitlePrefixAtEnd } = require('../utils/index')
|
||||
|
||||
|
@ -20,6 +20,11 @@ class Series extends Model {
|
|||
this.createdAt
|
||||
/** @type {Date} */
|
||||
this.updatedAt
|
||||
|
||||
// Expanded properties
|
||||
|
||||
/** @type {import('./Book').BookExpandedWithLibraryItem[]} - only set when expanded */
|
||||
this.books
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -103,6 +108,35 @@ class Series extends Model {
|
|||
Series.belongsTo(library)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all books in collection expanded with library item
|
||||
*
|
||||
* @returns {Promise<import('./Book').BookExpandedWithLibraryItem[]>}
|
||||
*/
|
||||
getBooksExpandedWithLibraryItem() {
|
||||
return this.getBooks({
|
||||
joinTableAttributes: ['sequence'],
|
||||
include: [
|
||||
{
|
||||
model: this.sequelize.models.libraryItem
|
||||
},
|
||||
{
|
||||
model: this.sequelize.models.author,
|
||||
through: {
|
||||
attributes: []
|
||||
}
|
||||
},
|
||||
{
|
||||
model: this.sequelize.models.series,
|
||||
through: {
|
||||
attributes: ['sequence']
|
||||
}
|
||||
}
|
||||
],
|
||||
order: [[literal('CAST(`bookSeries.sequence` AS FLOAT) ASC NULLS LAST')]]
|
||||
})
|
||||
}
|
||||
|
||||
toOldJSON() {
|
||||
return {
|
||||
id: this.id,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue