|
@@ -16,7 +16,7 @@
|
|
|
|
|
|
Strophe.addNamespace('PUSH', 'urn:xmpp:push:0');
|
|
|
|
|
|
-
|
|
|
+
|
|
|
converse.plugins.add('converse-push', {
|
|
|
|
|
|
initialize () {
|
|
@@ -27,56 +27,99 @@
|
|
|
{ __ } = _converse;
|
|
|
|
|
|
_converse.api.settings.update({
|
|
|
- 'push_service': undefined,
|
|
|
- 'push_service_node': undefined,
|
|
|
- 'push_service_secret': undefined
|
|
|
+ 'push_services': [],
|
|
|
});
|
|
|
|
|
|
- function enablePush() {
|
|
|
- if (_converse.session.get('push_enabled')) {
|
|
|
+ function disablePushService (push_service) {
|
|
|
+ if (!push_service.jid) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Promise.all([
|
|
|
+ _converse.api.disco.supports(Strophe.NS.PUSH, _converse.bare_jid)
|
|
|
+ ]).then((result) => {
|
|
|
+ if (!result[0].length && !result[1].length) {
|
|
|
+ return _converse.log(
|
|
|
+ `Not disabling push service "${push_service.jid}", no disco support from your server.`,
|
|
|
+ Strophe.LogLevel.WARN
|
|
|
+ );
|
|
|
+ }
|
|
|
+ const stanza = $iq({'type': 'set'})
|
|
|
+ .c('disable', {
|
|
|
+ 'xmlns': Strophe.NS.PUSH,
|
|
|
+ 'jid': push_service.jid,
|
|
|
+ });
|
|
|
+ if (push_service.node) {
|
|
|
+ stanza.attrs({'node': push_service.node});
|
|
|
+ }
|
|
|
+
|
|
|
+ _converse.api.sendIQ(stanza)
|
|
|
+ .then(() => _converse.session.set('push_enabled', true))
|
|
|
+ .catch((e) => {
|
|
|
+ _converse.log(`Could not enable push service for ${push_service.jid}`, Strophe.LogLevel.ERROR);
|
|
|
+ _converse.log(e, Strophe.LogLevel.ERROR);
|
|
|
+ });
|
|
|
+ }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
|
|
+ }
|
|
|
+
|
|
|
+ function enablePushService (push_service) {
|
|
|
+ if (!push_service.jid || !push_service.node) {
|
|
|
return;
|
|
|
}
|
|
|
- if (_converse.push_service && _converse.push_service_node) {
|
|
|
- _converse.api.disco.getIdentity('pubsub', 'push', _converse.push_service)
|
|
|
- .then((identity) => {
|
|
|
- if (!identity) {
|
|
|
+ _converse.api.disco.getIdentity('pubsub', 'push', push_service.jid)
|
|
|
+ .then((identity) => {
|
|
|
+ if (!identity) {
|
|
|
+ return _converse.log(
|
|
|
+ `Not enabling push the service "${push_service.jid}", it doesn't have the right disco identtiy.`,
|
|
|
+ Strophe.LogLevel.WARN
|
|
|
+ );
|
|
|
+ }
|
|
|
+ return Promise.all([
|
|
|
+ _converse.api.disco.supports(Strophe.NS.PUSH, push_service.jid),
|
|
|
+ _converse.api.disco.supports(Strophe.NS.PUSH, _converse.bare_jid)
|
|
|
+ ]).then((result) => {
|
|
|
+ if (!result[0].length && !result[1].length) {
|
|
|
return _converse.log(
|
|
|
- `Not enabling push the service "${_converse.push_service}", it doesn't have the right disco identtiy.`,
|
|
|
+ `Not enabling push service "${push_service.jid}", no disco support from your server.`,
|
|
|
Strophe.LogLevel.WARN
|
|
|
);
|
|
|
}
|
|
|
- return Promise.all([
|
|
|
- _converse.api.disco.supports(Strophe.NS.PUSH, _converse.push_service),
|
|
|
- _converse.api.disco.supports(Strophe.NS.PUSH, _converse.bare_jid)
|
|
|
- ]).then((result) => {
|
|
|
- if (!result[0].length && !result[1].length) {
|
|
|
- return _converse.log(
|
|
|
- `Not enabling push service "${_converse.push_service}", no disco support`,
|
|
|
- Strophe.LogLevel.WARN
|
|
|
- );
|
|
|
- }
|
|
|
- const stanza = $iq({'type': 'set'})
|
|
|
- .c('enable', {
|
|
|
- 'xmlns': Strophe.NS.PUSH,
|
|
|
- 'jid': _converse.push_service,
|
|
|
- 'node': _converse.push_service_node
|
|
|
- });
|
|
|
- if (_converse.push_service_secret) {
|
|
|
- stanza.c('x', {'xmlns': Strophe.NS.XFORM, 'type': 'submit'})
|
|
|
- .c('field', {'var': 'FORM_TYPE'})
|
|
|
- .c('value').t(`${Strophe.NS.PUBSUB}#publish-options`).up().up()
|
|
|
- .c('field', {'var': 'secret'})
|
|
|
- .c('value').t(_converse.push_service_secret);
|
|
|
- }
|
|
|
- _converse.api.sendIQ(stanza)
|
|
|
- .then(() => _converse.session.set('push_enabled', true))
|
|
|
- .catch((e) => {
|
|
|
- _converse.log(`Could not enable push service for ${_converse.push_service}`, Strophe.LogLevel.ERROR);
|
|
|
- _converse.log(e, Strophe.LogLevel.ERROR);
|
|
|
- });
|
|
|
- }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
|
|
+ const stanza = $iq({'type': 'set'})
|
|
|
+ .c('enable', {
|
|
|
+ 'xmlns': Strophe.NS.PUSH,
|
|
|
+ 'jid': push_service.jid,
|
|
|
+ 'node': push_service.node
|
|
|
+ });
|
|
|
+ if (push_service.secret) {
|
|
|
+ stanza.c('x', {'xmlns': Strophe.NS.XFORM, 'type': 'submit'})
|
|
|
+ .c('field', {'var': 'FORM_TYPE'})
|
|
|
+ .c('value').t(`${Strophe.NS.PUBSUB}#publish-options`).up().up()
|
|
|
+ .c('field', {'var': 'secret'})
|
|
|
+ .c('value').t(push_service.secret);
|
|
|
+ }
|
|
|
+ _converse.api.sendIQ(stanza)
|
|
|
+ .then(() => _converse.session.set('push_enabled', true))
|
|
|
+ .catch((e) => {
|
|
|
+ _converse.log(`Could not enable push service for ${push_service.jid}`, Strophe.LogLevel.ERROR);
|
|
|
+ _converse.log(e, Strophe.LogLevel.ERROR);
|
|
|
+ });
|
|
|
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
|
|
+ }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
|
|
+ }
|
|
|
+
|
|
|
+ function enablePush () {
|
|
|
+ if (_converse.session.get('push_enabled')) {
|
|
|
+ // XXX: this code is still a bit naive. We set push_enabled
|
|
|
+ // to true as soon as the first push service has been set.
|
|
|
+ //
|
|
|
+ // When enabling or disabling multiple push services,
|
|
|
+ // we won't wait until we have confirmation that all have been set.
|
|
|
+ return;
|
|
|
}
|
|
|
+ const enabled_services = _.reject(_converse.push_services, 'disable');
|
|
|
+ _.each(enabled_services, enablePushService);
|
|
|
+
|
|
|
+ const disabled_services = _.filter(_converse.push_services, 'disable');
|
|
|
+ _.each(disabled_services, disablePushService);
|
|
|
}
|
|
|
_converse.api.listen.on('statusInitialized', enablePush);
|
|
|
}
|