Don't keep on refetching roster if the service is unavailable

`sendIQ` now takes a flag to turn of rejection of the promise and to
resolve on error IQs instead.
This commit is contained in:
JC Brand 2019-07-29 14:32:57 +02:00
parent bdbcec65c6
commit 8bd72eed72
3 changed files with 33 additions and 20 deletions

View File

@ -1837,14 +1837,23 @@ _converse.api = {
/** /**
* Send an IQ stanza and receive a promise * Send an IQ stanza and receive a promise
* @method _converse.api.sendIQ * @method _converse.api.sendIQ
* @param { XMLElement } stanza
* @param { Integer } timeout
* @param { Boolean } reject - Whether an error IQ should cause the promise
* to be rejected. If `false`, the promise will resolve instead of being rejected.
* @returns {Promise} A promise which resolves when we receive a `result` stanza * @returns {Promise} A promise which resolves when we receive a `result` stanza
* or is rejected when we receive an `error` stanza. * or is rejected when we receive an `error` stanza.
*/ */
sendIQ (stanza, timeout) { sendIQ (stanza, timeout, reject=true) {
return new Promise((resolve, reject) => { timeout = timeout || _converse.IQ_TIMEOUT;
_converse.connection.sendIQ(stanza, resolve, reject, timeout || _converse.IQ_TIMEOUT); let promise;
_converse.api.trigger('send', stanza); if (reject) {
}); promise = new Promise((resolve, reject) => _converse.connection.sendIQ(stanza, resolve, reject, timeout));
} else {
promise = new Promise((resolve, reject) => _converse.connection.sendIQ(stanza, resolve, resolve, timeout));
}
_converse.api.trigger('send', stanza);
return promise;
} }
}; };

View File

@ -653,21 +653,18 @@ converse.plugins.add('converse-roster', {
if (this.rosterVersioningSupported()) { if (this.rosterVersioningSupported()) {
stanza.attrs({'ver': this.data.get('version')}); stanza.attrs({'ver': this.data.get('version')});
} }
let iq; const iq = await _converse.api.sendIQ(stanza, null, false);
try { if (iq.getAttribute('type') !== 'error') {
iq = await _converse.api.sendIQ(stanza); const query = sizzle(`query[xmlns="${Strophe.NS.ROSTER}"]`, iq).pop();
} catch (e) { if (query) {
_converse.log(e, Strophe.LogLevel.ERROR); const items = sizzle(`item`, query);
return _converse.log( items.forEach(item => this.updateContact(item));
"Error while trying to fetch roster from the server", this.data.save('version', query.getAttribute('ver'));
Strophe.LogLevel.ERROR }
); } else if (!u.isServiceUnavailableError(iq)) {
} // Some unknown error happened, so we will try to fetch again if the page reloads.
const query = sizzle(`query[xmlns="${Strophe.NS.ROSTER}"]`, iq).pop(); _converse.log(iq, Strophe.LogLevel.ERROR);
if (query) { return _converse.log("Error while trying to fetch roster from the server", Strophe.LogLevel.ERROR);
const items = sizzle(`item`, query);
items.forEach(item => this.updateContact(item));
this.data.save('version', query.getAttribute('ver'));
} }
_converse.session.save('roster_fetched', true); _converse.session.save('roster_fetched', true);
/** /**

View File

@ -156,6 +156,13 @@ u.isHeadlineMessage = function (_converse, message) {
return false; return false;
}; };
u.isServiceUnavailableError = function (stanza) {
if (!_.isElement(stanza)) {
return false;
}
return sizzle(`error[type="cancel"] service-unavailable[xmlns="${Strophe.NS.STANZAS}"]`, stanza).length > 0;
}
u.merge = function merge (first, second) { u.merge = function merge (first, second) {
/* Merge the second object into the first one. /* Merge the second object into the first one.
*/ */