Commit ab0521eb authored by JC Brand's avatar JC Brand

smacks: move functions out of plugin's `initialize` method

parent c9ba42d0
......@@ -14,27 +14,14 @@ const u = converse.env.utils;
Strophe.addNamespace('SM', 'urn:xmpp:sm:3');
converse.plugins.add('converse-smacks', {
initialize () {
// Configuration values for this plugin
// ====================================
// Refer to docs/source/configuration.rst for explanations of these
// configuration settings.
api.settings.extend({
'enable_smacks': true,
'smacks_max_unacked_stanzas': 5,
});
function isStreamManagementSupported () {
function isStreamManagementSupported () {
if (api.connection.isType('bosh') && !_converse.isTestEnv()) {
return false;
}
return api.disco.stream.getFeature('sm', Strophe.NS.SM);
}
}
function handleAck (el) {
function handleAck (el) {
if (!_converse.session.get('smacks_enabled')) {
return true;
}
......@@ -62,18 +49,18 @@ converse.plugins.add('converse-smacks', {
'unacked_stanzas': unacked_stanzas.slice(delta)
});
return true;
}
}
function sendAck() {
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}"/>`);
api.send(stanza);
}
return true;
}
}
function stanzaHandler (el) {
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');
......@@ -81,9 +68,9 @@ converse.plugins.add('converse-smacks', {
}
}
return true;
}
}
function initSessionData () {
function initSessionData () {
_converse.session.save({
'smacks_enabled': _converse.session.get('smacks_enabled') || false,
'num_stanzas_handled': _converse.session.get('num_stanzas_handled') || 0,
......@@ -91,9 +78,9 @@ converse.plugins.add('converse-smacks', {
'num_stanzas_since_last_ack': _converse.session.get('num_stanzas_since_last_ack') || 0,
'unacked_stanzas': _converse.session.get('unacked_stanzas') || []
});
}
}
function resetSessionData () {
function resetSessionData () {
_converse.session && _converse.session.save({
'smacks_enabled': false,
'num_stanzas_handled': 0,
......@@ -101,18 +88,18 @@ converse.plugins.add('converse-smacks', {
'num_stanzas_since_last_ack': 0,
'unacked_stanzas': []
});
}
}
function saveSessionData (el) {
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) {
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.
......@@ -133,9 +120,9 @@ converse.plugins.add('converse-smacks', {
*/
api.trigger('streamResumptionFailed');
return true;
}
}
function resendUnackedStanzas () {
function resendUnackedStanzas () {
const stanzas = _converse.session.get('unacked_stanzas');
// We clear the unacked_stanzas array because it'll get populated
// again in `onStanzaSent`
......@@ -158,9 +145,9 @@ converse.plugins.add('converse-smacks', {
// differently, more like push stanzas, so that they don't need
// explicit handlers.
stanzas.forEach(s => api.send(s));
}
}
function onResumedStanza (el) {
function onResumedStanza (el) {
saveSessionData(el);
handleAck(el);
resendUnackedStanzas();
......@@ -168,9 +155,9 @@ converse.plugins.add('converse-smacks', {
_converse.connection.authenticated = true;
_converse.connection.restored = true;
_converse.connection._changeConnectStatus(Strophe.Status.CONNECTED, null);
}
}
async function sendResumeStanza () {
async function sendResumeStanza () {
const promise = u.getResolveablePromise();
_converse.connection._addSysHandler(el => promise.resolve(onResumedStanza(el)), Strophe.NS.SM, 'resumed');
_converse.connection._addSysHandler(el => promise.resolve(onFailedStanza(el)), Strophe.NS.SM, 'failed');
......@@ -181,9 +168,9 @@ converse.plugins.add('converse-smacks', {
api.send(stanza);
_converse.connection.flush();
await promise;
}
}
async function sendEnableStanza () {
async function sendEnableStanza () {
if (!api.settings.get('enable_smacks') || _converse.session.get('smacks_enabled')) {
return;
}
......@@ -198,9 +185,9 @@ converse.plugins.add('converse-smacks', {
_converse.connection.flush();
await promise;
}
}
}
async function enableStreamManagement () {
async function enableStreamManagement () {
if (!api.settings.get('enable_smacks')) {
return;
}
......@@ -215,9 +202,9 @@ converse.plugins.add('converse-smacks', {
} else {
resetSessionData();
}
}
}
function onStanzaSent (stanza) {
function onStanzaSent (stanza) {
if (!_converse.session) {
log.warn('No _converse.session!');
return;
......@@ -244,11 +231,24 @@ converse.plugins.add('converse-smacks', {
_converse.session.save({'num_stanzas_since_last_ack': num});
}
}
}
}
api.listen.on('userSessionInitialized', initSessionData);
api.listen.on('beforeResourceBinding', enableStreamManagement);
converse.plugins.add('converse-smacks', {
initialize () {
// Configuration values for this plugin
// ====================================
// Refer to docs/source/configuration.rst for explanations of these
// configuration settings.
api.settings.extend({
'enable_smacks': true,
'smacks_max_unacked_stanzas': 5,
});
api.listen.on('afterResourceBinding', sendEnableStanza);
api.listen.on('beforeResourceBinding', enableStreamManagement);
api.listen.on('send', onStanzaSent);
api.listen.on('userSessionInitialized', initSessionData);
}
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment