Commit 8bd72eed authored by JC Brand's avatar JC Brand

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.
parent bdbcec65
......@@ -1837,14 +1837,23 @@ _converse.api = {
/**
* Send an IQ stanza and receive a promise
* @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
* or is rejected when we receive an `error` stanza.
*/
sendIQ (stanza, timeout) {
return new Promise((resolve, reject) => {
_converse.connection.sendIQ(stanza, resolve, reject, timeout || _converse.IQ_TIMEOUT);
sendIQ (stanza, timeout, reject=true) {
timeout = timeout || _converse.IQ_TIMEOUT;
let promise;
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;
}
};
......
......@@ -653,22 +653,19 @@ converse.plugins.add('converse-roster', {
if (this.rosterVersioningSupported()) {
stanza.attrs({'ver': this.data.get('version')});
}
let iq;
try {
iq = await _converse.api.sendIQ(stanza);
} catch (e) {
_converse.log(e, Strophe.LogLevel.ERROR);
return _converse.log(
"Error while trying to fetch roster from the server",
Strophe.LogLevel.ERROR
);
}
const iq = await _converse.api.sendIQ(stanza, null, false);
if (iq.getAttribute('type') !== 'error') {
const query = sizzle(`query[xmlns="${Strophe.NS.ROSTER}"]`, iq).pop();
if (query) {
const items = sizzle(`item`, query);
items.forEach(item => this.updateContact(item));
this.data.save('version', query.getAttribute('ver'));
}
} else if (!u.isServiceUnavailableError(iq)) {
// Some unknown error happened, so we will try to fetch again if the page reloads.
_converse.log(iq, Strophe.LogLevel.ERROR);
return _converse.log("Error while trying to fetch roster from the server", Strophe.LogLevel.ERROR);
}
_converse.session.save('roster_fetched', true);
/**
* When the roster has been received from the XMPP server.
......
......@@ -156,6 +156,13 @@ u.isHeadlineMessage = function (_converse, message) {
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) {
/* Merge the second object into the first one.
*/
......
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