From c5192f8ade3f366995dcfbf3b9c76b0920e12d48 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Sun, 4 Aug 2013 15:39:46 +0200 Subject: [PATCH] Bugfix. Updates #32 Users without roster contacts didn't have XMPPStatus.initStatus called and therefore had a default status of offline. Fixed this by setting the status to online from the start. The fetch command will get the true status from localStorage (if available). Made sure that the rest of converse's onConnected code is called in the callback from fetch (so that we know the user's status is correct). Updated and fixed tests. --- CHANGES.rst | 1 + converse.js | 139 +++++++++++++++++++++++++---------------------- spec/MainSpec.js | 17 ++++-- tests_main.js | 3 +- 4 files changed, 90 insertions(+), 70 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 1128a00c2..8cf55088b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,7 @@ Changelog 0.5.1 (Unreleased) ------------------ +- #32: Default status was offline when user didn't have contacts. [jcbrand] - Attach panels to the DOM upon initialize. [jcbrand] 0.5.0 (2013-07-30) diff --git a/converse.js b/converse.js index f881df61c..d00a495be 100644 --- a/converse.js +++ b/converse.js @@ -1046,21 +1046,6 @@ this.$el.html(this.template(this.model.toJSON())); this.contactspanel = new converse.ContactsPanel({'$parent': this.$el.find('#controlbox-panes')}); this.contactspanel.render(); - converse.xmppstatus = new converse.XMPPStatus(); - converse.xmppstatus.localStorage = new Backbone.LocalStorage( - hex_sha1('converse.xmppstatus-'+converse.bare_jid)); - converse.xmppstatus.fetch({ - success: function (xmppstatus, resp) { - if (!xmppstatus.get('fullname')) { - converse.getVCard( - null, // No 'to' attr when getting one's own vCard - function (jid, fullname, image, image_type, url) { - converse.xmppstatus.save({'fullname': fullname}); - } - ); - } - } - }); converse.xmppstatusview = new converse.XMPPStatusView({'model': converse.xmppstatus}); converse.xmppstatusview.render(); this.roomspanel = new converse.RoomsPanel({'$parent': this.$el.find('#controlbox-panes')}); @@ -2029,7 +2014,7 @@ item; if (this.isSelf(bare_jid)) { - if ((converse.connection.jid !== jid)&&(presence_type !== 'unavailabe')) { + if ((converse.connection.jid !== jid)&&(presence_type !== 'unavailable')) { // Another resource has changed it's status, we'll update ours as well. // FIXME: We should ideally differentiate between converse.js using // resources and other resources (i.e Pidgin etc.) @@ -2127,8 +2112,20 @@ this.model.on("destroy", function (item) { this.removeRosterItem(item); }, this); this.$el.hide().html(this.template()); - this.model.fetch({add: true}); // Get the cached roster items from localstorage - // XXX: is this necessary? this.initialSort(); + this.model.fetch({ + add: true, + success: function (model, resp, options) { + if (resp.length === 0) { + // The presence stanza is sent out once all + // roster contacts have been added and rendered. + // See RosterView's render method. + // + // If there aren't any roster contacts, we still + // want to send a presence stanza, so we do it here. + converse.xmppstatus.sendPresence(); + } + }, + }); // Get the cached roster items from localstorage }, updateChatBox: function (item, changed) { @@ -2195,9 +2192,8 @@ // options where all of the items are offline and now we can show the rosterView item.set('sorted', true); this.initialSort(); - this.$el.show(function () { - converse.xmppstatus.initStatus(); - }); + this.$el.show(); + converse.xmppstatus.sendPresence(); } } // Hide the headings if there are no contacts under them @@ -2228,22 +2224,24 @@ this.XMPPStatus = Backbone.Model.extend({ initialize: function () { this.set({ - 'status' : this.get('status'), - 'status_message' : this.get('status_message'), - 'fullname' : this.get('fullname') + 'status' : this.get('status') || 'online', }); - }, - - initStatus: function () { - var stat = this.get('status'); - if (stat === undefined) { - stat = 'online'; - this.save({status: stat}); - } - this.sendPresence(stat); + this.on('change', $.proxy(function () { + if (this.get('fullname') === undefined) { + converse.getVCard( + null, // No 'to' attr when getting one's own vCard + $.proxy(function (jid, fullname, image, image_type, url) { + this.save({'fullname': fullname}); + }, this) + ); + } + }, this)); }, sendPresence: function (type) { + if (type === undefined) { + type = this.get('status') || 'online'; + } var status_message = this.get('status_message'), presence; // Most of these presence types are actually not explicitly sent, @@ -2390,7 +2388,7 @@ this.$el.html(this.choose_template()); this.$el.find('#fancy-xmpp-status-select') .html(this.status_template({ - 'status_message': __("I am %1$s", this.getPrettyStatus(chat_status)), + 'status_message': this.model.get('status_message') || __("I am %1$s", this.getPrettyStatus(chat_status)), 'chat_status': chat_status })); // iterate through all the