2022-05-02 21:41:59 +02:00
|
|
|
const Path = require('path')
|
2022-11-24 22:53:58 +01:00
|
|
|
|
|
|
|
const Logger = require('../Logger')
|
|
|
|
const SocketAuthority = require('../SocketAuthority')
|
|
|
|
|
2022-07-06 02:53:01 +02:00
|
|
|
const fs = require('../libs/fsExtra')
|
2022-06-08 01:29:43 +02:00
|
|
|
const Feed = require('../objects/Feed')
|
2022-05-02 21:41:59 +02:00
|
|
|
|
|
|
|
class RssFeedManager {
|
2022-11-24 22:53:58 +01:00
|
|
|
constructor(db) {
|
2022-05-02 21:41:59 +02:00
|
|
|
this.db = db
|
2022-11-24 22:53:58 +01:00
|
|
|
|
2022-05-02 21:41:59 +02:00
|
|
|
this.feeds = {}
|
|
|
|
}
|
|
|
|
|
2022-08-06 02:23:18 +02:00
|
|
|
get feedsArray() {
|
|
|
|
return Object.values(this.feeds)
|
|
|
|
}
|
|
|
|
|
2022-06-08 01:29:43 +02:00
|
|
|
async init() {
|
2022-12-26 23:58:36 +01:00
|
|
|
const feedObjects = await this.db.getAllEntities('feed')
|
2022-06-08 01:29:43 +02:00
|
|
|
if (feedObjects && feedObjects.length) {
|
|
|
|
feedObjects.forEach((feedObj) => {
|
2022-12-26 23:58:36 +01:00
|
|
|
const feed = new Feed(feedObj)
|
2022-06-08 01:29:43 +02:00
|
|
|
this.feeds[feed.id] = feed
|
|
|
|
Logger.info(`[RssFeedManager] Opened rss feed ${feed.feedUrl}`)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-28 01:03:31 +01:00
|
|
|
findFeedForEntityId(entityId) {
|
|
|
|
return Object.values(this.feeds).find(feed => feed.entityId === entityId)
|
2022-05-02 23:42:30 +02:00
|
|
|
}
|
|
|
|
|
2022-12-26 23:58:36 +01:00
|
|
|
findFeed(feedId) {
|
|
|
|
return this.feeds[feedId] || null
|
|
|
|
}
|
|
|
|
|
2022-08-28 22:41:51 +02:00
|
|
|
async getFeed(req, res) {
|
2022-12-26 23:58:36 +01:00
|
|
|
const feed = this.feeds[req.params.id]
|
2022-06-08 01:29:43 +02:00
|
|
|
if (!feed) {
|
2022-11-21 01:54:25 +01:00
|
|
|
Logger.debug(`[RssFeedManager] Feed not found ${req.params.id}`)
|
2022-05-02 21:41:59 +02:00
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
2022-06-08 02:25:14 +02:00
|
|
|
|
2022-08-28 22:41:51 +02:00
|
|
|
if (feed.entityType === 'item') {
|
|
|
|
const libraryItem = this.db.getLibraryItem(feed.entityId)
|
|
|
|
if (libraryItem && (!feed.entityUpdatedAt || libraryItem.updatedAt > feed.entityUpdatedAt)) {
|
|
|
|
Logger.debug(`[RssFeedManager] Updating RSS feed for item ${libraryItem.id} "${libraryItem.media.metadata.title}"`)
|
|
|
|
feed.updateFromItem(libraryItem)
|
|
|
|
await this.db.updateEntity('feed', feed)
|
|
|
|
}
|
2022-12-27 00:48:39 +01:00
|
|
|
} else if (feed.entityType === 'collection') {
|
|
|
|
// TODO: Also trigger an update if any item in the collection was updated
|
|
|
|
const collection = this.db.collections.find(c => c.id === feed.entityId)
|
|
|
|
if (collection) {
|
|
|
|
const collectionExpanded = collection.toJSONExpanded(this.db.libraryItems)
|
|
|
|
if (!feed.entityUpdatedAt || collection.lastUpdate > feed.entityUpdatedAt) {
|
|
|
|
Logger.debug(`[RssFeedManager] Updating RSS feed for collection "${collection.name}"`)
|
|
|
|
|
|
|
|
feed.updateFromCollection(collectionExpanded)
|
|
|
|
await this.db.updateEntity('feed', feed)
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 22:41:51 +02:00
|
|
|
}
|
|
|
|
|
2022-12-26 23:58:36 +01:00
|
|
|
const xml = feed.buildXml()
|
2022-05-02 21:41:59 +02:00
|
|
|
res.set('Content-Type', 'text/xml')
|
|
|
|
res.send(xml)
|
|
|
|
}
|
|
|
|
|
|
|
|
getFeedItem(req, res) {
|
2022-12-26 23:58:36 +01:00
|
|
|
const feed = this.feeds[req.params.id]
|
2022-06-08 01:29:43 +02:00
|
|
|
if (!feed) {
|
2022-11-21 13:39:32 +01:00
|
|
|
Logger.debug(`[RssFeedManager] Feed not found ${req.params.id}`)
|
2022-05-02 21:41:59 +02:00
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
2022-12-26 23:58:36 +01:00
|
|
|
const episodePath = feed.getEpisodePath(req.params.episodeId)
|
2022-06-08 01:29:43 +02:00
|
|
|
if (!episodePath) {
|
|
|
|
Logger.error(`[RssFeedManager] Feed episode not found ${req.params.episodeId}`)
|
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res.sendFile(episodePath)
|
2022-05-02 21:41:59 +02:00
|
|
|
}
|
|
|
|
|
2022-05-02 23:42:30 +02:00
|
|
|
getFeedCover(req, res) {
|
2022-12-26 23:58:36 +01:00
|
|
|
const feed = this.feeds[req.params.id]
|
2022-06-08 01:29:43 +02:00
|
|
|
if (!feed) {
|
2022-11-21 13:39:32 +01:00
|
|
|
Logger.debug(`[RssFeedManager] Feed not found ${req.params.id}`)
|
2022-05-02 23:42:30 +02:00
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-08 01:29:43 +02:00
|
|
|
if (!feed.coverPath) {
|
2022-05-02 23:42:30 +02:00
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-19 01:39:51 +02:00
|
|
|
const extname = Path.extname(feed.coverPath).toLowerCase().slice(1)
|
2022-05-02 23:42:30 +02:00
|
|
|
res.type(`image/${extname}`)
|
2022-12-26 23:58:36 +01:00
|
|
|
const readStream = fs.createReadStream(feed.coverPath)
|
2022-05-02 23:42:30 +02:00
|
|
|
readStream.pipe(res)
|
|
|
|
}
|
|
|
|
|
2022-06-08 01:29:43 +02:00
|
|
|
async openFeedForItem(user, libraryItem, options) {
|
2022-05-02 21:41:59 +02:00
|
|
|
const serverAddress = options.serverAddress
|
2022-05-04 01:52:34 +02:00
|
|
|
const slug = options.slug
|
|
|
|
|
2022-06-08 01:29:43 +02:00
|
|
|
const feed = new Feed()
|
|
|
|
feed.setFromItem(user.id, slug, libraryItem, serverAddress)
|
|
|
|
this.feeds[feed.id] = feed
|
|
|
|
|
2022-12-26 23:58:36 +01:00
|
|
|
Logger.debug(`[RssFeedManager] Opened RSS feed "${feed.feedUrl}"`)
|
2022-06-08 01:29:43 +02:00
|
|
|
await this.db.insertEntity('feed', feed)
|
2022-12-22 23:26:11 +01:00
|
|
|
SocketAuthority.emitter('rss_feed_open', feed.toJSONMinified())
|
2022-06-08 01:29:43 +02:00
|
|
|
return feed
|
2022-05-02 21:41:59 +02:00
|
|
|
}
|
2022-05-02 23:42:30 +02:00
|
|
|
|
2022-12-27 00:48:39 +01:00
|
|
|
async openFeedForCollection(user, collectionExpanded, options) {
|
|
|
|
const serverAddress = options.serverAddress
|
|
|
|
const slug = options.slug
|
|
|
|
|
|
|
|
const feed = new Feed()
|
|
|
|
feed.setFromCollection(user.id, slug, collectionExpanded, serverAddress)
|
|
|
|
this.feeds[feed.id] = feed
|
|
|
|
|
|
|
|
Logger.debug(`[RssFeedManager] Opened RSS feed "${feed.feedUrl}"`)
|
|
|
|
await this.db.insertEntity('feed', feed)
|
|
|
|
SocketAuthority.emitter('rss_feed_open', feed.toJSONMinified())
|
|
|
|
return feed
|
2022-05-02 23:42:30 +02:00
|
|
|
}
|
|
|
|
|
2022-06-08 01:29:43 +02:00
|
|
|
async closeRssFeed(id) {
|
2022-05-02 23:42:30 +02:00
|
|
|
if (!this.feeds[id]) return
|
2022-12-26 23:58:36 +01:00
|
|
|
const feed = this.feeds[id]
|
2022-06-08 01:29:43 +02:00
|
|
|
await this.db.removeEntity('feed', id)
|
2022-12-22 23:26:11 +01:00
|
|
|
SocketAuthority.emitter('rss_feed_closed', feed.toJSONMinified())
|
2022-05-02 23:42:30 +02:00
|
|
|
delete this.feeds[id]
|
2022-06-08 01:29:43 +02:00
|
|
|
Logger.info(`[RssFeedManager] Closed RSS feed "${feed.feedUrl}"`)
|
2022-05-02 23:42:30 +02:00
|
|
|
}
|
2022-05-02 21:41:59 +02:00
|
|
|
}
|
|
|
|
module.exports = RssFeedManager
|