Commit 9fe6087e authored by JC Brand's avatar JC Brand

Simplify the logic of when to send out a presence stanza.

Updates #521 and #536.

Turn the logic around so that a presence stanza is always sent out, except in
the single case where we know for sure that the session was restored, in which
case a presence stanza is not required becaus we "never left".
parent 9d7b41cd
...@@ -421,12 +421,15 @@ ...@@ -421,12 +421,15 @@
// Module-level variables // Module-level variables
// ---------------------- // ----------------------
this.callback = callback || function () {}; this.callback = callback || function () {};
// This var is used to detect when the session was disconnected, /* When reloading the page:
// so that we can send out a new presence stanza. Otherwise * For new sessions, we need to send out a presence stanza to notify
// it won't be sent out due to roster contacts already being * the server/network that we're online.
// in sessionStorage. * When re-attaching to an existing session (e.g. via the keepalive
// https://github.com/jcbrand/converse.js/issues/521 * option), we don't need to again send out a presence stanza, because
this.initial_presence_sent = false; * it's as if "we never left" (see onConnectStatusChanged).
* https://github.com/jcbrand/converse.js/issues/521
*/
this.send_initial_presence = true;
this.msg_counter = 0; this.msg_counter = 0;
// Module-level functions // Module-level functions
...@@ -629,12 +632,18 @@ ...@@ -629,12 +632,18 @@
this.onConnectStatusChanged = function (status, condition, reconnect) { this.onConnectStatusChanged = function (status, condition, reconnect) {
converse.log("Status changed to: "+PRETTY_CONNECTION_STATUS[status]); converse.log("Status changed to: "+PRETTY_CONNECTION_STATUS[status]);
if (status === Strophe.Status.CONNECTED || status === Strophe.Status.ATTACHED) { if (status === Strophe.Status.CONNECTED || status === Strophe.Status.ATTACHED) {
// By default we always want to send out an initial presence stanza.
converse.send_initial_presence = true;
delete converse.disconnection_cause; delete converse.disconnection_cause;
if ((typeof reconnect !== 'undefined') && (reconnect)) { if ((typeof reconnect !== 'undefined') && (reconnect)) {
converse.log(status === Strophe.Status.CONNECTED ? 'Reconnected' : 'Reattached'); converse.log(status === Strophe.Status.CONNECTED ? 'Reconnected' : 'Reattached');
converse.onReconnected(); converse.onReconnected();
} else { } else {
converse.log(status === Strophe.Status.CONNECTED ? 'Connected' : 'Attached'); converse.log(status === Strophe.Status.CONNECTED ? 'Connected' : 'Attached');
if (converse.connection.restored) {
converse.send_initial_presence = false; // No need to send an initial presence stanza when
// we're restoring an existing session.
}
converse.onConnected(); converse.onConnected();
} }
} else if (status === Strophe.Status.DISCONNECTED) { } else if (status === Strophe.Status.DISCONNECTED) {
...@@ -4944,18 +4953,15 @@ ...@@ -4944,18 +4953,15 @@
* updates from our contacts. * updates from our contacts.
*/ */
converse.roster.fetchFromServer( converse.roster.fetchFromServer(
converse.xmppstatus.sendInitialPresence.bind(converse.xmppstatus) converse.xmppstatus.sendPresence.bind(converse.xmppstatus)
); );
} else if (converse.connection._proto instanceof Strophe.Websocket || } else if (converse.send_initial_presence) {
!converse.keepalive || /* We're not going to fetch the roster again because we have
!converse.initial_presence_sent
) {
/* We're not going to fetch the roster again (because we have
* it already cached in sessionStorage, but we still need to * it already cached in sessionStorage, but we still need to
* send out our presence because this is a new session. * send out a presence stanza because this is a new session.
* See: https://github.com/jcbrand/converse.js/issues/536 * See: https://github.com/jcbrand/converse.js/issues/536
*/ */
converse.xmppstatus.sendInitialPresence(); converse.xmppstatus.sendPresence();
} }
} }
}); });
...@@ -5288,11 +5294,6 @@ ...@@ -5288,11 +5294,6 @@
converse.connection.send(this.constructPresence(type, status_message)); converse.connection.send(this.constructPresence(type, status_message));
}, },
sendInitialPresence: function () {
this.sendPresence();
converse.initial_presence_sent = true;
},
setStatus: function (value) { setStatus: function (value) {
this.sendPresence(value); this.sendPresence(value);
this.save({'status': value}); this.save({'status': value});
...@@ -6264,7 +6265,6 @@ ...@@ -6264,7 +6265,6 @@
/* Remove those views which are only allowed with a valid /* Remove those views which are only allowed with a valid
* connection. * connection.
*/ */
this.initial_presence_sent = false;
if (this.roster) { if (this.roster) {
this.roster.off().reset(); // Removes roster contacts this.roster.off().reset(); // Removes roster contacts
} }
......
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