2019-07-11 10:48:52 +02:00
|
|
|
/**
|
|
|
|
* @module converse-smacks
|
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)
|
|
|
|
* @description Converse.js plugin which adds support for XEP-0198: Stream Management
|
2019-07-11 10:48:52 +02:00
|
|
|
*/
|
2020-04-23 13:22:31 +02:00
|
|
|
import { converse } from "./converse-core";
|
2019-11-06 11:01:34 +01:00
|
|
|
import log from "./log";
|
2018-05-29 12:00:23 +02:00
|
|
|
|
2019-11-06 11:01:34 +01:00
|
|
|
const { Strophe } = converse.env;
|
2018-05-29 12:00:23 +02:00
|
|
|
const u = converse.env.utils;
|
|
|
|
|
2019-10-09 16:01:38 +02:00
|
|
|
|
2018-05-29 12:00:23 +02:00
|
|
|
Strophe.addNamespace('SM', 'urn:xmpp:sm:3');
|
|
|
|
|
|
|
|
|
|
|
|
converse.plugins.add('converse-smacks', {
|
|
|
|
|
|
|
|
initialize () {
|
|
|
|
const { _converse } = this;
|
2020-03-31 13:15:57 +02:00
|
|
|
const { api } = _converse;
|
2018-05-29 12:00:23 +02:00
|
|
|
|
|
|
|
// Configuration values for this plugin
|
|
|
|
// ====================================
|
|
|
|
// Refer to docs/source/configuration.rst for explanations of these
|
|
|
|
// configuration settings.
|
2020-03-31 13:15:57 +02:00
|
|
|
api.settings.update({
|
2019-12-03 13:54:28 +01:00
|
|
|
'enable_smacks': true,
|
2018-05-29 12:00:23 +02:00
|
|
|
'smacks_max_unacked_stanzas': 5,
|
|
|
|
});
|
|
|
|
|
|
|
|
function isStreamManagementSupported () {
|
2020-03-31 13:15:57 +02:00
|
|
|
if (api.connection.isType('bosh') && !_converse.isTestEnv()) {
|
2019-09-20 10:39:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
return api.disco.stream.getFeature('sm', Strophe.NS.SM);
|
2018-05-29 12:00:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleAck (el) {
|
|
|
|
if (!_converse.session.get('smacks_enabled')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const handled = parseInt(el.getAttribute('h'), 10);
|
|
|
|
const last_known_handled = _converse.session.get('num_stanzas_handled_by_server');
|
|
|
|
const delta = handled - last_known_handled;
|
|
|
|
|
|
|
|
if (delta < 0) {
|
|
|
|
const err_msg = `New reported stanza count lower than previous. `+
|
|
|
|
`New: ${handled} - Previous: ${last_known_handled}`
|
2019-11-06 11:01:34 +01:00
|
|
|
log.error(err_msg);
|
2018-05-29 12:00:23 +02:00
|
|
|
}
|
|
|
|
const unacked_stanzas = _converse.session.get('unacked_stanzas');
|
|
|
|
if (delta > unacked_stanzas.length) {
|
|
|
|
const err_msg =
|
|
|
|
`Higher reported acknowledge count than unacknowledged stanzas. `+
|
|
|
|
`Reported Acknowledged Count: ${delta} -`+
|
|
|
|
`Unacknowledged Stanza Count: ${unacked_stanzas.length} -`+
|
|
|
|
`New: ${handled} - Previous: ${last_known_handled}`
|
2019-11-06 11:01:34 +01:00
|
|
|
log.error(err_msg);
|
2018-05-29 12:00:23 +02:00
|
|
|
}
|
|
|
|
_converse.session.save({
|
|
|
|
'num_stanzas_handled_by_server': handled,
|
|
|
|
'num_stanzas_since_last_ack': 0,
|
|
|
|
'unacked_stanzas': unacked_stanzas.slice(delta)
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendAck() {
|
|
|
|
if (_converse.session.get('smacks_enabled')) {
|
|
|
|
const h = _converse.session.get('num_stanzas_handled');
|
|
|
|
const stanza = u.toStanza(`<a xmlns="${Strophe.NS.SM}" h="${h}"/>`);
|
2020-03-31 13:15:57 +02:00
|
|
|
api.send(stanza);
|
2018-05-29 12:00:23 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function stanzaHandler (el) {
|
|
|
|
if (_converse.session.get('smacks_enabled')) {
|
|
|
|
if (u.isTagEqual(el, 'iq') || u.isTagEqual(el, 'presence') || u.isTagEqual(el, 'message')) {
|
|
|
|
const h = _converse.session.get('num_stanzas_handled');
|
|
|
|
_converse.session.save('num_stanzas_handled', h+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-25 23:58:18 +02:00
|
|
|
function initSessionData () {
|
|
|
|
_converse.session.save({
|
|
|
|
'smacks_enabled': _converse.session.get('smacks_enabled') || false,
|
|
|
|
'num_stanzas_handled': _converse.session.get('num_stanzas_handled') || 0,
|
|
|
|
'num_stanzas_handled_by_server': _converse.session.get('num_stanzas_handled_by_server') || 0,
|
|
|
|
'num_stanzas_since_last_ack': _converse.session.get('num_stanzas_since_last_ack') || 0,
|
|
|
|
'unacked_stanzas': _converse.session.get('unacked_stanzas') || []
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetSessionData () {
|
2019-09-20 10:39:44 +02:00
|
|
|
_converse.session && _converse.session.save({
|
2018-05-29 12:00:23 +02:00
|
|
|
'smacks_enabled': false,
|
|
|
|
'num_stanzas_handled': 0,
|
|
|
|
'num_stanzas_handled_by_server': 0,
|
|
|
|
'num_stanzas_since_last_ack': 0,
|
|
|
|
'unacked_stanzas': []
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveSessionData (el) {
|
|
|
|
const data = {'smacks_enabled': true};
|
|
|
|
if (['1', 'true'].includes(el.getAttribute('resume'))) {
|
|
|
|
data['smacks_stream_id'] = el.getAttribute('id');
|
|
|
|
}
|
|
|
|
_converse.session.save(data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onFailedStanza (el) {
|
|
|
|
if (el.querySelector('item-not-found')) {
|
|
|
|
// Stream resumption must happen before resource binding but
|
|
|
|
// enabling a new stream must happen after resource binding.
|
|
|
|
// Since resumption failed, we simply continue.
|
|
|
|
//
|
|
|
|
// After resource binding, sendEnableStanza will be called
|
|
|
|
// based on the afterResourceBinding event.
|
2019-11-06 11:01:34 +01:00
|
|
|
log.warn('Could not resume previous SMACKS session, session id not found. '+
|
|
|
|
'A new session will be established.');
|
2018-05-29 12:00:23 +02:00
|
|
|
} else {
|
2019-11-06 11:01:34 +01:00
|
|
|
log.error('Failed to enable stream management');
|
|
|
|
log.error(el.outerHTML);
|
2018-05-29 12:00:23 +02:00
|
|
|
}
|
2019-06-25 23:58:18 +02:00
|
|
|
resetSessionData();
|
2019-10-15 14:24:01 +02:00
|
|
|
/**
|
|
|
|
* Triggered when the XEP-0198 stream could not be resumed.
|
|
|
|
* @event _converse#streamResumptionFailed
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.trigger('streamResumptionFailed');
|
2018-05-29 12:00:23 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function resendUnackedStanzas () {
|
|
|
|
const stanzas = _converse.session.get('unacked_stanzas');
|
|
|
|
// We clear the unacked_stanzas array because it'll get populated
|
|
|
|
// again in `onStanzaSent`
|
|
|
|
_converse.session.save('unacked_stanzas', []);
|
|
|
|
|
|
|
|
// XXX: Currently we're resending *all* unacked stanzas, including
|
|
|
|
// IQ[type="get"] stanzas that longer have handlers (because the
|
|
|
|
// page reloaded or we reconnected, causing removal of handlers).
|
|
|
|
//
|
|
|
|
// *Side-note:* Is it necessary to clear handlers upon reconnection?
|
|
|
|
//
|
|
|
|
// I've considered not resending those stanzas, but then keeping
|
|
|
|
// track of what's been sent and ack'd and their order gets
|
|
|
|
// prohibitively complex.
|
|
|
|
//
|
|
|
|
// It's unclear how much of a problem this poses.
|
|
|
|
//
|
|
|
|
// Two possible solutions are running @converse/headless as a
|
|
|
|
// service worker or handling IQ[type="result"] stanzas
|
|
|
|
// differently, more like push stanzas, so that they don't need
|
|
|
|
// explicit handlers.
|
2020-03-31 13:15:57 +02:00
|
|
|
stanzas.forEach(s => api.send(s));
|
2018-05-29 12:00:23 +02:00
|
|
|
}
|
|
|
|
|
2019-10-09 16:01:38 +02:00
|
|
|
function onResumedStanza (el) {
|
2018-05-29 12:00:23 +02:00
|
|
|
saveSessionData(el);
|
|
|
|
handleAck(el);
|
|
|
|
resendUnackedStanzas();
|
|
|
|
_converse.connection.do_bind = false; // No need to bind our resource anymore
|
|
|
|
_converse.connection.authenticated = true;
|
2019-06-13 13:29:55 +02:00
|
|
|
_converse.connection.restored = true;
|
2018-05-29 12:00:23 +02:00
|
|
|
_converse.connection._changeConnectStatus(Strophe.Status.CONNECTED, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sendResumeStanza () {
|
|
|
|
const promise = u.getResolveablePromise();
|
2019-10-09 16:01:38 +02:00
|
|
|
_converse.connection._addSysHandler(el => promise.resolve(onResumedStanza(el)), Strophe.NS.SM, 'resumed');
|
|
|
|
_converse.connection._addSysHandler(el => promise.resolve(onFailedStanza(el)), Strophe.NS.SM, 'failed');
|
2018-05-29 12:00:23 +02:00
|
|
|
|
|
|
|
const previous_id = _converse.session.get('smacks_stream_id');
|
2019-06-13 13:54:58 +02:00
|
|
|
const h = _converse.session.get('num_stanzas_handled');
|
2018-05-29 12:00:23 +02:00
|
|
|
const stanza = u.toStanza(`<resume xmlns="${Strophe.NS.SM}" h="${h}" previd="${previous_id}"/>`);
|
2020-03-31 13:15:57 +02:00
|
|
|
api.send(stanza);
|
2018-05-29 12:00:23 +02:00
|
|
|
_converse.connection.flush();
|
|
|
|
await promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sendEnableStanza () {
|
2020-03-31 13:15:57 +02:00
|
|
|
if (!api.settings.get('enable_smacks') || _converse.session.get('smacks_enabled')) {
|
2018-05-29 12:00:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (await isStreamManagementSupported()) {
|
|
|
|
const promise = u.getResolveablePromise();
|
2019-10-09 16:01:38 +02:00
|
|
|
_converse.connection._addSysHandler(el => promise.resolve(saveSessionData(el)), Strophe.NS.SM, 'enabled');
|
|
|
|
_converse.connection._addSysHandler(el => promise.resolve(onFailedStanza(el)), Strophe.NS.SM, 'failed');
|
2018-05-29 12:00:23 +02:00
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
const resume = (api.connection.isType('websocket') || _converse.isTestEnv());
|
2019-06-14 11:28:31 +02:00
|
|
|
const stanza = u.toStanza(`<enable xmlns="${Strophe.NS.SM}" resume="${resume}"/>`);
|
2020-03-31 13:15:57 +02:00
|
|
|
api.send(stanza);
|
2018-05-29 12:00:23 +02:00
|
|
|
_converse.connection.flush();
|
|
|
|
await promise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function enableStreamManagement () {
|
2020-03-31 13:15:57 +02:00
|
|
|
if (!api.settings.get('enable_smacks')) {
|
2018-05-29 12:00:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(await isStreamManagementSupported())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_converse.connection.addHandler(stanzaHandler);
|
|
|
|
_converse.connection.addHandler(sendAck, Strophe.NS.SM, 'r');
|
|
|
|
_converse.connection.addHandler(handleAck, Strophe.NS.SM, 'a');
|
2019-09-20 10:39:44 +02:00
|
|
|
if (_converse.session.get('smacks_stream_id')) {
|
2018-05-29 12:00:23 +02:00
|
|
|
await sendResumeStanza();
|
|
|
|
} else {
|
2019-06-25 23:58:18 +02:00
|
|
|
resetSessionData();
|
2018-05-29 12:00:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onStanzaSent (stanza) {
|
|
|
|
if (!_converse.session) {
|
2019-11-06 11:01:34 +01:00
|
|
|
log.warn('No _converse.session!');
|
2018-05-29 12:00:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!_converse.session.get('smacks_enabled')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (u.isTagEqual(stanza, 'iq') ||
|
|
|
|
u.isTagEqual(stanza, 'presence') ||
|
|
|
|
u.isTagEqual(stanza, 'message')) {
|
|
|
|
|
|
|
|
const stanza_string = Strophe.serialize(stanza);
|
|
|
|
_converse.session.save(
|
|
|
|
'unacked_stanzas',
|
2019-06-25 22:53:39 +02:00
|
|
|
(_converse.session.get('unacked_stanzas') || []).concat([stanza_string])
|
2018-05-29 12:00:23 +02:00
|
|
|
);
|
2020-03-31 13:15:57 +02:00
|
|
|
const max_unacked = api.settings.get('smacks_max_unacked_stanzas');
|
2018-05-29 12:00:23 +02:00
|
|
|
if (max_unacked > 0) {
|
|
|
|
const num = _converse.session.get('num_stanzas_since_last_ack') + 1;
|
|
|
|
if (num % max_unacked === 0) {
|
|
|
|
// Request confirmation of sent stanzas
|
2020-03-31 13:15:57 +02:00
|
|
|
api.send(u.toStanza(`<r xmlns="${Strophe.NS.SM}"/>`));
|
2018-05-29 12:00:23 +02:00
|
|
|
}
|
|
|
|
_converse.session.save({'num_stanzas_since_last_ack': num});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('userSessionInitialized', initSessionData);
|
|
|
|
api.listen.on('beforeResourceBinding', enableStreamManagement);
|
|
|
|
api.listen.on('afterResourceBinding', sendEnableStanza);
|
|
|
|
api.listen.on('send', onStanzaSent);
|
2018-05-29 12:00:23 +02:00
|
|
|
}
|
|
|
|
});
|