From f40e2286faef696c25a81c04635aaf737606a39a Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 16 Feb 2017 02:17:03 +0100 Subject: lib --- lib/okpush/db.js | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 lib/okpush/db.js (limited to 'lib/okpush/db.js') diff --git a/lib/okpush/db.js b/lib/okpush/db.js new file mode 100644 index 00000000..844bccae --- /dev/null +++ b/lib/okpush/db.js @@ -0,0 +1,132 @@ +var mongoose = require('mongoose') +var findOrCreate = require('mongoose-findorcreate') +var _ = require('lodash') +var db, PushToken, Notification, Channel + +mongoose.Promise = require('bluebird') + +function init (config) { + db = mongoose.connect(config.mongodbUrl) + mongoose.connection.on('error', errorHandler) + + var pushTokenSchema = new db.Schema({ + type: { + type: 'String', + required: true, + lowercase: true, + enum: ['ios', 'android'], + }, + token: { + type: 'String', + required: true, + }, + channel: { + type: 'String', + required: true, + }, + }) + + var channelSchema = new db.Schema({ + channel: { + type: 'String', + required: true, + lowercase: true, + }, + last_push: { + type: 'Date', + required: true, + } + }) + channelSchema.plugin(findOrCreate); + + var notificationSchema = new db.Schema({ + channel: { + type: 'String', + required: true, + lowercase: true, + }, + date: { + type: 'Date', + required: true, + } + }) + + PushToken = db.model('PushToken', pushTokenSchema) + Notification = db.model('Notification', notificationSchema) + Channel = db.model('Channel', channelSchema) +} + +function errorHandler (error) { + console.error('ERROR: ' + error) +} + +/* devices / tokens */ + +function addToken (deviceType, token, channel) { + var pushItem = new PushToken({ + type: deviceType, + token: token, + channel: channel + }) + pushItem.save() +} +function getAllTokens (channel, cb) { + PushToken.find({ channel: channel }, function (err, items) { + if (err) return cb(err, null) + var items = _.map(items, function (item) { + return _.pick(item, ['type', 'token']) + }) + return cb(null, items) + }) +} +function removeToken (token, channel) { + PushToken.find({ token: token, channel: channel }).remove().exec() +} +function getDeviceCount (channel, cb) { + PushToken.count({ channel: channel }, cb) +} + +/* notifications */ + +function addNotification (channel, cb) { + var now = new Date + Channel.findOrCreate({channel: channel}, {last_push: now}, function(err, note, created) { + if (err) { + console.error("Error finding/creating notification", err) + cb(err, false) + return + } + else if (! created) { + note.last_push = now + note.save() + } + cb(null, note) + }) + new Notification ({ + channel: channel, + date: now, + }).save() +} +function getNotifications (cb) { + Notification.find(function (err, items) { + if (err) return cb(err, null) + + var items = _.map(items, function (item) { + return _.pick(item, ['channel', 'last_push']) + }) + + return cb(null, items) + }) +} + +/* wrap functions for some reason */ + +module.exports = { + init: init, + addToken: addToken, + removeToken: removeToken, + getAllTokens: getAllTokens, + getDeviceCount: getDeviceCount, + addNotification: addNotification, + getNotifications: getNotifications, +} \ No newline at end of file -- cgit v1.2.3-70-g09d2 From d3d195470caef02891de58ed25b92a02c088c37d Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 16 Feb 2017 18:32:08 +0100 Subject: routes --- lib/okpush/db.js | 15 +++++---------- lib/okpush/index.js | 16 ++++++++++------ 2 files changed, 15 insertions(+), 16 deletions(-) (limited to 'lib/okpush/db.js') diff --git a/lib/okpush/db.js b/lib/okpush/db.js index 844bccae..ad18b7b3 100644 --- a/lib/okpush/db.js +++ b/lib/okpush/db.js @@ -10,7 +10,7 @@ function init (config) { mongoose.connection.on('error', errorHandler) var pushTokenSchema = new db.Schema({ - type: { + platform: { type: 'String', required: true, lowercase: true, @@ -62,13 +62,8 @@ function errorHandler (error) { /* devices / tokens */ -function addToken (deviceType, token, channel) { - var pushItem = new PushToken({ - type: deviceType, - token: token, - channel: channel - }) - pushItem.save() +function addToken (data) { + return new PushToken(data).save() } function getAllTokens (channel, cb) { PushToken.find({ channel: channel }, function (err, items) { @@ -79,8 +74,8 @@ function getAllTokens (channel, cb) { return cb(null, items) }) } -function removeToken (token, channel) { - PushToken.find({ token: token, channel: channel }).remove().exec() +function removeToken (data) { + PushToken.find(data).remove().exec() } function getDeviceCount (channel, cb) { PushToken.count({ channel: channel }, cb) diff --git a/lib/okpush/index.js b/lib/okpush/index.js index a9ba12eb..a509c891 100644 --- a/lib/okpush/index.js +++ b/lib/okpush/index.js @@ -69,6 +69,7 @@ function OKPush (options) { // pass in admin middleware! router.get('/admin', function (req, res) { + // change this to get notification counts for each channel db.getNotifications(function(err, notes){ db.getDeviceCount(function(err, count){ var data = { @@ -100,14 +101,17 @@ function OKPush (options) { // should work without middleware router.post('/add', function (req, res) { - // add a key - db. - registrationId: localStorage.getItem("yoox.registrationId"), - channel: channel, - platform, + db.addToken({ + token: req.body.registrationId, + channel: req.body.channel, + platform: req.body.platform, + }) }) router.post('/remove', function (req, res) { - // remove a key + db.removeToken({ + token: req.body.registrationId, + channel: req.body.channel, + }) }) this._router = router -- cgit v1.2.3-70-g09d2 From cf85cc2b75b9c3ead3a693b6fa0feeca5b9e70ba Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Sun, 19 Feb 2017 01:23:53 +0100 Subject: counts --- lib/okpush/db.js | 17 +++++++++++++++-- lib/okpush/index.js | 35 +++++++++++++++++++---------------- lib/okpush/templates/index.liquid | 6 ++++-- 3 files changed, 38 insertions(+), 20 deletions(-) (limited to 'lib/okpush/db.js') diff --git a/lib/okpush/db.js b/lib/okpush/db.js index ad18b7b3..f64acf69 100644 --- a/lib/okpush/db.js +++ b/lib/okpush/db.js @@ -77,8 +77,21 @@ function getAllTokens (channel, cb) { function removeToken (data) { PushToken.find(data).remove().exec() } -function getDeviceCount (channel, cb) { - PushToken.count({ channel: channel }, cb) +function getDeviceCount (channels, cb) { + var countz = {} + get_next() + function get_next() { + var channel = channels.pop() + PushToken.count({ channel: channel }, function(err, count){ + countz[channel] = count + if (channels.length) { + return get_next() + } + else { + cb(countz) + } + }) + } } /* notifications */ diff --git a/lib/okpush/index.js b/lib/okpush/index.js index 2fe59932..df9c8265 100644 --- a/lib/okpush/index.js +++ b/lib/okpush/index.js @@ -50,37 +50,40 @@ function OKPush (options) { apn.init(config) db.init(config) - router.use('/admin/', passport.initialize()) +// 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() - }) +// 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) { - // change this to get notification counts for each channel db.getNotifications(function(err, notes){ - db.getDeviceCount(function(err, count){ + var channels = Object.keys(config.notifications) + db.getDeviceCount(channels, function(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 } + if (note.key in count) { + data.notifications[ note.key ].count = count[note.key] + } }) templates['index'].render(data).then(function(rendered) { res.send(rendered); diff --git a/lib/okpush/templates/index.liquid b/lib/okpush/templates/index.liquid index 10772b55..1db77a58 100644 --- a/lib/okpush/templates/index.liquid +++ b/lib/okpush/templates/index.liquid @@ -34,13 +34,12 @@ tr:nth-child(2n+1) {

Push Notifications

-
Device count: {{ device_count }}
- + {% for pair in notifications %} @@ -60,6 +59,9 @@ tr:nth-child(2n+1) { {{ spec.last_push | date: "%a %d-%b-%Y %H:%M" }} {% endunless %} + -- cgit v1.2.3-70-g09d2
Key Message Last PushRegistrations
+ {{spec.count}} +