Split push plugin up into multiple files
This commit is contained in:
parent
60826ac6c6
commit
5cce34eb40
|
@ -25,7 +25,6 @@ module.exports = function(config) {
|
||||||
{ pattern: "node_modules/sinon/pkg/sinon.js", type: 'module' },
|
{ pattern: "node_modules/sinon/pkg/sinon.js", type: 'module' },
|
||||||
{ pattern: "spec/mock.js", type: 'module' },
|
{ pattern: "spec/mock.js", type: 'module' },
|
||||||
|
|
||||||
{ pattern: "spec/push.js", type: 'module' },
|
|
||||||
{ pattern: "spec/user-details-modal.js", type: 'module' },
|
{ pattern: "spec/user-details-modal.js", type: 'module' },
|
||||||
{ pattern: "src/headless/plugins/caps/tests/caps.js", type: 'module' },
|
{ pattern: "src/headless/plugins/caps/tests/caps.js", type: 'module' },
|
||||||
{ pattern: "src/headless/plugins/chat/tests/api.js", type: 'module' },
|
{ pattern: "src/headless/plugins/chat/tests/api.js", type: 'module' },
|
||||||
|
@ -87,6 +86,7 @@ module.exports = function(config) {
|
||||||
{ pattern: "src/plugins/notifications/tests/notification.js", type: 'module' },
|
{ pattern: "src/plugins/notifications/tests/notification.js", type: 'module' },
|
||||||
{ pattern: "src/plugins/omemo/tests/media-sharing.js", type: 'module' },
|
{ pattern: "src/plugins/omemo/tests/media-sharing.js", type: 'module' },
|
||||||
{ pattern: "src/plugins/omemo/tests/omemo.js", type: 'module' },
|
{ pattern: "src/plugins/omemo/tests/omemo.js", type: 'module' },
|
||||||
|
{ pattern: "src/plugins/push/tests/push.js", type: 'module' },
|
||||||
{ pattern: "src/plugins/register/tests/register.js", type: 'module' },
|
{ pattern: "src/plugins/register/tests/register.js", type: 'module' },
|
||||||
{ pattern: "src/plugins/rootview/tests/root.js", type: 'module' },
|
{ pattern: "src/plugins/rootview/tests/root.js", type: 'module' },
|
||||||
{ pattern: "src/plugins/rosterview/tests/presence.js", type: 'module' },
|
{ pattern: "src/plugins/rosterview/tests/presence.js", type: 'module' },
|
||||||
|
|
|
@ -29,7 +29,7 @@ import "./plugins/muc-views/index.js"; // Views related to MUC
|
||||||
import "./plugins/notifications/index.js";
|
import "./plugins/notifications/index.js";
|
||||||
import "./plugins/omemo/index.js";
|
import "./plugins/omemo/index.js";
|
||||||
import "./plugins/profile/index.js";
|
import "./plugins/profile/index.js";
|
||||||
import "./plugins/push.js"; // XEP-0357 Push Notifications
|
import "./plugins/push/index.js"; // XEP-0357 Push Notifications
|
||||||
import "./plugins/register/index.js"; // XEP-0077 In-band registration
|
import "./plugins/register/index.js"; // XEP-0077 In-band registration
|
||||||
import "./plugins/roomslist/index.js"; // Show currently open chat rooms
|
import "./plugins/roomslist/index.js"; // Show currently open chat rooms
|
||||||
import "./plugins/rootview/index.js";
|
import "./plugins/rootview/index.js";
|
||||||
|
|
|
@ -1,123 +0,0 @@
|
||||||
/**
|
|
||||||
* @module converse-push
|
|
||||||
* @description
|
|
||||||
* Converse.js plugin which add support for registering
|
|
||||||
* an "App Server" as defined in XEP-0357
|
|
||||||
* @copyright 2020, the Converse.js contributors
|
|
||||||
* @license Mozilla Public License (MPLv2)
|
|
||||||
*/
|
|
||||||
import { _converse, api, converse } from "@converse/headless/core";
|
|
||||||
import log from "@converse/headless/log";
|
|
||||||
|
|
||||||
const { Strophe, $iq } = converse.env;
|
|
||||||
|
|
||||||
Strophe.addNamespace('PUSH', 'urn:xmpp:push:0');
|
|
||||||
|
|
||||||
|
|
||||||
converse.plugins.add('converse-push', {
|
|
||||||
|
|
||||||
initialize () {
|
|
||||||
/* The initialize function gets called as soon as the plugin is
|
|
||||||
* loaded by converse.js's plugin machinery.
|
|
||||||
*/
|
|
||||||
api.settings.extend({
|
|
||||||
'push_app_servers': [],
|
|
||||||
'enable_muc_push': false
|
|
||||||
});
|
|
||||||
|
|
||||||
async function disablePushAppServer (domain, push_app_server) {
|
|
||||||
if (!push_app_server.jid) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!(await api.disco.supports(Strophe.NS.PUSH, domain || _converse.bare_jid))) {
|
|
||||||
log.warn(`Not disabling push app server "${push_app_server.jid}", no disco support from your server.`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const stanza = $iq({'type': 'set'});
|
|
||||||
if (domain !== _converse.bare_jid) {
|
|
||||||
stanza.attrs({'to': domain});
|
|
||||||
}
|
|
||||||
stanza.c('disable', {
|
|
||||||
'xmlns': Strophe.NS.PUSH,
|
|
||||||
'jid': push_app_server.jid,
|
|
||||||
});
|
|
||||||
if (push_app_server.node) {
|
|
||||||
stanza.attrs({'node': push_app_server.node});
|
|
||||||
}
|
|
||||||
api.sendIQ(stanza)
|
|
||||||
.catch(e => {
|
|
||||||
log.error(`Could not disable push app server for ${push_app_server.jid}`);
|
|
||||||
log.error(e);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function enablePushAppServer (domain, push_app_server) {
|
|
||||||
if (!push_app_server.jid || !push_app_server.node) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const identity = await api.disco.getIdentity('pubsub', 'push', push_app_server.jid);
|
|
||||||
if (!identity) {
|
|
||||||
return log.warn(
|
|
||||||
`Not enabling push the service "${push_app_server.jid}", it doesn't have the right disco identtiy.`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const result = await Promise.all([
|
|
||||||
api.disco.supports(Strophe.NS.PUSH, push_app_server.jid),
|
|
||||||
api.disco.supports(Strophe.NS.PUSH, domain)
|
|
||||||
]);
|
|
||||||
if (!result[0] && !result[1]) {
|
|
||||||
log.warn(`Not enabling push app server "${push_app_server.jid}", no disco support from your server.`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
return api.sendIQ(stanza);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function enablePush (domain) {
|
|
||||||
domain = domain || _converse.bare_jid;
|
|
||||||
const push_enabled = _converse.session.get('push_enabled') || [];
|
|
||||||
if (push_enabled.includes(domain)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
const enabled = enabled_services.map(s => enablePushAppServer(domain, s));
|
|
||||||
const disabled = disabled_services.map(s => disablePushAppServer(domain, s));
|
|
||||||
try {
|
|
||||||
await Promise.all(enabled.concat(disabled));
|
|
||||||
} catch (e) {
|
|
||||||
log.error('Could not enable or disable push App Server');
|
|
||||||
if (e) log.error(e);
|
|
||||||
} finally {
|
|
||||||
push_enabled.push(domain);
|
|
||||||
}
|
|
||||||
_converse.session.save('push_enabled', push_enabled);
|
|
||||||
}
|
|
||||||
api.listen.on('statusInitialized', () => enablePush());
|
|
||||||
|
|
||||||
function onChatBoxAdded (model) {
|
|
||||||
if (model.get('type') == _converse.CHATROOMS_TYPE) {
|
|
||||||
enablePush(Strophe.getDomainFromJid(model.get('jid')));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (api.settings.get('enable_muc_push')) {
|
|
||||||
api.listen.on('chatBoxesInitialized', () => _converse.chatboxes.on('add', onChatBoxAdded));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
31
src/plugins/push/index.js
Normal file
31
src/plugins/push/index.js
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
/**
|
||||||
|
* @description
|
||||||
|
* Converse.js plugin which add support for registering
|
||||||
|
* an "App Server" as defined in XEP-0357
|
||||||
|
* @copyright 2021, the Converse.js contributors
|
||||||
|
* @license Mozilla Public License (MPLv2)
|
||||||
|
*/
|
||||||
|
import { _converse, api, converse } from '@converse/headless/core';
|
||||||
|
import { enablePush, onChatBoxAdded } from './utils.js';
|
||||||
|
|
||||||
|
const { Strophe } = converse.env;
|
||||||
|
|
||||||
|
Strophe.addNamespace('PUSH', 'urn:xmpp:push:0');
|
||||||
|
|
||||||
|
converse.plugins.add('converse-push', {
|
||||||
|
initialize () {
|
||||||
|
/* The initialize function gets called as soon as the plugin is
|
||||||
|
* loaded by converse.js's plugin machinery.
|
||||||
|
*/
|
||||||
|
api.settings.extend({
|
||||||
|
'push_app_servers': [],
|
||||||
|
'enable_muc_push': false,
|
||||||
|
});
|
||||||
|
|
||||||
|
api.listen.on('statusInitialized', () => enablePush());
|
||||||
|
|
||||||
|
if (api.settings.get('enable_muc_push')) {
|
||||||
|
api.listen.on('chatBoxesInitialized', () => _converse.chatboxes.on('add', onChatBoxAdded));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
94
src/plugins/push/utils.js
Normal file
94
src/plugins/push/utils.js
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
import log from "@converse/headless/log";
|
||||||
|
import { _converse, api, converse } from "@converse/headless/core";
|
||||||
|
|
||||||
|
const { Strophe, $iq } = converse.env;
|
||||||
|
|
||||||
|
async function disablePushAppServer (domain, push_app_server) {
|
||||||
|
if (!push_app_server.jid) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!(await api.disco.supports(Strophe.NS.PUSH, domain || _converse.bare_jid))) {
|
||||||
|
log.warn(`Not disabling push app server "${push_app_server.jid}", no disco support from your server.`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const stanza = $iq({'type': 'set'});
|
||||||
|
if (domain !== _converse.bare_jid) {
|
||||||
|
stanza.attrs({'to': domain});
|
||||||
|
}
|
||||||
|
stanza.c('disable', {
|
||||||
|
'xmlns': Strophe.NS.PUSH,
|
||||||
|
'jid': push_app_server.jid,
|
||||||
|
});
|
||||||
|
if (push_app_server.node) {
|
||||||
|
stanza.attrs({'node': push_app_server.node});
|
||||||
|
}
|
||||||
|
api.sendIQ(stanza)
|
||||||
|
.catch(e => {
|
||||||
|
log.error(`Could not disable push app server for ${push_app_server.jid}`);
|
||||||
|
log.error(e);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function enablePushAppServer (domain, push_app_server) {
|
||||||
|
if (!push_app_server.jid || !push_app_server.node) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const identity = await api.disco.getIdentity('pubsub', 'push', push_app_server.jid);
|
||||||
|
if (!identity) {
|
||||||
|
return log.warn(
|
||||||
|
`Not enabling push the service "${push_app_server.jid}", it doesn't have the right disco identtiy.`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const result = await Promise.all([
|
||||||
|
api.disco.supports(Strophe.NS.PUSH, push_app_server.jid),
|
||||||
|
api.disco.supports(Strophe.NS.PUSH, domain)
|
||||||
|
]);
|
||||||
|
if (!result[0] && !result[1]) {
|
||||||
|
log.warn(`Not enabling push app server "${push_app_server.jid}", no disco support from your server.`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
return api.sendIQ(stanza);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function enablePush (domain) {
|
||||||
|
domain = domain || _converse.bare_jid;
|
||||||
|
const push_enabled = _converse.session.get('push_enabled') || [];
|
||||||
|
if (push_enabled.includes(domain)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
const enabled = enabled_services.map(s => enablePushAppServer(domain, s));
|
||||||
|
const disabled = disabled_services.map(s => disablePushAppServer(domain, s));
|
||||||
|
try {
|
||||||
|
await Promise.all(enabled.concat(disabled));
|
||||||
|
} catch (e) {
|
||||||
|
log.error('Could not enable or disable push App Server');
|
||||||
|
if (e) log.error(e);
|
||||||
|
} finally {
|
||||||
|
push_enabled.push(domain);
|
||||||
|
}
|
||||||
|
_converse.session.save('push_enabled', push_enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function onChatBoxAdded (model) {
|
||||||
|
if (model.get('type') == _converse.CHATROOMS_TYPE) {
|
||||||
|
enablePush(Strophe.getDomainFromJid(model.get('jid')));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user