mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-07-14 19:34:57 +02:00
Add:Notification settings, notification manager trigger #996
This commit is contained in:
parent
9a7503cde2
commit
ff04eb8d5e
9 changed files with 239 additions and 5 deletions
|
@ -1,10 +1,55 @@
|
|||
const axios = require('axios')
|
||||
const Logger = require("../Logger")
|
||||
|
||||
class NotificationManager {
|
||||
constructor() { }
|
||||
constructor(db) {
|
||||
this.db = db
|
||||
|
||||
onNewPodcastEpisode(libraryItem, episode) {
|
||||
Logger.debug(`[NotificationManager] onNewPodcastEpisode: Episode "${episode.title}" for podcast ${libraryItem.media.metadata.title}`)
|
||||
this.notificationFailedMap = {}
|
||||
}
|
||||
|
||||
onPodcastEpisodeDownloaded(libraryItem, episode) {
|
||||
if (!this.db.notificationSettings.isUseable) return
|
||||
|
||||
Logger.debug(`[NotificationManager] onPodcastEpisodeDownloaded: Episode "${episode.title}" for podcast ${libraryItem.media.metadata.title}`)
|
||||
this.triggerNotification('onPodcastEpisodeDownloaded', { libraryItem, episode })
|
||||
}
|
||||
|
||||
onTest() {
|
||||
this.triggerNotification('onTest')
|
||||
}
|
||||
|
||||
async triggerNotification(eventName, eventData) {
|
||||
if (!this.db.notificationSettings.isUseable) return
|
||||
|
||||
const notifications = this.db.notificationSettings.getNotificationsForEvent(eventName)
|
||||
for (const notification of notifications) {
|
||||
Logger.debug(`[NotificationManager] triggerNotification: Sending ${eventName} notification ${notification.id}`)
|
||||
const success = await this.sendNotification(notification, eventData)
|
||||
|
||||
if (!success) { // Failed notification
|
||||
if (!this.notificationFailedMap[notification.id]) this.notificationFailedMap[notification.id] = 1
|
||||
else this.notificationFailedMap[notification.id]++
|
||||
|
||||
if (this.notificationFailedMap[notification.id] > 2) {
|
||||
Logger.error(`[NotificationManager] triggerNotification: ${notification.eventName}/${notification.id} reached max failed attempts`)
|
||||
// TODO: Do something like disable the notification
|
||||
}
|
||||
} else { // Successful notification
|
||||
delete this.notificationFailedMap[notification.id]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sendNotification(notification, eventData) {
|
||||
const payload = notification.getApprisePayload(eventData)
|
||||
return axios.post(`${this.db.notificationSettings.appriseApiUrl}/notify`, payload, { timeout: 6000 }).then((data) => {
|
||||
Logger.debug(`[NotificationManager] sendNotification: ${notification.eventName}/${notification.id} response=${data}`)
|
||||
return true
|
||||
}).catch((error) => {
|
||||
Logger.error(`[NotificationManager] sendNotification: ${notification.eventName}/${notification.id} error=`, error)
|
||||
return false
|
||||
})
|
||||
}
|
||||
}
|
||||
module.exports = NotificationManager
|
Loading…
Add table
Add a link
Reference in a new issue