2019-07-11 10:48:52 +02:00
|
|
|
/**
|
|
|
|
* @module converse-push
|
|
|
|
* @description
|
|
|
|
* Converse.js plugin which add support for registering
|
2018-06-06 11:07:59 +02:00
|
|
|
* an "App Server" as defined in XEP-0357
|
2020-01-26 16:21:20 +01:00
|
|
|
* @copyright 2020, the Converse.js contributors
|
|
|
|
* @license Mozilla Public License (MPLv2)
|
2018-06-06 11:07:59 +02:00
|
|
|
*/
|
2020-05-18 10:54:37 +02:00
|
|
|
import { _converse, api, converse } from "@converse/headless/converse-core";
|
2019-11-06 11:01:34 +01:00
|
|
|
import log from "@converse/headless/log";
|
2018-06-06 11:07:59 +02:00
|
|
|
|
2020-03-24 12:26:34 +01:00
|
|
|
const { Strophe, $iq } = converse.env;
|
2018-06-07 16:25:59 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
Strophe.addNamespace('PUSH', 'urn:xmpp:push:0');
|
2018-06-06 11:07:59 +02:00
|
|
|
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
converse.plugins.add('converse-push', {
|
|
|
|
|
|
|
|
initialize () {
|
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by converse.js's plugin machinery.
|
|
|
|
*/
|
2020-06-03 09:17:13 +02:00
|
|
|
api.settings.extend({
|
2018-10-23 03:41:38 +02:00
|
|
|
'push_app_servers': [],
|
|
|
|
'enable_muc_push': false
|
|
|
|
});
|
2018-06-06 11:07:59 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
async function disablePushAppServer (domain, push_app_server) {
|
|
|
|
if (!push_app_server.jid) {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
if (!(await api.disco.supports(Strophe.NS.PUSH, domain || _converse.bare_jid))) {
|
2019-11-06 11:01:34 +01:00
|
|
|
log.warn(`Not disabling push app server "${push_app_server.jid}", no disco support from your server.`);
|
|
|
|
return;
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
const stanza = $iq({'type': 'set'});
|
|
|
|
if (domain !== _converse.bare_jid) {
|
|
|
|
stanza.attrs({'to': domain});
|
2018-06-07 16:25:59 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
stanza.c('disable', {
|
|
|
|
'xmlns': Strophe.NS.PUSH,
|
|
|
|
'jid': push_app_server.jid,
|
|
|
|
});
|
|
|
|
if (push_app_server.node) {
|
|
|
|
stanza.attrs({'node': push_app_server.node});
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
api.sendIQ(stanza)
|
2018-10-23 03:41:38 +02:00
|
|
|
.catch(e => {
|
2019-11-06 11:01:34 +01:00
|
|
|
log.error(`Could not disable push app server for ${push_app_server.jid}`);
|
|
|
|
log.error(e);
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
|
|
|
}
|
2018-06-07 16:25:59 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
async function enablePushAppServer (domain, push_app_server) {
|
|
|
|
if (!push_app_server.jid || !push_app_server.node) {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
const identity = await api.disco.getIdentity('pubsub', 'push', push_app_server.jid);
|
2018-10-23 03:41:38 +02:00
|
|
|
if (!identity) {
|
2019-11-06 11:01:34 +01:00
|
|
|
return log.warn(
|
|
|
|
`Not enabling push the service "${push_app_server.jid}", it doesn't have the right disco identtiy.`
|
2018-10-23 03:41:38 +02:00
|
|
|
);
|
2018-06-07 16:25:59 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
const result = await Promise.all([
|
2020-03-31 13:15:57 +02:00
|
|
|
api.disco.supports(Strophe.NS.PUSH, push_app_server.jid),
|
|
|
|
api.disco.supports(Strophe.NS.PUSH, domain)
|
2018-10-23 03:41:38 +02:00
|
|
|
]);
|
2019-05-07 09:53:36 +02:00
|
|
|
if (!result[0] && !result[1]) {
|
2019-11-06 11:01:34 +01:00
|
|
|
log.warn(`Not enabling push app server "${push_app_server.jid}", no disco support from your server.`);
|
|
|
|
return;
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
const stanza = $iq({'type': 'set'});
|
|
|
|
if (domain !== _converse.bare_jid) {
|
|
|
|
stanza.attrs({'to': domain});
|
|
|
|
}
|
|
|
|
stanza.c('enable', {
|
|
|
|
'xmlns': Strophe.NS.PUSH,
|
|
|
|
'jid': push_app_server.jid,
|
|
|
|
'node': push_app_server.node
|
|
|
|
});
|
|
|
|
if (push_app_server.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_app_server.secret);
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
return api.sendIQ(stanza);
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2018-06-07 16:25:59 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
async function enablePush (domain) {
|
|
|
|
domain = domain || _converse.bare_jid;
|
|
|
|
const push_enabled = _converse.session.get('push_enabled') || [];
|
2020-03-06 15:19:48 +01:00
|
|
|
if (push_enabled.includes(domain)) {
|
2018-10-23 03:41:38 +02:00
|
|
|
return;
|
2018-06-06 11:07:59 +02:00
|
|
|
}
|
2020-10-01 11:13:13 +02:00
|
|
|
const enabled_services = api.settings.get('push_app_servers').filter(s => !s.disable);
|
|
|
|
const disabled_services = api.settings.get('push_app_servers').filter(s => s.disable);
|
2020-03-24 12:26:34 +01:00
|
|
|
const enabled = enabled_services.map(s => enablePushAppServer(domain, s));
|
|
|
|
const disabled = disabled_services.map(s => disablePushAppServer(domain, s));
|
2018-10-23 03:41:38 +02:00
|
|
|
try {
|
2018-11-29 13:22:58 +01:00
|
|
|
await Promise.all(enabled.concat(disabled));
|
2018-10-23 03:41:38 +02:00
|
|
|
} catch (e) {
|
2019-11-06 11:01:34 +01:00
|
|
|
log.error('Could not enable or disable push App Server');
|
|
|
|
if (e) log.error(e);
|
2018-10-23 03:41:38 +02:00
|
|
|
} finally {
|
|
|
|
push_enabled.push(domain);
|
|
|
|
}
|
|
|
|
_converse.session.save('push_enabled', push_enabled);
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('statusInitialized', () => enablePush());
|
2018-09-13 11:46:42 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
function onChatBoxAdded (model) {
|
|
|
|
if (model.get('type') == _converse.CHATROOMS_TYPE) {
|
|
|
|
enablePush(Strophe.getDomainFromJid(model.get('jid')));
|
2018-09-13 11:46:42 +02:00
|
|
|
}
|
2018-06-06 11:07:59 +02:00
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
if (api.settings.get('enable_muc_push')) {
|
|
|
|
api.listen.on('chatBoxesInitialized', () => _converse.chatboxes.on('add', onChatBoxAdded));
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|