From b176161ecf49af6027fe5e34bb91009ff92bfd37 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Wed, 7 Aug 2019 16:05:22 +0200 Subject: [PATCH] converse-roster: Reconnection bugfixes. Don't remove cached presences in `afterTearDown` event. We might reconnect again and resume the session, in which case we need that data still. Instead, we remove cached presences when `clearSession` fires, because then we know we're not reconnecting. When reconnecting, we don't fetch presences from cache, we still have them (since we no longer remove them in `afterTearDown`). When reconnecting and not resuming, we clear the presences from cache, since we're starting a new session. --- src/headless/converse-roster.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/headless/converse-roster.js b/src/headless/converse-roster.js index 39b2e364b..e2a995b51 100644 --- a/src/headless/converse-roster.js +++ b/src/headless/converse-roster.js @@ -941,19 +941,17 @@ converse.plugins.add('converse-roster', { }); }); - _converse.api.listen.on('afterTearDown', () => { + function clearPresences () { if (_converse.presences) { - _converse.presences.each(p => { + _converse.presences.forEach(p => { p.resources.reject(r => r === undefined).forEach(r => r.destroy({'silent': true})); - p.save({'show': 'offline'}, {'silent': true}) }); + _converse.presences.clearSession(); } - }); + } _converse.api.listen.on('clearSession', () => { - if (_converse.presences) { - _converse.presences.browserStorage._clear(); - } + clearPresences(); if (_converse.shouldClearCache()) { if (_converse.roster) { _.invoke(_converse, 'roster.data.destroy'); @@ -969,16 +967,18 @@ converse.plugins.add('converse-roster', { }); _converse.api.listen.on('statusInitialized', (reconnecting) => { - if (!reconnecting) { - _converse.presences = new _converse.Presences(); - } - _converse.presences.browserStorage = - new BrowserStorage.session(`converse.presences-${_converse.bare_jid}`); - - if (_converse.haveResumed()) { - _converse.presences.fetch(); + if (reconnecting) { + // When reconnecting and not resuming a previous session, + // we clear all cached presence data, since it might be stale + // and we'll receive new presence updates + !_converse.haveResumed() && clearPresences(); } else { - _converse.presences.clearSession(); + _converse.presences = new _converse.Presences(); + const id = `converse.presences-${_converse.bare_jid}`; + _converse.presences.browserStorage = new BrowserStorage.session(id); + // We might be continuing an existing session, so we fetch + // cached presence data. + _converse.presences.fetch(); } /** * Triggered once the _converse.Presences collection has been