Commit f21abc60 authored by JC Brand's avatar JC Brand

roster: Only fetch from browser storage if already cached

parent 358187f8
...@@ -44,10 +44,6 @@ ...@@ -44,10 +44,6 @@
`<iq from="romeo@montague.lit/orchard" id="${iq.getAttribute('id')}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+ `<iq from="romeo@montague.lit/orchard" id="${iq.getAttribute('id')}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
`<query xmlns="http://jabber.org/protocol/disco#info"/></iq>`); `<query xmlns="http://jabber.org/protocol/disco#info"/></iq>`);
iq = IQ_stanzas.pop();
expect(Strophe.serialize(iq)).toBe(
`<iq id="${iq.getAttribute('id')}" type="get" xmlns="jabber:client"><query xmlns="jabber:iq:roster"/></iq>`);
iq = IQ_stanzas.pop(); iq = IQ_stanzas.pop();
expect(Strophe.serialize(iq)).toBe( expect(Strophe.serialize(iq)).toBe(
`<iq from="romeo@montague.lit/orchard" id="${iq.getAttribute('id')}" to="montague.lit" type="get" xmlns="jabber:client">`+ `<iq from="romeo@montague.lit/orchard" id="${iq.getAttribute('id')}" to="montague.lit" type="get" xmlns="jabber:client">`+
...@@ -58,6 +54,10 @@ ...@@ -58,6 +54,10 @@
`<iq from="romeo@montague.lit" id="${disco_iq.getAttribute('id')}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+ `<iq from="romeo@montague.lit" id="${disco_iq.getAttribute('id')}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
`<pubsub xmlns="http://jabber.org/protocol/pubsub"><items node="eu.siacs.conversations.axolotl.devicelist"/></pubsub></iq>`); `<pubsub xmlns="http://jabber.org/protocol/pubsub"><items node="eu.siacs.conversations.axolotl.devicelist"/></pubsub></iq>`);
iq = IQ_stanzas.pop();
expect(Strophe.serialize(iq)).toBe(
`<iq id="${iq.getAttribute('id')}" type="get" xmlns="jabber:client"><query xmlns="jabber:iq:roster"/></iq>`);
expect(sent_stanzas.filter(s => (s.nodeName === 'r')).length).toBe(2); expect(sent_stanzas.filter(s => (s.nodeName === 'r')).length).toBe(2);
expect(_converse.session.get('unacked_stanzas').length).toBe(4); expect(_converse.session.get('unacked_stanzas').length).toBe(4);
...@@ -118,9 +118,9 @@ ...@@ -118,9 +118,9 @@
`<iq from="romeo@montague.lit/orchard" id="${iq.getAttribute('id')}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+ `<iq from="romeo@montague.lit/orchard" id="${iq.getAttribute('id')}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
`<query xmlns="http://jabber.org/protocol/disco#info"/></iq>`); `<query xmlns="http://jabber.org/protocol/disco#info"/></iq>`);
iq = IQ_stanzas.pop(); // We don't fetch the roster again because it's cached.
expect(Strophe.serialize(iq)).toBe( expect(_converse.session.get('roster_fetched')).toBeFalsy();
`<iq id="${iq.getAttribute('id')}" type="get" xmlns="jabber:client"><query xmlns="jabber:iq:roster"/></iq>`); expect(IQ_stanzas.filter(iq => sizzle('query[xmlns="jabber:iq:roster"]', iq).pop()).length).toBe(0);
await _converse.api.waitUntil('statusInitialized'); await _converse.api.waitUntil('statusInitialized');
done(); done();
......
...@@ -438,33 +438,33 @@ converse.plugins.add('converse-roster', { ...@@ -438,33 +438,33 @@ converse.plugins.add('converse-roster', {
* @returns {promise} Promise which resolves once the contacts have been fetched. * @returns {promise} Promise which resolves once the contacts have been fetched.
*/ */
async fetchRosterContacts () { async fetchRosterContacts () {
let collection; if (_converse.session.get('roster_fetched')) {
try { const result = await new Promise((resolve, reject) => {
collection = await new Promise((resolve, reject) => {
this.fetch({ this.fetch({
'add': true, 'add': true,
'silent': true, 'silent': true,
'success': resolve, 'success': resolve,
'error': (m, e) => reject(e) 'error': (c, e) => reject(e)
}); });
}); });
} catch (e) { if (u.isErrorObject(result)) {
_converse.log(e, Strophe.LogLevel.ERROR); _converse.log(result, Strophe.LogLevel.ERROR);
_converse.session.set('roster_fetched', false) _converse.session.set('roster_fetched', false)
} } else {
if (_converse.session.get('roster_fetched')) { /**
/** * The contacts roster has been retrieved from the local cache (`sessionStorage`).
* The contacts roster has been retrieved from the local cache (`sessionStorage`). * @event _converse#cachedRoster
* @event _converse#cachedRoster * @type { _converse.RosterContacts }
* @type { _converse.RosterContacts } * @example _converse.api.listen.on('cachedRoster', (items) => { ... });
* @example _converse.api.listen.on('cachedRoster', (items) => { ... }); * @example _converse.api.waitUntil('cachedRoster').then(items => { ... });
* @example _converse.api.waitUntil('cachedRoster').then(items => { ... }); */
*/ _converse.api.trigger('cachedRoster', result);
_converse.api.trigger('cachedRoster', collection); return;
} else { }
_converse.send_initial_presence = true;
return _converse.roster.fetchFromServer();
} }
_converse.send_initial_presence = true;
return _converse.roster.fetchFromServer();
}, },
subscribeToSuggestedItems (msg) { subscribeToSuggestedItems (msg) {
......
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