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.
This commit is contained in:
JC Brand 2019-08-07 16:05:22 +02:00
parent 3dc2b2b695
commit b176161ecf

View File

@ -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