2019-07-11 10:48:52 +02:00
|
|
|
/**
|
|
|
|
* @module converse-pubsub
|
2020-01-26 16:21:20 +01:00
|
|
|
* @copyright The Converse.js contributors
|
2019-09-19 16:54:55 +02:00
|
|
|
* @license Mozilla Public License (MPLv2)
|
2019-07-11 10:48:52 +02:00
|
|
|
*/
|
2018-12-19 14:38:16 +01:00
|
|
|
import "./converse-disco";
|
2020-05-18 10:54:37 +02:00
|
|
|
import { _converse, api, converse } from "./converse-core";
|
2019-11-06 11:01:34 +01:00
|
|
|
import log from "./log";
|
2018-12-19 14:38:16 +01:00
|
|
|
|
2019-10-09 16:01:38 +02:00
|
|
|
const { Strophe, $iq } = converse.env;
|
2018-12-19 14:38:16 +01:00
|
|
|
|
|
|
|
Strophe.addNamespace('PUBSUB_ERROR', Strophe.NS.PUBSUB+"#errors");
|
|
|
|
|
|
|
|
|
|
|
|
converse.plugins.add('converse-pubsub', {
|
|
|
|
|
|
|
|
dependencies: ["converse-disco"],
|
|
|
|
|
|
|
|
initialize () {
|
|
|
|
|
|
|
|
/************************ BEGIN API ************************/
|
|
|
|
// We extend the default converse.js API to add methods specific to MUC groupchats.
|
2019-04-29 09:07:15 +02:00
|
|
|
Object.assign(_converse.api, {
|
2018-12-19 14:38:16 +01:00
|
|
|
/**
|
|
|
|
* The "pubsub" namespace groups methods relevant to PubSub
|
|
|
|
*
|
|
|
|
* @namespace _converse.api.pubsub
|
|
|
|
* @memberOf _converse.api
|
|
|
|
*/
|
|
|
|
'pubsub': {
|
|
|
|
/**
|
|
|
|
* Publshes an item to a PubSub node
|
|
|
|
*
|
|
|
|
* @method _converse.api.pubsub.publish
|
|
|
|
* @param {string} jid The JID of the pubsub service where the node resides.
|
|
|
|
* @param {string} node The node being published to
|
|
|
|
* @param {Strophe.Builder} item The Strophe.Builder representation of the XML element being published
|
|
|
|
* @param {object} options An object representing the publisher options
|
2018-12-21 04:22:00 +01:00
|
|
|
* (see https://xmpp.org/extensions/xep-0060.html#publisher-publish-options)
|
|
|
|
* @param {boolean} strict_options Indicates whether the publisher
|
|
|
|
* options are a strict requirement or not. If they're NOT
|
|
|
|
* strict, then Converse will publish to the node even if
|
|
|
|
* the publish options precondication cannot be met.
|
2018-12-19 14:38:16 +01:00
|
|
|
*/
|
2018-12-21 04:22:00 +01:00
|
|
|
async 'publish' (jid, node, item, options, strict_options=true) {
|
2018-12-19 14:38:16 +01:00
|
|
|
const stanza = $iq({
|
|
|
|
'from': _converse.bare_jid,
|
|
|
|
'type': 'set',
|
|
|
|
'to': jid
|
|
|
|
}).c('pubsub', {'xmlns': Strophe.NS.PUBSUB})
|
|
|
|
.c('publish', {'node': node})
|
|
|
|
.cnode(item.tree()).up().up();
|
|
|
|
|
|
|
|
if (options) {
|
|
|
|
jid = jid || _converse.bare_jid;
|
2020-03-31 13:15:57 +02:00
|
|
|
if (await api.disco.supports(Strophe.NS.PUBSUB + '#publish-options', jid)) {
|
2018-12-19 14:38:16 +01:00
|
|
|
stanza.c('publish-options')
|
|
|
|
.c('x', {'xmlns': Strophe.NS.XFORM, 'type': 'submit'})
|
|
|
|
.c('field', {'var': 'FORM_TYPE', 'type': 'hidden'})
|
2018-12-21 04:22:00 +01:00
|
|
|
.c('value').t(`${Strophe.NS.PUBSUB}#publish-options`).up().up()
|
2018-12-19 14:38:16 +01:00
|
|
|
|
|
|
|
Object.keys(options).forEach(k => stanza.c('field', {'var': k}).c('value').t(options[k]).up().up());
|
|
|
|
} else {
|
2019-11-06 11:01:34 +01:00
|
|
|
log.warn(`_converse.api.publish: ${jid} does not support #publish-options, `+
|
|
|
|
`so we didn't set them even though they were provided.`)
|
2018-12-19 14:38:16 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-21 04:22:00 +01:00
|
|
|
try {
|
2020-09-18 12:55:24 +02:00
|
|
|
await api.sendIQ(stanza);
|
2018-12-21 04:22:00 +01:00
|
|
|
} catch (iq) {
|
|
|
|
if (iq instanceof Element &&
|
|
|
|
strict_options &&
|
|
|
|
iq.querySelector(`precondition-not-met[xmlns="${Strophe.NS.PUBSUB_ERROR}"]`)) {
|
|
|
|
|
|
|
|
// The publish-options precondition couldn't be
|
|
|
|
// met. We re-publish but without publish-options.
|
|
|
|
const el = stanza.nodeTree;
|
|
|
|
el.querySelector('publish-options').outerHTML = '';
|
2019-11-06 11:01:34 +01:00
|
|
|
log.warn(`PubSub: Republishing without publish options. ${el.outerHTML}`);
|
2020-09-18 12:55:24 +02:00
|
|
|
await api.sendIQ(el);
|
2018-12-21 04:22:00 +01:00
|
|
|
} else {
|
|
|
|
throw iq;
|
|
|
|
}
|
|
|
|
}
|
2018-12-19 14:38:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
/************************ END API ************************/
|
|
|
|
}
|
|
|
|
});
|