diff --git a/server/Server.js b/server/Server.js index 9c925459..5500a14b 100644 --- a/server/Server.js +++ b/server/Server.js @@ -23,6 +23,7 @@ const ApiRouter = require('./routers/ApiRouter') const HlsRouter = require('./routers/HlsRouter') const StaticRouter = require('./routers/StaticRouter') +const NotificationManager = require('./managers/NotificationManager') const CoverManager = require('./managers/CoverManager') const AbMergeManager = require('./managers/AbMergeManager') const CacheManager = require('./managers/CacheManager') @@ -64,13 +65,14 @@ class Server { this.auth = new Auth(this.db) // Managers + this.notificationManager = new NotificationManager(this.db) this.backupManager = new BackupManager(this.db, this.emitter.bind(this)) this.logManager = new LogManager(this.db) this.cacheManager = new CacheManager() this.abMergeManager = new AbMergeManager(this.db, this.clientEmitter.bind(this)) this.playbackSessionManager = new PlaybackSessionManager(this.db, this.emitter.bind(this), this.clientEmitter.bind(this)) this.coverManager = new CoverManager(this.db, this.cacheManager) - this.podcastManager = new PodcastManager(this.db, this.watcher, this.emitter.bind(this)) + this.podcastManager = new PodcastManager(this.db, this.watcher, this.emitter.bind(this), this.notificationManager) this.audioMetadataManager = new AudioMetadataMangaer(this.db, this.emitter.bind(this), this.clientEmitter.bind(this)) this.rssFeedManager = new RssFeedManager(this.db, this.emitter.bind(this)) diff --git a/server/managers/NotificationManager.js b/server/managers/NotificationManager.js new file mode 100644 index 00000000..9b71bfad --- /dev/null +++ b/server/managers/NotificationManager.js @@ -0,0 +1,10 @@ +const Logger = require("../Logger") + +class NotificationManager { + constructor() { } + + onNewPodcastEpisode(libraryItem, episode) { + Logger.debug(`[NotificationManager] onNewPodcastEpisode: Episode "${episode.title}" for podcast ${libraryItem.media.metadata.title}`) + } +} +module.exports = NotificationManager \ No newline at end of file diff --git a/server/managers/PodcastManager.js b/server/managers/PodcastManager.js index f9c1b819..73f35f19 100644 --- a/server/managers/PodcastManager.js +++ b/server/managers/PodcastManager.js @@ -13,10 +13,11 @@ const PodcastEpisode = require('../objects/entities/PodcastEpisode') const AudioFile = require('../objects/files/AudioFile') class PodcastManager { - constructor(db, watcher, emitter) { + constructor(db, watcher, emitter, notificationManager) { this.db = db this.watcher = watcher this.emitter = emitter + this.notificationManager = notificationManager this.downloadQueue = [] this.currentDownload = null @@ -122,8 +123,8 @@ class PodcastManager { } libraryItem.libraryFiles.push(libraryFile) - // Check setting maxEpisodesToKeep and remove episode if necessary - if (this.currentDownload.isAutoDownload) { // only applies for auto-downloaded episodes + if (this.currentDownload.isAutoDownload) { + // Check setting maxEpisodesToKeep and remove episode if necessary if (libraryItem.media.maxEpisodesToKeep && libraryItem.media.episodesWithPubDate.length > libraryItem.media.maxEpisodesToKeep) { Logger.info(`[PodcastManager] # of episodes (${libraryItem.media.episodesWithPubDate.length}) exceeds max episodes to keep (${libraryItem.media.maxEpisodesToKeep})`) await this.removeOldestEpisode(libraryItem, podcastEpisode.id) @@ -133,6 +134,11 @@ class PodcastManager { libraryItem.updatedAt = Date.now() await this.db.updateLibraryItem(libraryItem) this.emitter('item_updated', libraryItem.toJSONExpanded()) + + if (this.currentDownload.isAutoDownload) { // Notifications only for auto downloaded episodes + this.notificationManager.onNewPodcastEpisode(libraryItem, podcastEpisode) + } + return true } diff --git a/server/objects/Notification.js b/server/objects/Notification.js new file mode 100644 index 00000000..754e1997 --- /dev/null +++ b/server/objects/Notification.js @@ -0,0 +1,39 @@ +class Notification { + constructor(notification = null) { + this.id = null + this.eventName = '' + this.urls = [] + this.titleTemplate = '' + this.bodyTemplate = '' + this.enabled = false + + this.createdAt = null + + if (notification) { + this.construct(notification) + } + } + + construct(notification) { + this.id = notification.id + this.eventName = notification.eventName + this.urls = notification.urls || [] + this.titleTemplate = notification.titleTemplate || '' + this.bodyTemplate = notification.bodyTemplate || '' + this.enabled = !!notification.enabled + this.createdAt = notification.createdAt + } + + toJSON() { + return { + id: this.id, + eventName: this.eventName, + urls: this.urls, + titleTemplate: this.titleTemplate, + bodyTemplate: this.bodyTemplate, + enabled: this.enabled, + createdAt: this.createdAt + } + } +} +module.exports = Notification \ No newline at end of file