1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
/**
* 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('../../node_modules/okcms/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) {
console.log(req.url)
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){
var channels = Object.keys(config.notifications)
db.getDeviceCount(channels, function(count){
var data = {
meta: meta,
notifications: config.notifications,
}
notes.forEach(function(note){
if (note.key in data.notifications) {
data.notifications[ note.key ].last_push = note.last_push
}
})
Object.keys(count).forEach(function(key){
data.notifications[ key ].count = count[key]
})
templates['index'].render(data).then(function(rendered) {
res.send(rendered);
}).fail(error(req, res, 500))
})
})
})
router.get('/list', function(req, res){
db.getAllTokens("hub", function(err, hubz){
res.json(hubz)
})
})
router.post('/send', bodyParser.urlencoded({ extended: false }), function (req, res) {
var channel = req.body.channel
var opt = options.config.notifications[channel]
var note = apn.buildPayload(opt, options.config.bundleId)
apn.push(channel, note)
db.addNotification(channel, function(){
res.sendStatus(200)
})
})
// should work without middleware
router.post('/add', bodyParser.urlencoded({ extended: false }), function (req, res) {
db.addToken({
token: req.body.registrationId,
channel: req.body.channel,
platform: req.body.platform,
})
res.sendStatus(200)
})
router.post('/remove', bodyParser.urlencoded({ extended: false }), function (req, res) {
db.removeToken({
token: req.body.registrationId,
channel: req.body.channel,
})
res.sendStatus(200)
})
this._router = router
}
OKPush.prototype.middleware = function () {
return this._router
}
module.exports = OKPush
|