Browse Source

Refetch the roster from the server after reconnection.

From the perspective of the XMPP server, this is an entirely new login,
and therefore as per RFC-6121 the roster SHOULD be queried, making the
client an "interested resource".

https://tools.ietf.org/html/rfc6121#section-2
JC Brand 7 years ago
parent
commit
4f8a09b019
1 changed files with 37 additions and 23 deletions
  1. 37 23
      src/converse-core.js

+ 37 - 23
src/converse-core.js

@@ -736,17 +736,31 @@
             _converse.emit('rosterInitialized');
             _converse.emit('rosterInitialized');
         };
         };
 
 
-        this.populateRoster = function () {
+        this.populateRoster = function (ignore_cache=false) {
             /* Fetch all the roster groups, and then the roster contacts.
             /* Fetch all the roster groups, and then the roster contacts.
              * Emit an event after fetching is done in each case.
              * Emit an event after fetching is done in each case.
+             *
+             * Parameters:
+             *    (Bool) ignore_cache - If set to to true, the local cache
+             *      will be ignored it's guaranteed that the XMPP server
+             *      will be queried for the roster.
              */
              */
-            _converse.rostergroups.fetchRosterGroups().then(function () {
-                _converse.emit('rosterGroupsFetched');
-                _converse.roster.fetchRosterContacts().then(function () {
+            if (ignore_cache) {
+                _converse.send_initial_presence = true;
+                _converse.roster.fetchFromServer()
+                    .then(_converse.sendInitialPresence)
+                    .catch(_converse.sendInitialPresence);
+            } else {
+                _converse.rostergroups.fetchRosterGroups().then(() => {
+                    _converse.emit('rosterGroupsFetched');
+                    return _converse.roster.fetchRosterContacts();
+                }).then(() => {
                     _converse.emit('rosterContactsFetched');
                     _converse.emit('rosterContactsFetched');
                     _converse.sendInitialPresence();
                     _converse.sendInitialPresence();
+                }).catch(() => {
+                    _converse.sendInitialPresence();
                 });
                 });
-            });
+            }
         };
         };
 
 
         this.unregisterPresenceHandler = function () {
         this.unregisterPresenceHandler = function () {
@@ -788,11 +802,9 @@
                 _converse.initRoster();
                 _converse.initRoster();
             }
             }
             _converse.roster.onConnected();
             _converse.roster.onConnected();
-            _converse.populateRoster();
+            _converse.populateRoster(reconnecting);
             _converse.registerPresenceHandler();
             _converse.registerPresenceHandler();
-            if (reconnecting) {
-                _converse.xmppstatus.sendPresence();
-            } else {
+            if (!reconnecting) {
                 init_promise.resolve();
                 init_promise.resolve();
                 _converse.emit('initialized');
                 _converse.emit('initialized');
             }
             }
@@ -1086,14 +1098,8 @@
                         add: true,
                         add: true,
                         success (collection) {
                         success (collection) {
                             if (collection.length === 0) {
                             if (collection.length === 0) {
-                                /* We don't have any roster contacts stored in sessionStorage,
-                                * so lets fetch the roster from the XMPP server. We pass in
-                                * 'sendPresence' as callback method, because after initially
-                                * fetching the roster we are ready to receive presence
-                                * updates from our contacts.
-                                */
                                 _converse.send_initial_presence = true;
                                 _converse.send_initial_presence = true;
-                                _converse.roster.fetchFromServer(resolve);
+                                _converse.roster.fetchFromServer().then(resolve).catch(reject);
                             } else {
                             } else {
                                 _converse.emit('cachedRoster', collection);
                                 _converse.emit('cachedRoster', collection);
                                 resolve();
                                 resolve();
@@ -1244,13 +1250,21 @@
                 return true;
                 return true;
             },
             },
 
 
-            fetchFromServer (callback) {
-                /* Get the roster from the XMPP server */
-                const iq = $iq({type: 'get', 'id': _converse.connection.getUniqueId('roster')})
-                    .c('query', {xmlns: Strophe.NS.ROSTER});
-                return _converse.connection.sendIQ(iq, (iq) => {
-                    this.onReceivedFromServer(iq);
-                    callback.apply(this, arguments);
+            fetchFromServer () {
+                /* Fetch the roster from the XMPP server */
+                return new Promise((resolve, reject) => {
+                    const iq = $iq({
+                        'type': 'get',
+                        'id': _converse.connection.getUniqueId('roster')
+                    }).c('query', {xmlns: Strophe.NS.ROSTER});
+
+                    const callback = _.flow(this.onReceivedFromServer.bind(this), resolve);
+                    const errback = function (iq) {
+                        const errmsg = "Error while trying to fetch roster from the server";
+                        _converse.log(errmsg, Strophe.LogLevel.ERROR);
+                        reject(new Error(errmsg));
+                    }
+                    return _converse.connection.sendIQ(iq, callback, errback);
                 });
                 });
             },
             },