mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-08-31 07:09:55 +02:00
Refactor Feed model to create new feed for collection
This commit is contained in:
parent
ca2327aba3
commit
d576625cb7
7 changed files with 171 additions and 93 deletions
|
@ -279,8 +279,8 @@ class Feed extends Model {
|
|||
/**
|
||||
*
|
||||
* @param {string} userId
|
||||
* @param {string} slug
|
||||
* @param {import('./LibraryItem').LibraryItemExpanded} libraryItem
|
||||
* @param {string} slug
|
||||
* @param {string} serverAddress
|
||||
* @param {FeedOptions} feedOptions
|
||||
*
|
||||
|
@ -334,6 +334,72 @@ class Feed extends Model {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} userId
|
||||
* @param {import('./Collection')} collectionExpanded
|
||||
* @param {string} slug
|
||||
* @param {string} serverAddress
|
||||
* @param {FeedOptions} feedOptions
|
||||
*
|
||||
* @returns {Promise<FeedExpanded>}
|
||||
*/
|
||||
static async createFeedForCollection(userId, collectionExpanded, slug, serverAddress, feedOptions) {
|
||||
const booksWithTracks = collectionExpanded.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: 'collection',
|
||||
entityId: collectionExpanded.id,
|
||||
entityUpdatedAt: libraryItemMostRecentlyUpdatedAt,
|
||||
serverAddress,
|
||||
feedURL: `/feed/${slug}`,
|
||||
imageURL: firstBookWithCover?.coverPath ? `/feed/${slug}/cover${Path.extname(firstBookWithCover.coverPath)}` : `/Logo.png`,
|
||||
siteURL: `/collection/${collectionExpanded.id}`,
|
||||
title: collectionExpanded.name,
|
||||
description: collectionExpanded.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.createFromCollectionBooks(collectionExpanded, 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
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize model
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue