comparison plugins/roster.js @ 88:595be4a8584f

fix the roster plugin
author Sonny Piers <sonny.piers@gmail.com>
date Thu, 23 Feb 2012 17:07:22 +0100
parents 48e2bd6b1885
children c06ec02217ee
comparison
equal deleted inserted replaced
87:0f6f213f0a33 88:595be4a8584f
22 Lightstring.plugins['roster'] = { 22 Lightstring.plugins['roster'] = {
23 namespaces: { 23 namespaces: {
24 roster: 'jabber:iq:roster' 24 roster: 'jabber:iq:roster'
25 }, 25 },
26 stanzas: { 26 stanzas: {
27 get: function() { 27 'get': function() {
28 return "<iq type='get'>" + 28 return "<iq type='get'>" +
29 "<query xmlns='" + Lightstring.NS.roster + "'/>" + 29 "<query xmlns='" + Lightstring.ns.roster + "'/>" +
30 "</iq>"; 30 "</iq>";
31 }, 31 },
32 add: function(aAddress, aGroups) { 32 add: function(aAddress, aGroups) {
33 var iq = "<iq type='set'>" + 33 var iq = "<iq type='set'>" +
34 "<query xmlns='" + Lightstring.NS.roster + "'>" + 34 "<query xmlns='" + Lightstring.ns.roster + "'>" +
35 "<item jid='" + aAddress + "'/>" + 35 "<item jid='" + aAddress + "'/>" +
36 "</query>" + 36 "</query>" +
37 "</iq>"; 37 "</iq>";
38 for (var i = 0; i < aGroups.length; i++) { 38 for (var i = 0; i < aGroups.length; i++) {
39 if (i === 0) 39 if (i === 0)
42 } 42 }
43 return iq; 43 return iq;
44 }, 44 },
45 remove: function(aAddress) { 45 remove: function(aAddress) {
46 return "<iq type='set'>" + 46 return "<iq type='set'>" +
47 "<query xmlns='" + Lightstring.NS.roster + "'>" + 47 "<query xmlns='" + Lightstring.ns.roster + "'>" +
48 "<item jid='" + aAddress + "' subscription='remove'/>" + 48 "<item jid='" + aAddress + "' subscription='remove'/>" +
49 "</query>" + 49 "</query>" +
50 "</iq>"; 50 "</iq>";
51 } 51 }
52 }, 52 },
53 methods: { 53 methods: {
54 get: function(aResult, aError) { 54 'get': function(aSuccess, aError) {
55 this.send(this.stanza.roster.get(), function(stanza) { 55 this.send(Lightstring.stanzas.roster.get(), function(stanza) {
56 var contacts = []; 56 var contacts = [];
57 57
58 var children = stanza.DOM.firstChild.childNodes; 58 var items = stanza.DOM.getElementsByTagName('item');
59 var length = children.length;
60 59
61 for (var i = 0; i < length; i++) { 60 for (var i = 0; i < items.length; i++) {
62 var item = children[i]; 61 var item = items[i];
63 var jid = item.getAttributeNS(null, 'jid'); 62 var contact = {}
64 var name = item.getAttributeNS(null, 'name'); 63
65 var subscription = item.getAttributeNS(null, 'subscription'); 64 var jid = item.getAttribute('jid');
66 var groups = item.children; 65 if (jid)
67 var contact = {}; 66 contact.jid = jid;
67
68 var name = item.getAttribute('name');
68 if (name) 69 if (name)
69 contact.name = name; 70 contact.name = name;
70 if (jid) 71
71 contact.jid = jid; 72 var subscription = item.getAttribute('subscription');
72 if (subscription) 73 if (subscription)
73 contact.subscription = subscription; 74 contact.subscription = subscription;
74 if (groups.length > 0) { 75
76 var groups = item.getElementsByTagName('group');
77 if(groups) {
75 contact.groups = []; 78 contact.groups = [];
76 groups.forEach(function(group) { 79 for (var y = 0; y < groups.length; y++)
77 contact.groups.push(group.textContent); 80 contact.groups.push(groups[y].textContent);
78 });
79 } 81 }
80 82
81 contacts.push(contact); 83 contacts.push(contact);
82 } 84 }
83 85
84 if (aResult) 86 stanza.roster = {
85 aResult(contacts); 87 contacts: contacts
88 };
89
90 if (aSuccess)
91 aSuccess(stanza);
86 }, aError); 92 }, aError);
87 } 93 }
88 } 94 }
89 }; 95 };