From 03e8e493d7b659d5b36a8141b21f99c6a2ea8218 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Thu, 23 Jan 2014 16:34:21 -0800 Subject: [PATCH] Add experimental push notification support. --- clientapp/app.js | 2 + clientapp/helpers/pushNotifications.js | 77 ++++++++++++++++++++++++++ clientapp/helpers/xmppEventHandlers.js | 2 +- clientapp/models/me.js | 9 +++ package.json | 3 +- 5 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 clientapp/helpers/pushNotifications.js diff --git a/clientapp/app.js b/clientapp/app.js index b3945a1..75829ac 100644 --- a/clientapp/app.js +++ b/clientapp/app.js @@ -12,6 +12,7 @@ var MainView = require('./views/main'); var Router = require('./router'); var Storage = require('./storage'); var xmppEventHandlers = require('./helpers/xmppEventHandlers'); +var pushNotifications = require('./helpers/pushNotifications'); var Notify = require('notify.js'); var Desktop = require('./helpers/desktop'); var AppCache = require('./helpers/cache'); @@ -64,6 +65,7 @@ module.exports = { }; self.api = window.client = StanzaIO.createClient(config); + client.use(pushNotifications); xmppEventHandlers(self.api, self); if (self.api.jingle.capabilities.length > 1) { diff --git a/clientapp/helpers/pushNotifications.js b/clientapp/helpers/pushNotifications.js new file mode 100644 index 0000000..21f4d63 --- /dev/null +++ b/clientapp/helpers/pushNotifications.js @@ -0,0 +1,77 @@ +"use strict"; + +var jxt = require('jxt'); +var stanzaio = require('stanza.io'); + + +jxt.extend(stanzaio.Message, jxt.define({ + name: 'pushNotification', + namespace: 'urn:xmpp:push:0', + element: 'push', + fields: { + body: jxt.subText('urn:xmpp:push:0', 'body') + } +})); + +jxt.extend(stanzaio.Iq, jxt.define({ + name: 'registerPush', + namespace: 'urn:xmpp:push:0', + element: 'register', + fields: { + service: jxt.text() + } +})); + +jxt.extend(stanzaio.Iq, jxt.define({ + name: 'unregisterPush', + namespace: 'urn:xmpp:push:0', + element: 'unregister', + fields: { + service: jxt.text() + } +})); + + +jxt.extend(stanzaio.Iq, jxt.define({ + name: 'otalkRegister', + namespace: 'http://otalk.im/protocol/push', + element: 'register', + fields: { + deviceID: jxt.text() + } +})); + + +module.exports = function (client) { + client.registerPushService = function (jid, cb) { + return client.sendIq({ + type: 'set', + registerPush: { + service: jid + } + }, cb); + }; + + client.getPushServices = function (cb) { + return client.getDiscoItems('', 'urn:xmpp:push', cb); + }; + + client.unregisterPushService = function (jid, cb) { + return client.sendIq({ + type: 'set', + unregisterPush: { + service: jid + } + }, cb); + }; + + client.otalkRegister = function (deviceID, cb) { + return client.sendIq({ + type: 'set', + to: 'push@push.otalk.im/prod', + otalkRegister: { + deviceID: deviceID + } + }, cb); + }; +}; diff --git a/clientapp/helpers/xmppEventHandlers.js b/clientapp/helpers/xmppEventHandlers.js index ed7bb6e..099f694 100644 --- a/clientapp/helpers/xmppEventHandlers.js +++ b/clientapp/helpers/xmppEventHandlers.js @@ -79,7 +79,7 @@ module.exports = function (client, app) { } localStorage.config = JSON.stringify({ - jid: client.config.jid, + jid: client.config.jid.bare, server: client.config.server, wsURL: client.config.wsURL, credentials: creds diff --git a/clientapp/models/me.js b/clientapp/models/me.js index 8b4aa51..7aa59b9 100644 --- a/clientapp/models/me.js +++ b/clientapp/models/me.js @@ -28,6 +28,7 @@ module.exports = HumanModel.define({ this.bind('change:rosterVer', this.save, this); this.contacts.bind('change:unreadCount', this.updateUnreadCount, this); app.state.bind('change:active', this.updateIdlePresence, this); + app.state.bind('change:deviceIDReady', this.registerDevice, this); }, props: { jid: ['object', true], @@ -195,5 +196,13 @@ module.exports = HumanModel.define({ this.stream.stop(); this.stream = null; } + }, + registerDevice: function () { + var deviceID = app.state.deviceID; + client.otalkRegister(deviceID).then(function () { + client.registerPush('push@push.otalk.im/prod'); + }).catch(function (err) { + console.log('Could not enable push notifications'); + }); } }); diff --git a/package.json b/package.json index c3e7e1c..8c01598 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "otalk.im", - "version": "0.0.16", + "version": "0.0.17", "description": "Otalk: WebRTC Enabled XMPP Client, in the Browser", "repository": { "type": "git", @@ -21,6 +21,7 @@ "human-model": "2.6.0", "human-view": "1.5.0", "jade": "0.35.0", + "jxt": "0.5.1", "moonboots": "1.0.0", "node-uuid": "1.4.1", "notify.js": "0.0.3",