summaryrefslogtreecommitdiff
path: root/examples/lib/okpush
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2016-10-06 17:08:38 -0400
committerJules Laplace <jules@okfoc.us>2016-10-06 17:08:38 -0400
commitd2299d24a23c9d0d835631b72e4f1d1974958f94 (patch)
tree2133ec4d5701767d9fd8d51051eaea768e07629f /examples/lib/okpush
parentc8c0b465d9796b526d268ea0e641fd7ba00e44bb (diff)
js component
Diffstat (limited to 'examples/lib/okpush')
-rw-r--r--examples/lib/okpush/db.js14
-rw-r--r--examples/lib/okpush/index.js29
-rw-r--r--examples/lib/okpush/package.json2
-rw-r--r--examples/lib/okpush/public/push.js31
-rw-r--r--examples/lib/okpush/templates/index.liquid18
5 files changed, 70 insertions, 24 deletions
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) {
<h2>Push Notifications</h2>
+ <div class="device-count" data-count="{{ device_count }}">Device count: {{ device_count }}</div>
+
<table class="notifications" cellpadding="0" cellspacing="0">
<tr>
<th>Key</th>
@@ -48,11 +53,11 @@ tr:nth-child(2n+1) {
<td>
{{spec.alert}}
</td>
- <td>
- {% unless spec.last_fired %}
+ <td class="notification-date">
+ {% unless spec.last_push %}
Never
{% else %}
- {{spec.last_fired}}
+ {{ spec.last_push | date: "%a %d-%b-%Y %H:%M" }}
{% endunless %}
</td>
<td>
@@ -64,9 +69,6 @@ tr:nth-child(2n+1) {
</section>
-<script>
-$(function(){
- $("
-})
-</script>
{% include 'partials/tail' %}
+
+<script src="public/push.js"></script> \ No newline at end of file