-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Notification rule part UI * Wire up backend for alerts
- Loading branch information
Showing
20 changed files
with
538 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Push API Controller. | ||
* | ||
* Implements the Push API for notifications to use | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
const PromiseRouter = require('express-promise-router'); | ||
const WebPush = require('web-push'); | ||
const Database = require('../db'); | ||
const Settings = require('../models/settings'); | ||
|
||
const PushController = PromiseRouter(); | ||
|
||
/** | ||
* Initialize the Push API, generating and storing a VAPID keypair if necessary | ||
*/ | ||
PushController.init = async () => { | ||
let vapid = await Settings.get('push.vapid'); | ||
if (!vapid) { | ||
vapid = WebPush.generateVAPIDKeys(); | ||
await Settings.set('push.vapid', vapid); | ||
} | ||
const {publicKey, privateKey} = vapid; | ||
|
||
const {tunnelDomain} = await Settings.getTunnelInfo(); | ||
WebPush.setVapidDetails(tunnelDomain, publicKey, privateKey); | ||
}; | ||
|
||
/** | ||
* Handle requests for the public key | ||
*/ | ||
PushController.get('/vapid-public-key', async (request, response) => { | ||
const vapid = await Settings.get('push.vapid'); | ||
if (!vapid) { | ||
response.status(500).json({error: 'vapid not configured'}); | ||
return; | ||
} | ||
response.status(200).json({publicKey: vapid.publicKey}); | ||
}); | ||
|
||
PushController.post('/register', async (request, response) => { | ||
await Database.createPushSubscription(request.body.subscription); | ||
response.status(200).json({}); | ||
}); | ||
|
||
PushController.broadcastNotification = async (message) => { | ||
const subscriptions = await Database.getPushSubscriptions(); | ||
for (const subscription of subscriptions) { | ||
WebPush.sendNotification(subscription, message).catch((err) => { | ||
console.warn('Push API error', err); | ||
Database.deletePushSubscription(subscription.id); | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = PushController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.* | ||
*/ | ||
|
||
const assert = require('assert'); | ||
const Effect = require('./Effect'); | ||
const PushController = require('../../controllers/push_controller'); | ||
|
||
/** | ||
* An Effect which creates a notification | ||
*/ | ||
class NotificationEffect extends Effect { | ||
/** | ||
* @param {EffectDescription} desc | ||
*/ | ||
constructor(desc) { | ||
super(desc); | ||
|
||
assert(desc.hasOwnProperty('message')); | ||
|
||
this.message = desc.message; | ||
} | ||
|
||
/** | ||
* @return {EffectDescription} | ||
*/ | ||
toDescription() { | ||
return Object.assign( | ||
super.toDescription(), | ||
{ | ||
message: this.message, | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* @param {State} state | ||
*/ | ||
setState(state) { | ||
if (!state.on) { | ||
return; | ||
} | ||
|
||
PushController.broadcastNotification(this.message); | ||
} | ||
} | ||
|
||
module.exports = NotificationEffect; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.