summaryrefslogtreecommitdiff
path: root/lib/okpush/index.js
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2017-02-16 02:17:03 +0100
committerJules Laplace <jules@okfoc.us>2017-02-16 02:17:03 +0100
commitf40e2286faef696c25a81c04635aaf737606a39a (patch)
treebedf10e8c731deae7d275e0e5d9d6ded604db154 /lib/okpush/index.js
parent71e277a76862a1787ab34194faf762b7623347fd (diff)
lib
Diffstat (limited to 'lib/okpush/index.js')
-rw-r--r--lib/okpush/index.js120
1 files changed, 120 insertions, 0 deletions
diff --git a/lib/okpush/index.js b/lib/okpush/index.js
new file mode 100644
index 00000000..a9ba12eb
--- /dev/null
+++ b/lib/okpush/index.js
@@ -0,0 +1,120 @@
+/**
+ * OKPush - Handles basic broadcast push notifications, as well as keeping track of tokens.
+ */
+
+var path = require('path')
+var passport = require('passport')
+var DigestStrategy = require('passport-http').DigestStrategy;
+var bodyParser = require('body-parser')
+var OKTemplate = require('../../../app/node_modules/oktemplate')
+var apn = require('./apn')
+var db = require('./db')
+
+passport.use(new DigestStrategy({qop: 'auth'}, function authenticate(username, done) {
+ if (!process.env.OK_USER || !process.env.OK_PASS) {
+ return done(new Error('No user or pass configured on server'))
+ } else {
+ return done(null, process.env.OK_USER, process.env.OK_PASS)
+ }
+}))
+
+function OKPush (options) {
+ if (!(this instanceof OKPush)) return new OKPush(options)
+ options = options || {}
+ if (!options.express)
+ throw new Error('Express not provided to OKPush')
+ if (!options.config)
+ throw new Error('Configuration not provided to OKPush')
+ if (!options.config.notifications)
+ throw new Error('Notifications not provided to OKPush')
+ if (!options.config.bundleId)
+ throw new Error('bundleId not provided to OKPush')
+ if (!options.config.mongodbUrl)
+ throw new Error('mongodbUrl not provided to OKPush')
+
+ var express = options.express
+ var router = express.Router()
+ var config = options.config
+ var meta = options.meta
+ var error = options.errorHandler
+ // var okcms_db = options.db
+
+ var templateProvider = this._templateProvider = new OKTemplate({
+ root: path.join(__dirname, './templates'),
+ debug: meta.debug
+ })
+
+ var templates = {}
+ templates['index'] = templateProvider.getTemplate('index')
+
+ apn.init(config)
+ db.init(config)
+
+ router.use('/admin/', passport.initialize())
+ router.use('/public/', express.static(path.join(__dirname, './public')));
+
+ // monkeypatch because of app.use(router) .. obnoxious
+ router.all('/admin/(:path*)?', function (req, res, next) {
+ req.newUrl = req.url
+ req.url = req.originalUrl
+ next()
+ })
+ router.all('/admin/(:path*)?', passport.authenticate('digest', {
+ session: false
+ }))
+ router.all('/admin/(:path*)?', function (req, res, next) {
+ req.url = req.newUrl
+ next()
+ })
+
+ // pass in admin middleware!
+ router.get('/admin', function (req, res) {
+ db.getNotifications(function(err, notes){
+ db.getDeviceCount(function(err, count){
+ var data = {
+ meta: meta,
+ notifications: config.notifications,
+ device_count: count,
+ }
+ notes.forEach(function(note){
+ if (note.key in data.notifications) {
+ data.notifications[ note.key ].last_push = note.last_push
+ }
+ })
+ templates['index'].render(data).then(function(rendered) {
+ res.send(rendered);
+ }).fail(error(req, res, 500))
+ })
+ })
+ })
+
+ router.post('/send', bodyParser.urlencoded({ extended: false }), function (req, res) {
+ var key = req.body.key
+ var opt = options.config.notifications[key]
+ var note = apn.buildPayload(opt, options.config.bundleId)
+ // apn.push(note)
+ db.updateNotification(key, function(){
+ res.send(200)
+ })
+ })
+
+ // should work without middleware
+ router.post('/add', function (req, res) {
+ // add a key
+ db.
+ registrationId: localStorage.getItem("yoox.registrationId"),
+ channel: channel,
+ platform,
+ })
+ router.post('/remove', function (req, res) {
+ // remove a key
+ })
+
+ this._router = router
+}
+
+OKPush.prototype.middleware = function () {
+ return this._router
+}
+
+module.exports = OKPush