Update NotificationManager to singleton

This commit is contained in:
advplyr 2024-09-27 17:33:23 -05:00
parent 567a9a4e58
commit 7cd8d7f44d
6 changed files with 16 additions and 20 deletions

View File

@ -23,7 +23,6 @@ const HlsRouter = require('./routers/HlsRouter')
const PublicRouter = require('./routers/PublicRouter') const PublicRouter = require('./routers/PublicRouter')
const LogManager = require('./managers/LogManager') const LogManager = require('./managers/LogManager')
const NotificationManager = require('./managers/NotificationManager')
const EmailManager = require('./managers/EmailManager') const EmailManager = require('./managers/EmailManager')
const AbMergeManager = require('./managers/AbMergeManager') const AbMergeManager = require('./managers/AbMergeManager')
const CacheManager = require('./managers/CacheManager') const CacheManager = require('./managers/CacheManager')
@ -67,12 +66,11 @@ class Server {
this.auth = new Auth() this.auth = new Auth()
// Managers // Managers
this.notificationManager = new NotificationManager()
this.emailManager = new EmailManager() this.emailManager = new EmailManager()
this.backupManager = new BackupManager(this.notificationManager) this.backupManager = new BackupManager()
this.abMergeManager = new AbMergeManager() this.abMergeManager = new AbMergeManager()
this.playbackSessionManager = new PlaybackSessionManager() this.playbackSessionManager = new PlaybackSessionManager()
this.podcastManager = new PodcastManager(this.watcher, this.notificationManager) this.podcastManager = new PodcastManager(this.watcher)
this.audioMetadataManager = new AudioMetadataMangaer() this.audioMetadataManager = new AudioMetadataMangaer()
this.rssFeedManager = new RssFeedManager() this.rssFeedManager = new RssFeedManager()
this.cronManager = new CronManager(this.podcastManager, this.playbackSessionManager) this.cronManager = new CronManager(this.podcastManager, this.playbackSessionManager)

View File

@ -1,6 +1,7 @@
const { Request, Response, NextFunction } = require('express') const { Request, Response, NextFunction } = require('express')
const Database = require('../Database') const Database = require('../Database')
const { version } = require('../../package.json') const { version } = require('../../package.json')
const NotificationManager = require('../managers/NotificationManager')
/** /**
* @typedef RequestUserObject * @typedef RequestUserObject
@ -23,7 +24,7 @@ class NotificationController {
*/ */
get(req, res) { get(req, res) {
res.json({ res.json({
data: this.notificationManager.getData(), data: NotificationManager.getData(),
settings: Database.notificationSettings settings: Database.notificationSettings
}) })
} }
@ -52,7 +53,7 @@ class NotificationController {
* @param {Response} res * @param {Response} res
*/ */
getData(req, res) { getData(req, res) {
res.json(this.notificationManager.getData()) res.json(NotificationManager.getData())
} }
/** /**
@ -64,7 +65,7 @@ class NotificationController {
* @param {Response} res * @param {Response} res
*/ */
async fireTestEvent(req, res) { async fireTestEvent(req, res) {
await this.notificationManager.triggerNotification('onTest', { version: `v${version}` }, req.query.fail === '1') await NotificationManager.triggerNotification('onTest', { version: `v${version}` }, req.query.fail === '1')
res.sendStatus(200) res.sendStatus(200)
} }
@ -121,7 +122,7 @@ class NotificationController {
async sendNotificationTest(req, res) { async sendNotificationTest(req, res) {
if (!Database.notificationSettings.isUseable) return res.status(400).send('Apprise is not configured') if (!Database.notificationSettings.isUseable) return res.status(400).send('Apprise is not configured')
const success = await this.notificationManager.sendTestNotification(req.notification) const success = await NotificationManager.sendTestNotification(req.notification)
if (success) res.sendStatus(200) if (success) res.sendStatus(200)
else res.sendStatus(500) else res.sendStatus(500)
} }

View File

@ -15,13 +15,12 @@ const { getFileSize } = require('../utils/fileUtils')
const Backup = require('../objects/Backup') const Backup = require('../objects/Backup')
const CacheManager = require('./CacheManager') const CacheManager = require('./CacheManager')
const NotificationManager = require('./NotificationManager')
class BackupManager { class BackupManager {
constructor(notificationManager) { constructor() {
this.ItemsMetadataPath = Path.join(global.MetadataPath, 'items') this.ItemsMetadataPath = Path.join(global.MetadataPath, 'items')
this.AuthorsMetadataPath = Path.join(global.MetadataPath, 'authors') this.AuthorsMetadataPath = Path.join(global.MetadataPath, 'authors')
/** @type {import('./NotificationManager')} */
this.notificationManager = notificationManager
this.scheduleTask = null this.scheduleTask = null
@ -301,7 +300,7 @@ class BackupManager {
const sqliteBackupPath = await this.backupSqliteDb(newBackup).catch((error) => { const sqliteBackupPath = await this.backupSqliteDb(newBackup).catch((error) => {
Logger.error(`[BackupManager] Failed to backup sqlite db`, error) Logger.error(`[BackupManager] Failed to backup sqlite db`, error)
const errorMsg = error?.message || error || 'Unknown Error' const errorMsg = error?.message || error || 'Unknown Error'
this.notificationManager.onBackupFailed(errorMsg) NotificationManager.onBackupFailed(errorMsg)
return false return false
}) })
@ -313,7 +312,7 @@ class BackupManager {
const zipResult = await this.zipBackup(sqliteBackupPath, newBackup).catch((error) => { const zipResult = await this.zipBackup(sqliteBackupPath, newBackup).catch((error) => {
Logger.error(`[BackupManager] Backup Failed ${error}`) Logger.error(`[BackupManager] Backup Failed ${error}`)
const errorMsg = error?.message || error || 'Unknown Error' const errorMsg = error?.message || error || 'Unknown Error'
this.notificationManager.onBackupFailed(errorMsg) NotificationManager.onBackupFailed(errorMsg)
return false return false
}) })
@ -344,7 +343,7 @@ class BackupManager {
} }
// Notification for backup successfully completed // Notification for backup successfully completed
this.notificationManager.onBackupCompleted(newBackup, this.backups.length, removeOldest) NotificationManager.onBackupCompleted(newBackup, this.backups.length, removeOldest)
return true return true
} }

View File

@ -180,4 +180,4 @@ class NotificationManager {
}) })
} }
} }
module.exports = NotificationManager module.exports = new NotificationManager()

View File

@ -14,6 +14,7 @@ const ffmpegHelpers = require('../utils/ffmpegHelpers')
const TaskManager = require('./TaskManager') const TaskManager = require('./TaskManager')
const CoverManager = require('../managers/CoverManager') const CoverManager = require('../managers/CoverManager')
const NotificationManager = require('../managers/NotificationManager')
const LibraryFile = require('../objects/files/LibraryFile') const LibraryFile = require('../objects/files/LibraryFile')
const PodcastEpisodeDownload = require('../objects/PodcastEpisodeDownload') const PodcastEpisodeDownload = require('../objects/PodcastEpisodeDownload')
@ -22,9 +23,8 @@ const AudioFile = require('../objects/files/AudioFile')
const LibraryItem = require('../objects/LibraryItem') const LibraryItem = require('../objects/LibraryItem')
class PodcastManager { class PodcastManager {
constructor(watcher, notificationManager) { constructor(watcher) {
this.watcher = watcher this.watcher = watcher
this.notificationManager = notificationManager
this.downloadQueue = [] this.downloadQueue = []
this.currentDownload = null this.currentDownload = null
@ -203,7 +203,7 @@ class PodcastManager {
if (this.currentDownload.isAutoDownload) { if (this.currentDownload.isAutoDownload) {
// Notifications only for auto downloaded episodes // Notifications only for auto downloaded episodes
this.notificationManager.onPodcastEpisodeDownloaded(libraryItem, podcastEpisode) NotificationManager.onPodcastEpisodeDownloaded(libraryItem, podcastEpisode)
} }
return true return true

View File

@ -55,8 +55,6 @@ class ApiRouter {
this.rssFeedManager = Server.rssFeedManager this.rssFeedManager = Server.rssFeedManager
/** @type {import('../managers/CronManager')} */ /** @type {import('../managers/CronManager')} */
this.cronManager = Server.cronManager this.cronManager = Server.cronManager
/** @type {import('../managers/NotificationManager')} */
this.notificationManager = Server.notificationManager
/** @type {import('../managers/EmailManager')} */ /** @type {import('../managers/EmailManager')} */
this.emailManager = Server.emailManager this.emailManager = Server.emailManager
this.apiCacheManager = Server.apiCacheManager this.apiCacheManager = Server.apiCacheManager