add support for Gotify push notifications

This commit is contained in:
2020-09-02 08:27:09 -05:00
parent 7117099993
commit 3ce470a9b2
17 changed files with 374 additions and 1 deletions

View File

@@ -28,6 +28,13 @@ class ForeignIPLoginAlertJob extends Job {
button_link: `${this.configs.get('app.url')}dash/profile`,
}
})
if ( user.notify_config && user.notify_config.active ) {
await user.notify_config.log({
title: `${this.configs.get('app.name')}: Sign-In From New IP`,
message: `Someone signed into your account (${user.uid}) from the IP address ${ip}. If this was you, no further action is required.`,
})
}
} catch (e) {
this.output.error(e)
}

View File

@@ -33,6 +33,15 @@ class PasswordResetJob extends Job {
button_link: key_action.url(),
}
})
if ( user.notify_config && user.notify_config.active ) {
await user.notify_config.log({
title: `${this.configs.get('app.name')}: Password Reset Requested`,
message: `A password reset request was logged for your account (${user.uid}). If this was you, please check your e-mail for further instructions.`,
priority: 8,
})
}
this.output.success('Password reset logged.')
} catch (e) {
this.output.error(e)

View File

@@ -27,6 +27,14 @@ class PasswordResetAlertJob extends Job {
],
},
})
if ( user.notify_config && user.notify_config.active ) {
await user.notify_config.log({
title: `${this.configs.get('app.name')}: Password Reset`,
message: `The password to your account (${user.uid}) was reset from the IP address ${ip}. If this was not you, please contact your system administrator.`,
priority: 8,
})
}
} catch (e) {
this.output.error(e)
}

View File

@@ -0,0 +1,31 @@
const { Job } = require('flitter-jobs')
class PushNotifyJob extends Job {
static get services() {
return [...super.services, 'models', 'jobs', 'output']
}
async execute(job) {
try {
const User = this.models.get('auth:User')
const { data } = job
let { title = '', message, priority = 5, user_id } = data
const user = await User.findById(user_id)
if ( !user ) throw new Error('Invalid user_id.')
const notify = user.notify_config
if ( !notify || !notify.active ) throw new Error('User does not have notifications configured.')
this.output.info(`Sending notification to ${user.uid}...`)
await notify.send({ title, message, priority })
} catch (e) {
this.output.error(e)
}
this.output.success(`Notification sent!`)
}
}
module.exports = exports = PushNotifyJob