Skip to content

Commit

Permalink
Switch our Contact model to wrap Strophe roster items
Browse files Browse the repository at this point in the history
Requires updated Strophe roster plugin from strophe/strophejs-plugins#33
  • Loading branch information
benlangfeld committed Aug 4, 2014
1 parent 3dbba66 commit c5634bd
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 45 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"dependencies": {
"jquery": "~1.10.2",
"strophe": "1.1.3",
"strophejs-plugins": "strophe/strophejs-plugins#ca6124b235b0dc007a1bac740a5db3e0978c1a29",
"strophejs-plugins": "benlangfeld/strophejs-plugins#30fb089457addc37e01d69c3536dee868a90a9ad",
"mustache": "0.3.0",
"jquery-i18n": "1.1.1"
}
Expand Down
1 change: 0 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ Candy.Core = (function(self, Strophe, $) {
self.registerEventHandlers = function() {
self.addHandler(self.Event.Jabber.Version, Strophe.NS.VERSION, 'iq');
self.addHandler(self.Event.Jabber.Presence, null, 'presence');
self.addHandler(self.Event.Jabber.RosterPush, Strophe.NS.ROSTER, 'iq', 'set');
self.addHandler(self.Event.Jabber.Message, null, 'message');
self.addHandler(self.Event.Jabber.Bookmarks, Strophe.NS.PRIVATE, 'iq');
self.addHandler(self.Event.Jabber.Room.Disco, Strophe.NS.DISCO_INFO, 'iq', 'result');
Expand Down
4 changes: 3 additions & 1 deletion src/core/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ Candy.Core.Action = (function(self, Strophe, $) {
* Sends a request for a roster
*/
Roster: function() {
Candy.Core.getConnection().roster.get(Candy.Core.Event.Jabber.RosterFetch);
var roster = Candy.Core.getConnection().roster;
roster.registerCallback(Candy.Core.Event.Jabber.RosterPush);
roster.get(Candy.Core.Event.Jabber.RosterFetch);
},

/** Function: Presence
Expand Down
17 changes: 9 additions & 8 deletions src/core/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@
/** Class: Candy.Core.Contact
* Roster contact
*/
Candy.Core.Contact = function(jid, name, subscription, groups) {
Candy.Core.Contact = function(stropheRosterItem) {
/** Object: data
* User data containing:
* Strophe Roster plugin item model containing:
* - jid
* - name
* - subscription
* - groups
*/
this.data = {
jid: jid,
name: Strophe.unescapeNode(name),
subscription: subscription,
groups: groups,
};
this.data = stropheRosterItem;

/** Function: getJid
* Gets an unescaped user jid
Expand Down Expand Up @@ -67,6 +62,9 @@ Candy.Core.Contact = function(jid, name, subscription, groups) {
* (String) - name
*/
this.getName = function() {
if (!this.data.name) {
return this.getJid();
}
return Strophe.unescapeNode(this.data.name);
};

Expand Down Expand Up @@ -97,6 +95,9 @@ Candy.Core.Contact = function(jid, name, subscription, groups) {
* (String) - subscription
*/
this.getSubscription = function() {
if (!this.data.subscription) {
return 'none';
}
return this.data.subscription;
};

Expand Down
48 changes: 15 additions & 33 deletions src/core/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ Candy.Core.Event = (function(self, Strophe, $) {
*/
RosterFetch: function(items) {
$.each(items, function(i, item) {
self.Jabber._addOrUpdateRosterItem(item);
self.Jabber._addRosterItem(item);
});

return true;
Expand All @@ -187,44 +187,26 @@ Candy.Core.Event = (function(self, Strophe, $) {
* Returns:
* (Boolean) - true
*/
RosterPush: function(stanza) {
var item = $($('item', stanza)[0]),
jid = item.attr("jid"),
name = item.attr("name"),
subscription = item.attr("subscription");

if (subscription === "remove") {
Candy.Core.getRoster().remove(jid);
} else {
var groups = $.map($('group', item), function (group) {
return $(group).text();
});
RosterPush: function(items, updatedItem) {
if (!updatedItem) {
return true;
}

self.Jabber._addOrUpdateRosterItem({
name : name,
jid : jid,
subscription : subscription,
groups : groups
});
if (updatedItem.subscription === "remove") {
Candy.Core.getRoster().remove(updatedItem.jid);
} else {
var user = Candy.Core.getRoster().get(updatedItem.jid);
if (!user) {
self.Jabber._addRosterItem(updatedItem);
}
}

return true;
},

_addOrUpdateRosterItem: function(item) {
var roster = Candy.Core.getRoster(),
name = item.name || item.jid,
subscription = item.subscription || 'none',
user = roster.get(item.jid);

if (!user) {
user = new Candy.Core.Contact(item.jid, name, subscription, item.groups);
roster.add(user);
} else {
user.setName(name);
user.setSubscription(subscription);
user.setGroups(item.groups);
}
_addRosterItem: function(item) {
var user = new Candy.Core.Contact(item);
Candy.Core.getRoster().add(user);
},

/** Function: Bookmarks
Expand Down
5 changes: 5 additions & 0 deletions tests/candy/unit/core/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ define([
fakeConnection.authenticated = true;
fakeConnection.jid = 'n@d/r';

// The Strophe roster plugin adds its callbacks when we connect only (see https://github.com/strophe/strophejs-plugins/commit/4f3bcd25c43142f99c314f75a9bc10c8957a23d1). Add them manually here to compensate.
fakeConnection.addHandler(fakeConnection.roster._onReceivePresence.bind(fakeConnection.roster), null, 'presence', null, null, null);
fakeConnection.addHandler(fakeConnection.roster._onReceiveIQ.bind(fakeConnection.roster), Strophe.NS.ROSTER, 'iq', "set", null, null);

Candy.Core.init('http://foo.bar/http-bind', {}, fakeConnection);
Candy.Core.registerEventHandlers();
});
Expand Down Expand Up @@ -154,6 +158,7 @@ define([
});

bdd.it('makes the new item available in the main roster', function () {
expect(Candy.Core.getConnection().roster.findItem('new@guy.com').name).to.eql('Foo Bar');
expect(Candy.Core.getRoster().get('new@guy.com')).to.be.an.instanceof(Candy.Core.Contact);
});

Expand Down
2 changes: 1 addition & 1 deletion tests/candy/unit/core/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ define([
var contact;

bdd.beforeEach(function () {
contact = new Candy.Core.Contact('foo bar@baz.com', 'Some Name', 'both', ['Friends']);
contact = new Candy.Core.Contact({jid: 'foo bar@baz.com', name: 'Some Name', subscription: 'both', groups: ['Friends']});
});

bdd.it('reveals its JID', function () {
Expand Down

0 comments on commit c5634bd

Please sign in to comment.