Permalink
Browse files

Feature: Added simple Disco result support

  • Loading branch information...
Theo Cushion
Theo Cushion committed May 2, 2012
1 parent 91f132c commit 5cba03a753d7a50a8663fa1aac851248786c282b
Showing with 211 additions and 1 deletion.
  1. +22 −0 spec/javascripts/helpers/SpecHelper.js
  2. +107 −0 spec/javascripts/xep-0030Spec.js
  3. +1 −1 src/xeps/iq.js
  4. +81 −0 src/xeps/xep-0030.js
@@ -56,6 +56,28 @@ beforeEach(function() {
var bare_expected = $.map(expected,function(e){ return makeCallbackBare(e); });
return this.env.equals_(bare_actuals, bare_expected);
+ },
+ toBeFeature: function(expected){
+ var makeBare = function(e){
+ return {var: e.get('var')};
+ };
+
+ var bare_actual = makeBare(this.actual);
+
+ return this.env.equals_(bare_actual, expected);
+ },
+ toBeIdentity: function(expected){
+ var makeBare = function(e){
+ return {
+ category: e.get('category'),
+ type: e.get('type'),
+ name: e.get('name')
+ };
+ };
+
+ var bare_actual = makeBare(this.actual);
+
+ return this.env.equals_(bare_actual, expected);
}
});
});
@@ -0,0 +1,107 @@
+describe("XEP-0030", function(){
+
+ beforeEach(function(){
+ Frabjous.Store.init();
+ });
+
+ describe("http://jabber.org/protocol/disco#items", function(){
+
+ describe("parsing", function(){
+
+ var stanza;
+
+ beforeEach(function(){
+ stanza = "<iq type='result' from='juliet@capulet.com' to='shakespeare.lit' id='items2'><query xmlns='http://jabber.org/protocol/disco#items'><item jid='juliet@capulet.com/balcony'/><item jid='juliet@capulet.com/chamber'/></query></iq>";
+ });
+
+ it("should create a contact", function(){
+ processStanza(stanza);
+
+ var c = Frabjous.Store.find(Frabjous.Contact, "juliet@capulet.com");
+
+ expect(c.get('jid')).toBeJid("juliet@capulet.com");
+ });
+
+ it("should create further contacts as a list", function(){
+ processStanza(stanza);
+
+ var c = Frabjous.Store.find(Frabjous.Contact, "juliet@capulet.com");
+ var items = c.get('items');
+
+ expect(c.get('items').get('length')).toEqual(2);
+
+ var i0 = c.get('items').objectAt(0);
+ var i1 = c.get('items').objectAt(1);
+
+ expect(i0.get('jid')).toBeJid("juliet@capulet.com/balcony");
+ expect(i1.get('jid')).toBeJid("juliet@capulet.com/chamber");
+ });
+
+ it("should not send out a reply", function(){
+ new Mock(Frabjous.Connection);
+ Frabjous.Connection.expects('send').never();
+
+ Frabjous.Connection.receive(stanza);
+ });
+
+ });
+
+ });
+
+ describe("http://jabber.org/protocol/disco#info", function(){
+
+ describe("parsing",function(){
+
+ var stanza;
+
+ beforeEach(function(){
+ stanza = "<iq type='result' from='plays.shakespeare.lit' to='romeo@montague.net/orchard' id='info1'><query xmlns='http://jabber.org/protocol/disco#info'><identity category='conference' type='text' name='Play-Specific Chatrooms'/><identity category='directory' type='chatroom' name='Play-Specific Chatrooms'/><feature var='http://jabber.org/protocol/disco#info'/><feature var='http://jabber.org/protocol/disco#items'/><feature var='http://jabber.org/protocol/muc'/><feature var='jabber:iq:register'/><feature var='jabber:iq:search'/><feature var='jabber:iq:time'/><feature var='jabber:iq:version'/></query></iq>";
+ });
+
+ it("should create a contact", function(){
+ processStanza(stanza);
+
+ var c = Frabjous.Store.find(Frabjous.Contact, "plays.shakespeare.lit");
+
+ expect(c.get('jid')).toBeJid("plays.shakespeare.lit");
+ });
+
+ it("should add features", function(){
+ processStanza(stanza);
+
+ var c = Frabjous.Store.find(Frabjous.Contact, "plays.shakespeare.lit");
+ var features = c.get('features');
+
+ expect(features.get('length')).toEqual(7);
+
+ expect(features.objectAt(0)).toBeFeature({var: "http://jabber.org/protocol/disco#info"});
+ expect(features.objectAt(1)).toBeFeature({var: "http://jabber.org/protocol/disco#items"});
+ expect(features.objectAt(2)).toBeFeature({var: "http://jabber.org/protocol/muc"});
+ expect(features.objectAt(3)).toBeFeature({var: "jabber:iq:register"});
+ expect(features.objectAt(4)).toBeFeature({var: "jabber:iq:search"});
+ expect(features.objectAt(5)).toBeFeature({var: "jabber:iq:time"});
+ expect(features.objectAt(6)).toBeFeature({var: "jabber:iq:version"});
+ });
+
+ it("should add identities", function(){
+ processStanza(stanza);
+
+ var c = Frabjous.Store.find(Frabjous.Contact, "plays.shakespeare.lit");
+ var identities = c.get('identities');
+
+ expect(identities.get('length')).toEqual(2);
+ expect(identities.objectAt(0)).toBeIdentity({category: "conference", type: "text", name: "Play-Specific Chatrooms"});
+ expect(identities.objectAt(1)).toBeIdentity({category: "directory", type: "chatroom", name: "Play-Specific Chatrooms"});
+ });
+
+ it("should not send out a reply", function(){
+ new Mock(Frabjous.Connection);
+ Frabjous.Connection.expects('send').never();
+
+ Frabjous.Connection.receive(stanza);
+ });
+ });
+
+ });
+
+});
View
@@ -60,7 +60,7 @@ Frabjous.Connection.get('callbacks').add({
Frabjous.Connection.send(xml.toString());
},
is_match: function(obj){
- return obj.get('is_temporary') && obj.get('stanza_type') == "iq";
+ return obj.get('is_temporary') && obj.get('stanza_type') == "iq" && obj.get('stanza').root().attr('type') == "get";
},
must_keep: true
});
View
@@ -0,0 +1,81 @@
+//= require ember
+//= require ember-data
+//= require ./contact
+
+Frabjous.Feature = Frabjous.Permanent.extend({
+ var: DS.attr('string')
+});
+
+Frabjous.Identity = Frabjous.Permanent.extend({
+ category: DS.attr('string'),
+ type: DS.attr('string'),
+ name: DS.attr('string')
+});
+
+Frabjous.Contact.reopen({
+ name: DS.attr('string'),
+ node: DS.attr('string'),
+ items: DS.hasMany('Frabjous.Contact'),
+ features: DS.hasMany('Frabjous.Feature', {embedded: true}),
+ identities: DS.hasMany('Frabjous.Identity', {embedded: true})
+});
+
+Frabjous.Parser.register("XEP-0030 items", function(stanza){
+ var query = stanza.root().find("query[xmlns='http://jabber.org/protocol/disco#items']");
+ if( stanza.type() == "result" && stanza.is_iq && query.length > 0 ){
+
+ // Each item is a new contact
+ items = query.find('item').map(function(i,e){
+ $e = $(e);
+ return {
+ frabjous_type: Frabjous.Contact,
+ id: $e.attr('jid'),
+ jid: $e.attr('jid'),
+ name: $e.attr('name'),
+ node: $e.attr('node')
+ };
+ }).toArray();
+
+ // Finally, the place where the query came from is a contact, that is linked to the items
+ items.push({
+ frabjous_type: Frabjous.Contact,
+ id: stanza.from(),
+ jid: stanza.from(),
+ items: $.map(items,function(e){ return e.jid; })
+ });
+
+ return items;
+ }
+});
+
+Frabjous.Parser.register("XEP-0030 info", function(stanza){
+ var query = stanza.root().find("query[xmlns='http://jabber.org/protocol/disco#info']");
+ if( stanza.type() == "result" && stanza.is_iq && query.length > 0 ){
+
+ identities = query.find('identity').map(function(i,e){
+ $e = $(e);
+ return {
+ id: [stanza.from(), $e.attr('category'), $e.attr('type')].join(" "),
+ category: $e.attr('category'),
+ type: $e.attr('type'),
+ name: $e.attr('name')
+ };
+ }).toArray();
+
+ features = query.find('feature').map(function(i,e){
+ $e = $(e);
+ return {
+ id: [stanza.from(), $e.attr('var')].join(" "),
+ var: $e.attr('var')
+ };
+ }).toArray();
+
+ return {
+ frabjous_type: Frabjous.Contact,
+ id: stanza.from(),
+ jid: stanza.from(),
+ identities: identities,
+ features: features
+ };
+ }
+});

0 comments on commit 5cba03a

Please sign in to comment.