mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-01-17 00:08:55 +01:00
Update:Notifications onTest for testing and parse title/body template #996
This commit is contained in:
parent
8e8046541e
commit
0ef2a2e4b6
@ -1,17 +1,22 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="w-full border border-white border-opacity-10 rounded-xl p-4" :class="notification.enabled ? 'bg-primary bg-opacity-25' : 'bg-error bg-opacity-5'">
|
<div class="w-full border border-white border-opacity-10 rounded-xl p-4 my-2" :class="notification.enabled ? 'bg-primary bg-opacity-25' : 'bg-error bg-opacity-5'">
|
||||||
<div class="flex items-center">
|
<div class="flex items-center">
|
||||||
<p class="text-lg font-semibold">{{ notification.eventName }}</p>
|
<p class="text-lg font-semibold">{{ eventName }}</p>
|
||||||
<div class="flex-grow" />
|
<div class="flex-grow" />
|
||||||
|
|
||||||
<ui-btn v-if="notification.enabled" :loading="sendingTest" small class="mr-2" @click.stop="sendTestClick">Test</ui-btn>
|
<ui-btn v-if="eventName === 'onTest' && notification.enabled" :loading="testing" small class="mr-2" @click.stop="fireTestEventAndSucceed">Fire onTest Event</ui-btn>
|
||||||
|
<ui-btn v-if="eventName === 'onTest' && notification.enabled" :loading="testing" small class="mr-2" color="red-600" @click.stop="fireTestEventAndFail">Fire & Fail</ui-btn>
|
||||||
|
<ui-btn v-else-if="notification.enabled" :loading="sendingTest" small class="mr-2" @click.stop="sendTestClick">Test</ui-btn>
|
||||||
<ui-btn v-else :loading="enabling" small color="success" class="mr-2" @click="enableNotification">Enable</ui-btn>
|
<ui-btn v-else :loading="enabling" small color="success" class="mr-2" @click="enableNotification">Enable</ui-btn>
|
||||||
|
|
||||||
<ui-icon-btn bg-color="warning" :size="7" icon-font-size="1.2rem" icon="edit" class="mr-2" @click="editNotification" />
|
<ui-icon-btn bg-color="warning" :size="7" icon-font-size="1.2rem" icon="edit" class="mr-2" @click="editNotification" />
|
||||||
<ui-icon-btn bg-color="error" :size="7" icon-font-size="1.2rem" icon="delete" @click="deleteNotificationClick" />
|
<ui-icon-btn bg-color="error" :size="7" icon-font-size="1.2rem" icon="delete" @click="deleteNotificationClick" />
|
||||||
</div>
|
</div>
|
||||||
<div class="pt-4">
|
<div class="pt-4">
|
||||||
<p class="text-gray-300 text-sm">{{ notification.urls.join(', ') }}</p>
|
<p class="text-gray-300 text-sm mb-2">{{ notification.urls.join(', ') }}</p>
|
||||||
|
|
||||||
|
<p v-if="lastFiredAt && lastAttemptFailed" class="text-red-300 text-xs">Last attempt failed {{ $dateDistanceFromNow(lastFiredAt) }} ({{ numConsecutiveFailedAttempts }} attempt{{ numConsecutiveFailedAttempts === 1 ? '' : 's' }})</p>
|
||||||
|
<p v-else-if="lastFiredAt" class="text-gray-400 text-xs">Last fired {{ $dateDistanceFromNow(lastFiredAt) }}</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@ -28,14 +33,50 @@ export default {
|
|||||||
return {
|
return {
|
||||||
sendingTest: false,
|
sendingTest: false,
|
||||||
enabling: false,
|
enabling: false,
|
||||||
deleting: false
|
deleting: false,
|
||||||
|
testing: false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
computed: {
|
||||||
|
eventName() {
|
||||||
|
return this.notification ? this.notification.eventName : null
|
||||||
|
},
|
||||||
|
lastFiredAt() {
|
||||||
|
return this.notification ? this.notification.lastFiredAt : null
|
||||||
|
},
|
||||||
|
lastAttemptFailed() {
|
||||||
|
return this.notification ? this.notification.lastAttemptFailed : null
|
||||||
|
},
|
||||||
|
numConsecutiveFailedAttempts() {
|
||||||
|
return this.notification ? this.notification.numConsecutiveFailedAttempts : null
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: {},
|
|
||||||
methods: {
|
methods: {
|
||||||
|
fireTestEventAndFail() {
|
||||||
|
this.fireTestEvent(true)
|
||||||
|
},
|
||||||
|
fireTestEventAndSucceed() {
|
||||||
|
this.fireTestEvent(false)
|
||||||
|
},
|
||||||
|
fireTestEvent(intentionallyFail = false) {
|
||||||
|
this.testing = true
|
||||||
|
this.$axios
|
||||||
|
.$get(`/api/notifications/test?fail=${intentionallyFail ? 1 : 0}`)
|
||||||
|
.then(() => {
|
||||||
|
this.$toast.success('Triggered onTest Event')
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error('Failed', error)
|
||||||
|
const errorMsg = error.response ? error.response.data : null
|
||||||
|
this.$toast.error(`Failed: ${errorMsg}` || 'Failed to trigger onTest event')
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
this.testing = false
|
||||||
|
})
|
||||||
|
},
|
||||||
sendTestClick() {
|
sendTestClick() {
|
||||||
const payload = {
|
const payload = {
|
||||||
message: `Send a test notification to event ${this.notification.eventName}?`,
|
message: `Send a test notification to event ${this.eventName}?`,
|
||||||
callback: (confirmed) => {
|
callback: (confirmed) => {
|
||||||
if (confirmed) {
|
if (confirmed) {
|
||||||
this.sendTest()
|
this.sendTest()
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<form @submit.prevent="submitForm">
|
<form @submit.prevent="submitForm">
|
||||||
<div class="px-4 w-full text-sm py-6 rounded-lg bg-bg shadow-lg border border-black-300">
|
<div class="p-4 w-full text-sm rounded-lg bg-bg shadow-lg border border-black-300">
|
||||||
<div class="w-full p-8">
|
<div class="w-full p-8">
|
||||||
<ui-dropdown v-model="newNotification.eventName" label="Notification Event" :items="eventOptions" class="mb-4" @input="eventOptionUpdated" />
|
<ui-dropdown v-model="newNotification.eventName" label="Notification Event" :items="eventOptions" class="mb-4" @input="eventOptionUpdated" />
|
||||||
|
|
||||||
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
<ui-textarea-with-label v-model="newNotification.bodyTemplate" label="Body Template" class="mb-2" />
|
<ui-textarea-with-label v-model="newNotification.bodyTemplate" label="Body Template" class="mb-2" />
|
||||||
|
|
||||||
<div class="flex pt-4">
|
<div class="flex items-center pt-4">
|
||||||
<div class="flex items-center">
|
<div class="flex items-center">
|
||||||
<ui-toggle-switch v-model="newNotification.enabled" />
|
<ui-toggle-switch v-model="newNotification.enabled" />
|
||||||
<p class="text-lg pl-2">Enabled</p>
|
<p class="text-lg pl-2">Enabled</p>
|
||||||
@ -25,105 +25,6 @@
|
|||||||
<ui-btn color="success" type="submit">Submit</ui-btn>
|
<ui-btn color="success" type="submit">Submit</ui-btn>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<!-- <div class="w-full p-8">
|
|
||||||
<div class="flex py-2">
|
|
||||||
<div class="w-1/2 px-2">
|
|
||||||
<ui-text-input-with-label v-model="newUser.username" label="Username" />
|
|
||||||
</div>
|
|
||||||
<div class="w-1/2 px-2">
|
|
||||||
<ui-text-input-with-label v-if="!isEditingRoot" v-model="newUser.password" :label="isNew ? 'Password' : 'Change Password'" type="password" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div v-show="!isEditingRoot" class="flex py-2">
|
|
||||||
<div class="px-2 w-52">
|
|
||||||
<ui-dropdown v-model="newUser.type" label="Account Type" :disabled="isEditingRoot" :items="accountTypes" @input="userTypeUpdated" />
|
|
||||||
</div>
|
|
||||||
<div class="flex-grow" />
|
|
||||||
<div class="flex items-center pt-4 px-2">
|
|
||||||
<p class="px-3 font-semibold" :class="isEditingRoot ? 'text-gray-300' : ''">Is Active</p>
|
|
||||||
<ui-toggle-switch v-model="newUser.isActive" :disabled="isEditingRoot" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div v-if="!isEditingRoot && newUser.permissions" class="w-full border-t border-b border-black-200 py-2 px-3 mt-4">
|
|
||||||
<p class="text-lg mb-2 font-semibold">Permissions</p>
|
|
||||||
<div class="flex items-center my-2 max-w-md">
|
|
||||||
<div class="w-1/2">
|
|
||||||
<p>Can Download</p>
|
|
||||||
</div>
|
|
||||||
<div class="w-1/2">
|
|
||||||
<ui-toggle-switch v-model="newUser.permissions.download" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="flex items-center my-2 max-w-md">
|
|
||||||
<div class="w-1/2">
|
|
||||||
<p>Can Update</p>
|
|
||||||
</div>
|
|
||||||
<div class="w-1/2">
|
|
||||||
<ui-toggle-switch v-model="newUser.permissions.update" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="flex items-center my-2 max-w-md">
|
|
||||||
<div class="w-1/2">
|
|
||||||
<p>Can Delete</p>
|
|
||||||
</div>
|
|
||||||
<div class="w-1/2">
|
|
||||||
<ui-toggle-switch v-model="newUser.permissions.delete" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="flex items-center my-2 max-w-md">
|
|
||||||
<div class="w-1/2">
|
|
||||||
<p>Can Upload</p>
|
|
||||||
</div>
|
|
||||||
<div class="w-1/2">
|
|
||||||
<ui-toggle-switch v-model="newUser.permissions.upload" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="flex items-center my-2 max-w-md">
|
|
||||||
<div class="w-1/2">
|
|
||||||
<p>Can Access Explicit Content</p>
|
|
||||||
</div>
|
|
||||||
<div class="w-1/2">
|
|
||||||
<ui-toggle-switch v-model="newUser.permissions.accessExplicitContent" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="flex items-center my-2 max-w-md">
|
|
||||||
<div class="w-1/2">
|
|
||||||
<p>Can Access All Libraries</p>
|
|
||||||
</div>
|
|
||||||
<div class="w-1/2">
|
|
||||||
<ui-toggle-switch v-model="newUser.permissions.accessAllLibraries" @input="accessAllLibrariesToggled" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div v-if="!newUser.permissions.accessAllLibraries" class="my-4">
|
|
||||||
<ui-multi-select-dropdown v-model="newUser.librariesAccessible" :items="libraryItems" label="Libraries Accessible to User" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="flex items-cen~ter my-2 max-w-md">
|
|
||||||
<div class="w-1/2">
|
|
||||||
<p>Can Access All Tags</p>
|
|
||||||
</div>
|
|
||||||
<div class="w-1/2">
|
|
||||||
<ui-toggle-switch v-model="newUser.permissions.accessAllTags" @input="accessAllTagsToggled" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div v-if="!newUser.permissions.accessAllTags" class="my-4">
|
|
||||||
<ui-multi-select-dropdown v-model="newUser.itemTagsAccessible" :items="itemTags" label="Tags Accessible to User" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="flex pt-4 px-2">
|
|
||||||
<ui-btn v-if="isEditingRoot" to="/account">Change Root Password</ui-btn>
|
|
||||||
<div class="flex-grow" />
|
|
||||||
<ui-btn color="success" type="submit">Submit</ui-btn>
|
|
||||||
</div>
|
|
||||||
</div> -->
|
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</modals-modal>
|
</modals-modal>
|
||||||
@ -195,6 +96,11 @@ export default {
|
|||||||
if (this.$refs.modal) this.$refs.modal.setHide()
|
if (this.$refs.modal) this.$refs.modal.setHide()
|
||||||
},
|
},
|
||||||
submitForm() {
|
submitForm() {
|
||||||
|
if (!this.newNotification.urls.length) {
|
||||||
|
this.$toast.error('Must enter an Apprise URL')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (this.isNew) {
|
if (this.isNew) {
|
||||||
this.submitCreate()
|
this.submitCreate()
|
||||||
} else {
|
} else {
|
||||||
@ -263,11 +169,12 @@ export default {
|
|||||||
libraryId: null,
|
libraryId: null,
|
||||||
eventName: 'onTest',
|
eventName: 'onTest',
|
||||||
urls: [],
|
urls: [],
|
||||||
titleTemplate: 'Test Title',
|
titleTemplate: '',
|
||||||
bodyTemplate: 'Test Body',
|
bodyTemplate: '',
|
||||||
enabled: true,
|
enabled: true,
|
||||||
type: null
|
type: null
|
||||||
}
|
}
|
||||||
|
this.eventOptionUpdated()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -59,6 +59,15 @@ export default {
|
|||||||
this.selectedNotification = null
|
this.selectedNotification = null
|
||||||
this.showEditModal = true
|
this.showEditModal = true
|
||||||
},
|
},
|
||||||
|
validateAppriseApiUrl() {
|
||||||
|
try {
|
||||||
|
return new URL(this.appriseApiUrl)
|
||||||
|
} catch (error) {
|
||||||
|
console.log('URL error', error)
|
||||||
|
this.$toast.error(error.message)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
submitForm() {
|
submitForm() {
|
||||||
if (this.notificationSettings && this.notificationSettings.appriseApiUrl == this.appriseApiUrl) {
|
if (this.notificationSettings && this.notificationSettings.appriseApiUrl == this.appriseApiUrl) {
|
||||||
this.$toast.info('No update necessary')
|
this.$toast.info('No update necessary')
|
||||||
@ -69,7 +78,10 @@ export default {
|
|||||||
this.$refs.apiUrlInput.blur()
|
this.$refs.apiUrlInput.blur()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Validate apprise api url
|
const isValid = this.validateAppriseApiUrl()
|
||||||
|
if (!isValid) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const updatePayload = {
|
const updatePayload = {
|
||||||
appriseApiUrl: this.appriseApiUrl || null
|
appriseApiUrl: this.appriseApiUrl || null
|
||||||
@ -99,14 +111,25 @@ export default {
|
|||||||
if (!notificationResponse) {
|
if (!notificationResponse) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.notificationSettings = notificationResponse.settings
|
|
||||||
this.notificationData = notificationResponse.data
|
this.notificationData = notificationResponse.data
|
||||||
this.appriseApiUrl = this.notificationSettings.appriseApiUrl
|
this.setNotificationSettings(notificationResponse.settings)
|
||||||
this.notifications = this.notificationSettings.notifications || []
|
},
|
||||||
|
setNotificationSettings(notificationSettings) {
|
||||||
|
this.notificationSettings = notificationSettings
|
||||||
|
this.appriseApiUrl = notificationSettings.appriseApiUrl
|
||||||
|
this.notifications = notificationSettings.notifications || []
|
||||||
|
},
|
||||||
|
notificationsUpdated(notificationSettings) {
|
||||||
|
console.log('Notifications updated', notificationSettings)
|
||||||
|
this.setNotificationSettings(notificationSettings)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
this.init()
|
this.init()
|
||||||
|
this.$root.socket.on('notifications_updated', this.notificationsUpdated)
|
||||||
|
},
|
||||||
|
beforeDestroy() {
|
||||||
|
this.$root.socket.off('notifications_updated', this.notificationsUpdated)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
@ -65,7 +65,7 @@ class Server {
|
|||||||
this.auth = new Auth(this.db)
|
this.auth = new Auth(this.db)
|
||||||
|
|
||||||
// Managers
|
// Managers
|
||||||
this.notificationManager = new NotificationManager(this.db)
|
this.notificationManager = new NotificationManager(this.db, this.emitter.bind(this))
|
||||||
this.backupManager = new BackupManager(this.db, this.emitter.bind(this))
|
this.backupManager = new BackupManager(this.db, this.emitter.bind(this))
|
||||||
this.logManager = new LogManager(this.db)
|
this.logManager = new LogManager(this.db)
|
||||||
this.cacheManager = new CacheManager()
|
this.cacheManager = new CacheManager()
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
const Logger = require('../Logger')
|
const Logger = require('../Logger')
|
||||||
|
const { version } = require('../../package.json')
|
||||||
|
|
||||||
class NotificationController {
|
class NotificationController {
|
||||||
constructor() { }
|
constructor() { }
|
||||||
@ -22,6 +23,11 @@ class NotificationController {
|
|||||||
res.json(this.notificationManager.getData())
|
res.json(this.notificationManager.getData())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fireTestEvent(req, res) {
|
||||||
|
await this.notificationManager.triggerNotification('onTest', { version: `v${version}` }, req.query.fail === '1')
|
||||||
|
res.sendStatus(200)
|
||||||
|
}
|
||||||
|
|
||||||
async createNotification(req, res) {
|
async createNotification(req, res) {
|
||||||
const success = this.db.notificationSettings.createNotification(req.body)
|
const success = this.db.notificationSettings.createNotification(req.body)
|
||||||
|
|
||||||
@ -40,17 +46,18 @@ class NotificationController {
|
|||||||
|
|
||||||
async updateNotification(req, res) {
|
async updateNotification(req, res) {
|
||||||
const success = this.db.notificationSettings.updateNotification(req.body)
|
const success = this.db.notificationSettings.updateNotification(req.body)
|
||||||
console.log('Update notification', success, req.body)
|
|
||||||
if (success) {
|
if (success) {
|
||||||
await this.db.updateEntity('settings', this.db.notificationSettings)
|
await this.db.updateEntity('settings', this.db.notificationSettings)
|
||||||
}
|
}
|
||||||
res.json(this.db.notificationSettings)
|
res.json(this.db.notificationSettings)
|
||||||
}
|
}
|
||||||
|
|
||||||
sendNotificationTest(req, res) {
|
async sendNotificationTest(req, res) {
|
||||||
if (!this.db.notificationSettings.isUsable) return res.status(500).send('Apprise is not configured')
|
if (!this.db.notificationSettings.isUseable) return res.status(500).send('Apprise is not configured')
|
||||||
this.notificationManager.onTest()
|
|
||||||
res.sendStatus(200)
|
const success = await this.notificationManager.sendTestNotification(req.notification)
|
||||||
|
if (success) res.sendStatus(200)
|
||||||
|
else res.sendStatus(500)
|
||||||
}
|
}
|
||||||
|
|
||||||
middleware(req, res, next) {
|
middleware(req, res, next) {
|
||||||
|
@ -3,8 +3,9 @@ const Logger = require("../Logger")
|
|||||||
const { notificationData } = require('../utils/notifications')
|
const { notificationData } = require('../utils/notifications')
|
||||||
|
|
||||||
class NotificationManager {
|
class NotificationManager {
|
||||||
constructor(db) {
|
constructor(db, emitter) {
|
||||||
this.db = db
|
this.db = db
|
||||||
|
this.emitter = emitter
|
||||||
|
|
||||||
this.notificationFailedMap = {}
|
this.notificationFailedMap = {}
|
||||||
}
|
}
|
||||||
@ -17,38 +18,59 @@ class NotificationManager {
|
|||||||
if (!this.db.notificationSettings.isUseable) return
|
if (!this.db.notificationSettings.isUseable) return
|
||||||
|
|
||||||
Logger.debug(`[NotificationManager] onPodcastEpisodeDownloaded: Episode "${episode.title}" for podcast ${libraryItem.media.metadata.title}`)
|
Logger.debug(`[NotificationManager] onPodcastEpisodeDownloaded: Episode "${episode.title}" for podcast ${libraryItem.media.metadata.title}`)
|
||||||
this.triggerNotification('onPodcastEpisodeDownloaded', { libraryItem, episode })
|
const library = this.db.libraries.find(lib => lib.id === libraryItem.libraryId)
|
||||||
|
const eventData = {
|
||||||
|
libraryItemId: libraryItem.id,
|
||||||
|
libraryId: libraryItem.libraryId,
|
||||||
|
libraryName: library ? library.name : 'Unknown',
|
||||||
|
podcastTitle: libraryItem.media.metadata.title,
|
||||||
|
episodeId: episode.id,
|
||||||
|
episodeTitle: episode.title
|
||||||
|
}
|
||||||
|
this.triggerNotification('onPodcastEpisodeDownloaded', eventData)
|
||||||
}
|
}
|
||||||
|
|
||||||
onTest() {
|
onTest() {
|
||||||
this.triggerNotification('onTest')
|
this.triggerNotification('onTest')
|
||||||
}
|
}
|
||||||
|
|
||||||
async triggerNotification(eventName, eventData) {
|
async triggerNotification(eventName, eventData, intentionallyFail = false) {
|
||||||
if (!this.db.notificationSettings.isUseable) return
|
if (!this.db.notificationSettings.isUseable) return false
|
||||||
|
|
||||||
const notifications = this.db.notificationSettings.getNotificationsForEvent(eventName)
|
const notifications = this.db.notificationSettings.getNotificationsForEvent(eventName)
|
||||||
for (const notification of notifications) {
|
for (const notification of notifications) {
|
||||||
Logger.debug(`[NotificationManager] triggerNotification: Sending ${eventName} notification ${notification.id}`)
|
Logger.debug(`[NotificationManager] triggerNotification: Sending ${eventName} notification ${notification.id}`)
|
||||||
const success = await this.sendNotification(notification, eventData)
|
const success = intentionallyFail ? false : await this.sendNotification(notification, eventData)
|
||||||
|
|
||||||
|
notification.updateNotificationFired(success)
|
||||||
if (!success) { // Failed notification
|
if (!success) { // Failed notification
|
||||||
if (!this.notificationFailedMap[notification.id]) this.notificationFailedMap[notification.id] = 1
|
if (notification.numConsecutiveFailedAttempts > 2) {
|
||||||
else this.notificationFailedMap[notification.id]++
|
|
||||||
|
|
||||||
if (this.notificationFailedMap[notification.id] > 2) {
|
|
||||||
Logger.error(`[NotificationManager] triggerNotification: ${notification.eventName}/${notification.id} reached max failed attempts`)
|
Logger.error(`[NotificationManager] triggerNotification: ${notification.eventName}/${notification.id} reached max failed attempts`)
|
||||||
// TODO: Do something like disable the notification
|
notification.enabled = false
|
||||||
|
} else {
|
||||||
|
Logger.error(`[NotificationManager] triggerNotification: ${notification.eventName}/${notification.id} ${notification.numConsecutiveFailedAttempts} failed attempts`)
|
||||||
}
|
}
|
||||||
} else { // Successful notification
|
|
||||||
delete this.notificationFailedMap[notification.id]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await this.db.updateEntity('settings', this.db.notificationSettings)
|
||||||
|
this.emitter('notifications_updated', this.db.notificationSettings)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
sendTestNotification(notification) {
|
||||||
|
const eventData = notificationData.events.find(e => e.name === notification.eventName)
|
||||||
|
if (!eventData) {
|
||||||
|
Logger.error(`[NotificationManager] sendTestNotification: Event not found ${notification.eventName}`)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.sendNotification(notification, eventData.testData)
|
||||||
}
|
}
|
||||||
|
|
||||||
sendNotification(notification, eventData) {
|
sendNotification(notification, eventData) {
|
||||||
const payload = notification.getApprisePayload(eventData)
|
const payload = notification.getApprisePayload(eventData)
|
||||||
return axios.post(`${this.db.notificationSettings.appriseApiUrl}/notify`, payload, { timeout: 6000 }).then((response) => {
|
return axios.post(this.db.notificationSettings.appriseApiUrl, payload, { timeout: 6000 }).then((response) => {
|
||||||
Logger.debug(`[NotificationManager] sendNotification: ${notification.eventName}/${notification.id} response=`, response.data)
|
Logger.debug(`[NotificationManager] sendNotification: ${notification.eventName}/${notification.id} response=`, response.data)
|
||||||
return true
|
return true
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
|
@ -11,6 +11,10 @@ class Notification {
|
|||||||
this.type = 'info'
|
this.type = 'info'
|
||||||
this.enabled = false
|
this.enabled = false
|
||||||
|
|
||||||
|
this.lastFiredAt = null
|
||||||
|
this.lastAttemptFailed = false
|
||||||
|
this.numConsecutiveFailedAttempts = 0
|
||||||
|
this.numTimesFired = 0
|
||||||
this.createdAt = null
|
this.createdAt = null
|
||||||
|
|
||||||
if (notification) {
|
if (notification) {
|
||||||
@ -27,6 +31,10 @@ class Notification {
|
|||||||
this.bodyTemplate = notification.bodyTemplate || ''
|
this.bodyTemplate = notification.bodyTemplate || ''
|
||||||
this.type = notification.type || 'info'
|
this.type = notification.type || 'info'
|
||||||
this.enabled = !!notification.enabled
|
this.enabled = !!notification.enabled
|
||||||
|
this.lastFiredAt = notification.lastFiredAt || null
|
||||||
|
this.lastAttemptFailed = !!notification.lastAttemptFailed
|
||||||
|
this.numConsecutiveFailedAttempts = notification.numConsecutiveFailedAttempts || 0
|
||||||
|
this.numTimesFired = notification.numTimesFired || 0
|
||||||
this.createdAt = notification.createdAt
|
this.createdAt = notification.createdAt
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,6 +48,10 @@ class Notification {
|
|||||||
bodyTemplate: this.bodyTemplate,
|
bodyTemplate: this.bodyTemplate,
|
||||||
enabled: this.enabled,
|
enabled: this.enabled,
|
||||||
type: this.type,
|
type: this.type,
|
||||||
|
lastFiredAt: this.lastFiredAt,
|
||||||
|
lastAttemptFailed: this.lastAttemptFailed,
|
||||||
|
numConsecutiveFailedAttempts: this.numConsecutiveFailedAttempts,
|
||||||
|
numTimesFired: this.numTimesFired,
|
||||||
createdAt: this.createdAt
|
createdAt: this.createdAt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -57,6 +69,13 @@ class Notification {
|
|||||||
}
|
}
|
||||||
|
|
||||||
update(payload) {
|
update(payload) {
|
||||||
|
if (!this.enabled && payload.enabled) {
|
||||||
|
// Reset
|
||||||
|
this.lastFiredAt = null
|
||||||
|
this.lastAttemptFailed = false
|
||||||
|
this.numConsecutiveFailedAttempts = 0
|
||||||
|
}
|
||||||
|
|
||||||
const keysToUpdate = ['libraryId', 'eventName', 'urls', 'titleTemplate', 'bodyTemplate', 'enabled', 'type']
|
const keysToUpdate = ['libraryId', 'eventName', 'urls', 'titleTemplate', 'bodyTemplate', 'enabled', 'type']
|
||||||
var hasUpdated = false
|
var hasUpdated = false
|
||||||
for (const key of keysToUpdate) {
|
for (const key of keysToUpdate) {
|
||||||
@ -75,14 +94,32 @@ class Notification {
|
|||||||
return hasUpdated
|
return hasUpdated
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateNotificationFired(success) {
|
||||||
|
this.lastFiredAt = Date.now()
|
||||||
|
this.lastAttemptFailed = !success
|
||||||
|
this.numConsecutiveFailedAttempts = success ? 0 : this.numConsecutiveFailedAttempts + 1
|
||||||
|
this.numTimesFired++
|
||||||
|
}
|
||||||
|
|
||||||
|
replaceVariablesInTemplate(templateText, data) {
|
||||||
|
const ptrn = /{{ ?([a-zA-Z]+) ?}}/mg
|
||||||
|
|
||||||
|
var match
|
||||||
|
var updatedTemplate = templateText
|
||||||
|
while ((match = ptrn.exec(templateText)) != null) {
|
||||||
|
if (data[match[1]]) {
|
||||||
|
updatedTemplate = updatedTemplate.replace(match[0], data[match[1]])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return updatedTemplate
|
||||||
|
}
|
||||||
|
|
||||||
parseTitleTemplate(data) {
|
parseTitleTemplate(data) {
|
||||||
// TODO: Implement template parsing
|
return this.replaceVariablesInTemplate(this.titleTemplate, data)
|
||||||
return 'Test Title'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
parseBodyTemplate(data) {
|
parseBodyTemplate(data) {
|
||||||
// TODO: Implement template parsing
|
return this.replaceVariablesInTemplate(this.bodyTemplate, data)
|
||||||
return 'Test Body'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getApprisePayload(data) {
|
getApprisePayload(data) {
|
||||||
|
@ -207,6 +207,7 @@ class ApiRouter {
|
|||||||
this.router.get('/notifications', NotificationController.middleware.bind(this), NotificationController.get.bind(this))
|
this.router.get('/notifications', NotificationController.middleware.bind(this), NotificationController.get.bind(this))
|
||||||
this.router.patch('/notifications', NotificationController.middleware.bind(this), NotificationController.update.bind(this))
|
this.router.patch('/notifications', NotificationController.middleware.bind(this), NotificationController.update.bind(this))
|
||||||
this.router.get('/notificationdata', NotificationController.middleware.bind(this), NotificationController.getData.bind(this))
|
this.router.get('/notificationdata', NotificationController.middleware.bind(this), NotificationController.getData.bind(this))
|
||||||
|
this.router.get('/notifications/test', NotificationController.middleware.bind(this), NotificationController.fireTestEvent.bind(this))
|
||||||
this.router.post('/notifications', NotificationController.middleware.bind(this), NotificationController.createNotification.bind(this))
|
this.router.post('/notifications', NotificationController.middleware.bind(this), NotificationController.createNotification.bind(this))
|
||||||
this.router.delete('/notifications/:id', NotificationController.middleware.bind(this), NotificationController.deleteNotification.bind(this))
|
this.router.delete('/notifications/:id', NotificationController.middleware.bind(this), NotificationController.deleteNotification.bind(this))
|
||||||
this.router.patch('/notifications/:id', NotificationController.middleware.bind(this), NotificationController.updateNotification.bind(this))
|
this.router.patch('/notifications/:id', NotificationController.middleware.bind(this), NotificationController.updateNotification.bind(this))
|
||||||
|
@ -1,12 +1,37 @@
|
|||||||
|
const { version } = require('../../package.json')
|
||||||
|
|
||||||
module.exports.notificationData = {
|
module.exports.notificationData = {
|
||||||
events: [
|
events: [
|
||||||
|
{
|
||||||
|
name: 'onPodcastEpisodeDownloaded',
|
||||||
|
requiresLibrary: true,
|
||||||
|
libraryMediaType: 'podcast',
|
||||||
|
description: 'Triggered when a podcast episode is auto-downloaded',
|
||||||
|
variables: ['libraryItemId', 'libraryId', 'podcastTitle', 'episodeTitle', 'libraryName', 'episodeId'],
|
||||||
|
defaults: {
|
||||||
|
title: 'New {{podcastTitle}} Episode!',
|
||||||
|
body: '{{episodeTitle}} has been added to {{libraryName}} library.'
|
||||||
|
},
|
||||||
|
testData: {
|
||||||
|
libraryItemId: 'li_notification_test',
|
||||||
|
libraryId: 'lib_test',
|
||||||
|
libraryName: 'Podcasts',
|
||||||
|
podcastTitle: 'Abs Test Podcast',
|
||||||
|
episodeId: 'ep_notification_test',
|
||||||
|
episodeTitle: 'Successful Test'
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'onTest',
|
name: 'onTest',
|
||||||
requiresLibrary: false,
|
requiresLibrary: false,
|
||||||
description: 'Notification for testing',
|
description: 'Notification for testing',
|
||||||
|
variables: ['version'],
|
||||||
defaults: {
|
defaults: {
|
||||||
title: 'Test Title',
|
title: 'Test Notification on Abs {{version}}',
|
||||||
body: 'Test Body'
|
body: 'Test notificataion body for abs {{version}}.'
|
||||||
|
},
|
||||||
|
testData: {
|
||||||
|
version: 'v' + version
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user