From d2299d24a23c9d0d835631b72e4f1d1974958f94 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 6 Oct 2016 17:08:38 -0400 Subject: js component --- examples/lib/okpush/db.js | 14 ++++++++++---- examples/lib/okpush/index.js | 29 ++++++++++++++++------------ examples/lib/okpush/package.json | 2 ++ examples/lib/okpush/public/push.js | 31 ++++++++++++++++++++++++++++++ examples/lib/okpush/templates/index.liquid | 18 +++++++++-------- 5 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 examples/lib/okpush/public/push.js (limited to 'examples/lib/okpush') diff --git a/examples/lib/okpush/db.js b/examples/lib/okpush/db.js index 75beb65..83a6ebc 100644 --- a/examples/lib/okpush/db.js +++ b/examples/lib/okpush/db.js @@ -3,6 +3,8 @@ var findOrCreate = require('mongoose-findorcreate') var _ = require('lodash') var db, PushToken +mongoose.Promise = require('bluebird') + function init (config) { db = mongoose.connect(config.mongodbUrl) mongoose.connection.on('error', errorHandler) @@ -52,20 +54,23 @@ function getAllDevices (cb) { } function removeDevice () { PushToken.remove({token: token}, function (err) { - if (err) console.log(err) + if (err) console.log(err) }) } function removeDevices (tokens) { - PushAssociation.remove({token: {$in: tokens}}, function (err) { + PushToken.remove({token: {$in: tokens}}, function (err) { if (err) console.log(err) }) } +function getDeviceCount (cb) { + PushToken.count({}, cb); +} /* notifications */ function updateNotification (key, cb) { var now = new Date - Notification.findOrCreate({key: key}, {last_push: now}, function(err, note, created){ + Notification.findOrCreate({key: key}, {last_push: now}, function(err, note, created) { if (err) { console.error("Error finding/creating notification", err) cb(err, false) @@ -74,8 +79,8 @@ function updateNotification (key, cb) { else if (! created) { note.last_push = now note.save() - cb(null, note) } + console.log(note, created) cb(null, note) }) } @@ -113,6 +118,7 @@ module.exports = { getAllDevices: getAllDevices, removeDevice: removeDevice, removeDevices: removeDevices, + getDeviceCount: getDeviceCount, updateNotification: updateNotification, getNotifications: getNotifications, } \ No newline at end of file diff --git a/examples/lib/okpush/index.js b/examples/lib/okpush/index.js index 3ff8bb5..04cf33a 100644 --- a/examples/lib/okpush/index.js +++ b/examples/lib/okpush/index.js @@ -4,6 +4,7 @@ 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') @@ -47,7 +48,8 @@ function OKPush (options) { db.init(config) router.use('/admin/', passport.initialize()) - + router.use('/public/', express.static(path.join(__dirname, './public'))); + // monkeypatch because of this app.use(router) shit.. obnoxious router.all('/admin/(:path*)?', function (req, res, next) { // req.url = "/_services/push" + req.url @@ -66,22 +68,25 @@ function OKPush (options) { // pass in admin middleware! router.get('/admin', function (req, res) { db.getNotifications(function(err, notes){ - 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 + 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)) }) - templates['index'].render(data).then(function(rendered) { - res.send(rendered); - }).fail(error(req, res, 500)) }) }) - router.post('/admin/send', function (req, res) { + 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) diff --git a/examples/lib/okpush/package.json b/examples/lib/okpush/package.json index 7553616..87ca92c 100644 --- a/examples/lib/okpush/package.json +++ b/examples/lib/okpush/package.json @@ -10,6 +10,8 @@ "license": "LNT", "dependencies": { "apn": "^2.1.1", + "bluebird": "^3.4.6", + "body-parser": "^1.15.2", "lodash": "^4.16.3", "mongoose": "^4.6.2", "mongoose-findorcreate": "^0.1.2", diff --git a/examples/lib/okpush/public/push.js b/examples/lib/okpush/public/push.js new file mode 100644 index 0000000..d369c90 --- /dev/null +++ b/examples/lib/okpush/public/push.js @@ -0,0 +1,31 @@ +$(function(){ + var count = $(".device-count").data("count"); + var confirm_msg = "This will send the notification {{key}} to {{count}} people. Click OK to confirm."; + $(".notifications button").click(function(){ + var $el = $(this) + var data = $el.data() + var msg = confirm_msg.replace("{{key}}", data.key).replace("{{count}}", count) + if (! confirm(msg)) return + $.ajax({ + type: "POST", + url: "/_services/push/send", + data: { key: data.key }, + success: function(){ + alert("Push notification sent.") + var now = new Date() + // "%a %d-%b-%Y %H:%M" + var months = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" ") + var days = "Sun Mon Tue Wed Thu Fri Sat".split(" ") + var date = days[ now.getDay() ] + date += " " + now.getDate() + date += "-" + months[now.getMonth()] + date += "-" + now.getFullYear() + date += " " + now.getHours() + var mins = now.getMinutes() + if (mins < 10) mins = "0" + mins + date += ":" + mins + $el.closest("tr").find(".notification-date").html(date) + } + }) + }) +}) \ No newline at end of file diff --git a/examples/lib/okpush/templates/index.liquid b/examples/lib/okpush/templates/index.liquid index e0547c3..10772b5 100644 --- a/examples/lib/okpush/templates/index.liquid +++ b/examples/lib/okpush/templates/index.liquid @@ -11,6 +11,9 @@ th { th:nth-child(2) { min-width: 300px; } +th:nth-child(3) { + min-width: 170px; +} table,tr,th,td { margin: 0; padding: 0; @@ -31,6 +34,8 @@ tr:nth-child(2n+1) {

Push Notifications

+
Device count: {{ device_count }}
+ @@ -48,11 +53,11 @@ tr:nth-child(2n+1) { -
Key {{spec.alert}} - {% unless spec.last_fired %} + + {% unless spec.last_push %} Never {% else %} - {{spec.last_fired}} + {{ spec.last_push | date: "%a %d-%b-%Y %H:%M" }} {% endunless %} @@ -64,9 +69,6 @@ tr:nth-child(2n+1) { - {% include 'partials/tail' %} + + \ No newline at end of file -- cgit v1.2.3-70-g09d2