Browse Source

Store roster on converse obj.

Storing it on the rosterview causes race conditions.
JC Brand 11 năm trước cách đây
mục cha
commit
33d84a0c8e
5 tập tin đã thay đổi với 54 bổ sung53 xóa
  1. 23 22
      converse.js
  2. 1 1
      spec/chatbox.js
  3. 25 25
      spec/controlbox.js
  4. 1 1
      spec/minchats.js
  5. 4 4
      tests/utils.js

+ 23 - 22
converse.js

@@ -338,7 +338,7 @@
                         img_type = $vcard.find('TYPE').text(),
                         url = $vcard.find('URL').text();
                     if (jid) {
-                        var contact = converse.rosterview.roster.get(jid);
+                        var contact = converse.roster.get(jid);
                         if (contact) {
                             fullname = _.isEmpty(fullname)? contact.get('fullname') || jid: fullname;
                             contact.save({
@@ -357,7 +357,7 @@
                 jid,
                 function (iq) {
                     // Error callback
-                    var contact = converse.rosterview.roster.get(jid);
+                    var contact = converse.roster.get(jid);
                     if (contact) {
                         contact.save({
                             'vcard_updated': moment().format()
@@ -565,6 +565,7 @@
             this.features = new this.Features();
             this.enableCarbons();
             this.initStatus($.proxy(function () {
+                this.roster = new converse.RosterContacts();
                 this.rosterview = new this.RosterView({model: new this.RosterGroups()});
                 this.chatboxes.onConnected();
                 this.connection.roster.get(function () {});
@@ -1333,7 +1334,7 @@
 
             updateVCard: function () {
                 var jid = this.model.get('jid'),
-                    contact = converse.rosterview.roster.get(jid);
+                    contact = converse.roster.get(jid);
                 if ((contact) && (!contact.get('vcard_updated'))) {
                     converse.getVCard(
                         jid,
@@ -2458,7 +2459,7 @@
                     resource = Strophe.getResourceFromJid(message_from);
                 }
                 chatbox = this.get(buddy_jid);
-                roster_item = converse.rosterview.roster.get(buddy_jid);
+                roster_item = converse.roster.get(buddy_jid);
 
                 if (roster_item === undefined) {
                     // The buddy was likely removed
@@ -2479,7 +2480,7 @@
                     });
                 }
                 chatbox.receiveMessage($message);
-                converse.rosterview.roster.addResource(buddy_jid, resource);
+                converse.roster.addResource(buddy_jid, resource);
                 converse.emit('message', message);
                 return true;
             }
@@ -2734,7 +2735,8 @@
             },
 
             updateUnreadMessagesCounter: function () {
-                var ls = this.model.pluck('num_unread'), count = 0;
+                var ls = this.model.pluck('num_unread'),
+                    count = 0, i;
                 for (i=0; i<ls.length; i++) { count += ls[i]; }
                 this.toggleview.model.set({'num_unread': count});
                 this.render();
@@ -2932,6 +2934,9 @@
 
         this.RosterContacts = Backbone.Collection.extend({
             model: converse.RosterContact,
+            browserStorage: new Backbone.BrowserStorage[converse.storage](
+                b64_sha1('converse.contacts-'+converse.bare_jid)),
+
             comparator: function (contact1, contact2) {
                 var name1 = contact1.get('fullname').toLowerCase();
                 var status1 = contact1.get('chat_status') || 'offline';
@@ -3218,7 +3223,6 @@
                     description: DESC_GROUP_TOGGLE,
                     state: OPENED
                 }, attributes))
-
                 // Collection of contacts belonging to this group.
                 this.contacts = new converse.RosterContacts();
             }
@@ -3238,7 +3242,7 @@
                 }, this);
                 this.model.contacts.on("destroy", this.onRemove, this);
                 this.model.contacts.on("remove", this.onRemove, this);
-                converse.rosterview.roster.on('change:groups', this.onContactGroupChange, this);
+                converse.roster.on('change:groups', this.onContactGroupChange, this);
             },
 
             render: function () {
@@ -3362,21 +3366,18 @@
             id: 'converse-roster',
 
             initialize: function () {
-                this.roster = new converse.RosterContacts();
-                this.roster.browserStorage = new Backbone.BrowserStorage[converse.storage](
-                    b64_sha1('converse.contacts-'+converse.bare_jid));
                 this.registerRosterHandler();
                 this.registerRosterXHandler();
                 this.registerPresenceHandler();
 
-                this.roster.on("add", this.onAdd, this);
-                this.roster.on('change', this.onChange, this);
-                this.roster.on("remove", this.update, this);
-                this.roster.on("destroy", this.update, this);
+                converse.roster.on("add", this.onAdd, this);
+                converse.roster.on('change', this.onChange, this);
+                converse.roster.on("remove", this.update, this);
+                converse.roster.on("destroy", this.update, this);
                 this.model.on("reset", this.reset, this);
                 this.render();
                 this.model.fetch({add: true});
-                this.roster.fetch({add: true});
+                converse.roster.fetch({add: true});
             },
 
             render: function () {
@@ -3387,7 +3388,7 @@
             update: function () {
                 // XXX: Is this still being used/valid?
                 var $count = $('#online-count');
-                $count.text('('+this.roster.getNumOnlineContacts()+')');
+                $count.text('('+converse.roster.getNumOnlineContacts()+')');
                 if (!$count.is(':visible')) {
                     $count.show();
                 }
@@ -3395,7 +3396,7 @@
             },
 
             reset: function () {
-                this.roster.reset();
+                converse.roster.reset();
                 this.removeAll();
                 this.render().update();
                 return this;
@@ -3404,20 +3405,20 @@
             registerRosterHandler: function () {
                 // Register handlers that depend on the roster
                 converse.connection.roster.registerCallback(
-                    $.proxy(this.roster.rosterHandler, this.roster),
+                    $.proxy(converse.roster.rosterHandler, converse.roster),
                     null, 'presence', null);
             },
 
             registerRosterXHandler: function () {
                 converse.connection.addHandler(
-                    $.proxy(this.roster.subscribeToSuggestedItems, this.roster),
+                    $.proxy(converse.roster.subscribeToSuggestedItems, converse.roster),
                     'http://jabber.org/protocol/rosterx', 'message', null);
             },
 
             registerPresenceHandler: function () {
                 converse.connection.addHandler(
                     $.proxy(function (presence) {
-                        this.roster.presenceHandler(presence);
+                        converse.roster.presenceHandler(presence);
                         return true;
                     }, this), null, 'presence', null);
             },
@@ -3475,7 +3476,7 @@
                     return view;
                 }
                 view = new converse.RosterGroupView({
-                    model: this.model.create({name: name, id: b64_sha1(name)})
+                    model: this.model.create({name: name, id: b64_sha1(name)}),
                 });
                 this.add(name, view);
                 return this.positionGroup(view)

+ 1 - 1
spec/chatbox.js

@@ -682,7 +682,7 @@
             beforeEach(function () {
                 utils.closeAllChatBoxes();
                 utils.removeControlBox();
-                converse.rosterview.roster.browserStorage._clear();
+                converse.roster.browserStorage._clear();
                 utils.initConverse();
                 utils.createContacts();
                 utils.openControlBox();

+ 25 - 25
spec/controlbox.js

@@ -158,7 +158,7 @@
                 _.each(_.keys(groups), $.proxy(function (name) {
                     j = i;
                     for (i=j; i<j+groups[name]; i++) {
-                        this.rosterview.roster.create({
+                        this.roster.create({
                             jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
                             subscription: 'both',
                             ask: null,
@@ -196,7 +196,7 @@
                 converse.rosterview.render();
                 var groups = ['colleagues', 'friends'];
                 for (i=0; i<mock.cur_names.length; i++) {
-                    this.rosterview.roster.create({
+                    this.roster.create({
                         jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
                         subscription: 'both',
                         ask: null,
@@ -224,7 +224,7 @@
                 _.each(_.keys(groups), $.proxy(function (name) {
                     j = i;
                     for (i=j; i<j+groups[name]; i++) {
-                        this.rosterview.roster.create({
+                        this.roster.create({
                             jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
                             subscription: 'both',
                             ask: null,
@@ -266,7 +266,7 @@
                 spyOn(converse, 'emit');
                 spyOn(this.rosterview, 'update').andCallThrough();
                 runs($.proxy(function () {
-                    this.rosterview.roster.create({
+                    this.roster.create({
                         jid: mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
                         subscription: 'none',
                         ask: 'subscribe',
@@ -305,7 +305,7 @@
                 var name = mock.pend_names[0];
                 _clearContacts();
                 spyOn(window, 'confirm').andReturn(true);
-                this.rosterview.roster.create({
+                this.roster.create({
                     jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
                     subscription: 'none',
                     ask: 'subscribe',
@@ -339,7 +339,7 @@
                 spyOn(this.rosterview, 'update').andCallThrough();
                 for (i=0; i<mock.pend_names.length; i++) {
                     is_last = i===(mock.pend_names.length-1);
-                    this.rosterview.roster.create({
+                    this.roster.create({
                         jid: mock.pend_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
                         subscription: 'none',
                         ask: 'subscribe',
@@ -377,7 +377,7 @@
                 spyOn(converse, 'emit');
                 spyOn(this.rosterview, 'update').andCallThrough();
                 for (i=0; i<mock.cur_names.length; i++) {
-                    this.rosterview.roster.create({
+                    this.roster.create({
                         jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
                         subscription: 'both',
                         ask: null,
@@ -416,7 +416,7 @@
                 var name = mock.cur_names[0];
                 _clearContacts();
                 spyOn(window, 'confirm').andReturn(true);
-                this.rosterview.roster.create({
+                this.roster.create({
                     jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
                     subscription: 'both',
                     ask: null,
@@ -437,7 +437,7 @@
                 spyOn(this.rosterview, 'update').andCallThrough();
                 for (i=0; i<mock.cur_names.length; i++) {
                     jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
-                    this.rosterview.roster.get(jid).set('chat_status', 'online');
+                    this.roster.get(jid).set('chat_status', 'online');
                     expect(this.rosterview.update).toHaveBeenCalled();
                     // Check that they are sorted alphabetically
                     t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text();
@@ -452,7 +452,7 @@
                 spyOn(this.rosterview, 'update').andCallThrough();
                 for (i=0; i<mock.cur_names.length; i++) {
                     jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
-                    this.rosterview.roster.get(jid).set('chat_status', 'dnd');
+                    this.roster.get(jid).set('chat_status', 'dnd');
                     expect(this.rosterview.update).toHaveBeenCalled();
                     // Check that they are sorted alphabetically
                     t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
@@ -467,7 +467,7 @@
                 spyOn(this.rosterview, 'update').andCallThrough();
                 for (i=0; i<mock.cur_names.length; i++) {
                     jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
-                    this.rosterview.roster.get(jid).set('chat_status', 'away');
+                    this.roster.get(jid).set('chat_status', 'away');
                     expect(this.rosterview.update).toHaveBeenCalled();
                     // Check that they are sorted alphabetically
                     t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
@@ -482,7 +482,7 @@
                 spyOn(this.rosterview, 'update').andCallThrough();
                 for (i=0; i<mock.cur_names.length; i++) {
                     jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
-                    this.rosterview.roster.get(jid).set('chat_status', 'xa');
+                    this.roster.get(jid).set('chat_status', 'xa');
                     expect(this.rosterview.update).toHaveBeenCalled();
                     // Check that they are sorted alphabetically
                     t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.xa').find('a.open-chat').text();
@@ -497,7 +497,7 @@
                 spyOn(this.rosterview, 'update').andCallThrough();
                 for (i=0; i<mock.cur_names.length; i++) {
                     jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
-                    this.rosterview.roster.get(jid).set('chat_status', 'unavailable');
+                    this.roster.get(jid).set('chat_status', 'unavailable');
                     expect(this.rosterview.update).toHaveBeenCalled();
                     // Check that they are sorted alphabetically
                     t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
@@ -510,23 +510,23 @@
                 var i;
                 for (i=0; i<3; i++) {
                     jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
-                    this.rosterview.roster.get(jid).set('chat_status', 'online');
+                    this.roster.get(jid).set('chat_status', 'online');
                 }
                 for (i=3; i<6; i++) {
                     jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
-                    this.rosterview.roster.get(jid).set('chat_status', 'dnd');
+                    this.roster.get(jid).set('chat_status', 'dnd');
                 }
                 for (i=6; i<9; i++) {
                     jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
-                    this.rosterview.roster.get(jid).set('chat_status', 'away');
+                    this.roster.get(jid).set('chat_status', 'away');
                 }
                 for (i=9; i<12; i++) {
                     jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
-                    this.rosterview.roster.get(jid).set('chat_status', 'xa');
+                    this.roster.get(jid).set('chat_status', 'xa');
                 }
                 for (i=12; i<15; i++) {
                     jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
-                    this.rosterview.roster.get(jid).set('chat_status', 'unavailable');
+                    this.roster.get(jid).set('chat_status', 'unavailable');
                 }
 
                 var contacts = this.rosterview.$el.find('dd.current-xmpp-contact');
@@ -577,7 +577,7 @@
                     }
                 };
                 for (i=0; i<mock.req_names.length; i++) {
-                    this.rosterview.roster.create({
+                    this.roster.create({
                         jid: mock.req_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
                         subscription: 'none',
                         ask: null,
@@ -601,7 +601,7 @@
                 converse.rosterview.model.reset(); // We want to manually create users so that we can spy
                 var name = mock.req_names[0];
                 spyOn(window, 'confirm').andReturn(true);
-                this.rosterview.roster.create({
+                this.roster.create({
                     jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
                     subscription: 'none',
                     ask: null,
@@ -650,7 +650,7 @@
                 expect(window.confirm).toHaveBeenCalled();
                 expect(this.connection.roster.unauthorize).toHaveBeenCalled();
                 // There should now be one less contact
-                expect(this.rosterview.roster.length).toEqual(mock.req_names.length-1);
+                expect(this.roster.length).toEqual(mock.req_names.length-1);
             }, converse));
         }, converse));
 
@@ -664,11 +664,11 @@
 
             it("are saved to, and can be retrieved from, browserStorage", $.proxy(function () {
                 var new_attrs, old_attrs, attrs, old_roster;
-                var num_contacts = this.rosterview.roster.length;
+                var num_contacts = this.roster.length;
                 new_roster = new this.RosterContacts();
                 // Roster items are yet to be fetched from browserStorage
                 expect(new_roster.length).toEqual(0);
-                new_roster.browserStorage = this.rosterview.roster.browserStorage;
+                new_roster.browserStorage = this.roster.browserStorage;
                 new_roster.fetch();
                 expect(new_roster.length).toEqual(num_contacts);
                 // Check that the roster items retrieved from browserStorage
@@ -676,7 +676,7 @@
                 attrs = ['jid', 'fullname', 'subscription', 'ask'];
                 for (i=0; i<attrs.length; i++) {
                     new_attrs = _.pluck(_.pluck(new_roster.models, 'attributes'), attrs[i]);
-                    old_attrs = _.pluck(_.pluck(this.rosterview.roster.models, 'attributes'), attrs[i]);
+                    old_attrs = _.pluck(_.pluck(this.roster.models, 'attributes'), attrs[i]);
                     // Roster items in storage are not necessarily sorted,
                     // so we have to sort them here to do a proper
                     // comparison
@@ -691,7 +691,7 @@
                 // we make some online now
                 for (i=0; i<5; i++) {
                     jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
-                    this.rosterview.roster.get(jid).set('chat_status', 'online');
+                    this.roster.get(jid).set('chat_status', 'online');
                 }
             }, converse));
         }, converse));

+ 1 - 1
spec/minchats.js

@@ -13,7 +13,7 @@
             runs(function () {
                 utils.closeAllChatBoxes();
                 utils.removeControlBox();
-                converse.rosterview.roster.browserStorage._clear();
+                converse.roster.browserStorage._clear();
                 utils.initConverse();
                 utils.createContacts();
                 utils.openControlBox();

+ 4 - 4
tests/utils.js

@@ -36,7 +36,7 @@
     };
 
     utils.initRoster = function () {
-        converse.rosterview.roster.browserStorage._clear();
+        converse.roster.browserStorage._clear();
         converse.initRoster();
     };
 
@@ -80,13 +80,13 @@
         var i = 0, jid, views = [];
         for (i; i<amount; i++) {
             jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
-            views[i] = converse.rosterview.roster.get(jid).trigger("open");
+            views[i] = converse.roster.get(jid).trigger("open");
         }
         return views;
     };
 
     utils.openChatBoxFor = function (jid) {
-        return converse.rosterview.roster.get(jid).trigger("open");
+        return converse.roster.get(jid).trigger("open");
     };
 
     utils.removeRosterContacts = function () {
@@ -134,7 +134,7 @@
             ask = null;
         }
         for (i=0; i<names.length; i++) {
-            converse.rosterview.roster.create({
+            converse.roster.create({
                 ask: ask,
                 fullname: names[i],
                 is_last: i===(names.length-1),