comparison plugins/pubsub.js @ 53:d1a7895b3dce

Fix PubSub plugin.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 01 Feb 2012 04:01:25 +0100
parents bdfbd58b4835
children d1ba6f0e2a92
comparison
equal deleted inserted replaced
52:48e2bd6b1885 53:d1a7895b3dce
17 */ 17 */
18 18
19 ////////// 19 //////////
20 //PubSub// 20 //PubSub//
21 ////////// 21 //////////
22 Lightstring.NS.x = "jabber:x:data"; 22 Lightstring.plugins['pubsub'] = {
23 Lightstring.NS.pubsub = "http://jabber.org/protocol/pubsub"; 23 namespaces: {
24 Lightstring.NS.pubsub_owner = "http://jabber.org/protocol/pubsub#owner"; 24 x: "jabber:x:data", //XXX
25 Lightstring.stanza.pubsub = { 25 pubsub: "http://jabber.org/protocol/pubsub",
26 getConfig: function(aTo, aNode) { 26 pubsub_owner: "http://jabber.org/protocol/pubsub#owner",
27 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><configure node='"+aNode+"'/></pubsub></iq>"; 27 pubsub_error: "http://jabber.org/protocol/pubsub#error"
28 }, 28 },
29 items: function(aTo, aNode) { 29 stanzas: {
30 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><items node='"+aNode+"'/></pubsub></iq>"; 30 getConfig: function(aTo, aNode) {
31 return "<iq type='get' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub_owner + "'><configure node='" + aNode + "'/></pubsub></iq>";
32 },
33 items: function(aTo, aNode) {
34 return "<iq type='get' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub + "'><items node='" + aNode + "'/></pubsub></iq>";
35 },
36 affiliations: function(aTo, aNode) {
37 return "<iq type='get' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub_owner + "'><affiliations node='" + aNode + "'/></pubsub></iq>";
38 },
39 publish: function(aTo, aNode, aItem, aId) {
40 return "<iq type='set' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub + "'><publish node='" + aNode + "'><item id='" + aId + "'>" + aItem + "</item></publish></pubsub></iq>";
41 },
42 retract: function(aTo, aNode, aItem) {
43 return "<iq type='set' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub + "'><retract node='" + aNode + "'><item id='" + aItem + "'/></retract></pubsub></iq>";
44 },
45 'delete': function(aTo, aNode, aURI) {
46 return "<iq type='set' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub_owner + "'><delete node='" + aNode + "'/></pubsub></iq>";
47 },
48 create: function(aTo, aNode, aFields) {
49 var iq = "<iq type='set' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub + "'><create node='" + aNode + "'/>";
50 if (aFields) {
51 iq += "<configure><x xmlns='" + Lightstring.NS.x + "' type='submit'>"
52 aFields.forEach(function(field) {
53 iq += field;
54 });
55 iq += "</x></configure>";
56 }
57 iq += "</pubsub></iq>";
58 return iq;
59 },
60 setAffiliations: function(aTo, aNode, aAffiliations) {
61 var iq = "<iq type='set' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub_owner + "'><affiliations node='" + aNode + "'>";
62 for (var i = 0; i < aAffiliations.length; i++)
63 iq += "<affiliation jid='" + aAffiliations[i][0] + "' affiliation='" + aAffiliations[i][1] + "'/>";
64 iq += "</affiliations></pubsub></iq>";
65 return iq;
66 },
31 }, 67 },
32 affiliations: function(aTo, aNode) { 68 methods: {
33 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><affiliations node='"+aNode+"'/></pubsub></iq>"; 69 items: function(aTo, aNode, aResult, aError) {
34 }, 70 this.send(Lightstring.stanza.pubsub.items(aTo, aNode), function(stanza) {
35 publish: function(aTo, aNode, aItem, aId) { 71 var items = [];
36 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><publish node='"+aNode+"'><item id='"+aId+"'>"+aItem+"</item></publish></pubsub></iq>"; 72 var elms = stanza.DOM.querySelectorAll('item');
37 }, 73 for (var i = 0; i < elms.length; i++) {
38 retract: function(aTo, aNode, aItem) { 74 var node = elms[i];
39 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><retract node='"+aNode+"'><item id='"+aItem+"'/></retract></pubsub></iq>"; 75 var item = {
40 }, 76 id: node.getAttribute('id'),
41 'delete': function(aTo, aNode, aURI) { 77 name: node.querySelector('title').textContent,
42 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><delete node='"+aNode+"'/></pubsub></iq>"; 78 src: node.querySelector('content').getAttribute('src'),
43 }, 79 type: node.querySelector('content').getAttribute('type'),
44 create: function(aTo, aNode, aFields) { 80 }
45 var iq = "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><create node='"+aNode+"'/>"; 81 var miniature = node.querySelector('link');
46 if(aFields) { 82 if (miniature)
47 iq += "<configure><x xmlns='"+Lightstring.NS.x+"' type='submit'>" 83 item.miniature = miniature.getAttribute('href');
48 aFields.forEach(function(field) { 84 items.push(item);
49 iq += field; 85 };
86
87 if (aResult)
88 aResult(items);
89 }, aError);
90 },
91 create: function(aTo, aNode, aFields, aResult, aError) {
92 this.send(Lightstring.stanza.pubsub.create(aTo, aNode, aFields), aResult, aError);
93 },
94 config: function(aTo, aNode, aResult, aError) {
95 this.send(Lightstring.stanza.pubsub.getConfig(aTo, aNode), function(stanza) {
96 //FIXME: wtf?
97 var accessmodel = stanza.DOM.querySelector('field[var="pubsub#access_model"]').lastChild.textContent;
98 if(accessmodel)
99 aResult, aError(accessmodel);
100 else
101 aResult, aError(null);
50 }); 102 });
51 iq += "</x></configure>"; 103 },
104 retract: function(aTo, aNode, aItem, aResult, aError) {
105 this.send(Lightstring.stanza.pubsub.retract(aTo, aNode, aItem), aResult, aError);
106 },
107 publish = function(aTo, aNode, aItem, aId, aResult, aError) {
108 this.send(Lightstring.stanza.pubsub.publish(aTo, aNode, aItem, aId), aResult, aError);
109 },
110 'delete': function(aTo, aNode, aResult, aError) {
111 this.send(Lightstring.stanza.pubsub.delete(aTo, aNode), aResult, aError);
112 },
113 getAffiliations: function(aTo, aNode, aResult, aError) {
114 this.send(Lightstring.stanza.pubsub.affiliations(aTo, aNode), function(stanza) {
115 if((stanza.DOM.getAttribute('type') === 'result') && aResult, aError) {
116 var affiliations = {};
117 stanza.DOM.querySelectorAll('affiliation').forEach(function(affiliation) {
118 affiliations[affiliation.getAttribute("jid")] = affiliation.getAttribute("affiliation");
119 })
120 if (aResult)
121 aResult(affiliations);
122 }
123 }, aError);
124 },
125 setAffiliations: function(aTo, aNode, aAffiliations, aResult, aError) {
126 this.send(Lightstring.stanza.pubsub.setAffiliations(aTo, aNode, aAffiliations), aResult, aError);
52 } 127 }
53 iq += "</pubsub></iq>"; 128 }
54 return iq;
55 },
56 setAffiliations: function(aTo, aNode, aAffiliations) {
57 var iq = "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><affiliations node='"+aNode+"'>";
58 for(var i = 0; i < aAffiliations.length; i++) {
59 iq += "<affiliation jid='"+aAffiliations[i][0]+"' affiliation='"+aAffiliations[i][1]+"'/>"
60 }
61 iq += "</affiliations></pubsub></iq>";
62 return iq;
63 },
64 }; 129 };
65 Lightstring.pubsubItems = function(aConnection, aTo, aNode, aCallback) {
66 aConnection.send(Lightstring.stanza.pubsub.items(aTo, aNode), function(stanza){
67 var items = [];
68 var elms = stanza.DOM.querySelectorAll('item');
69 for(var i = 0; i < elms.length; i++) {
70 var node = elms[i];
71 var item = {
72 id: node.getAttribute('id'),
73 name: node.querySelector('title').textContent,
74 src: node.querySelector('content').getAttribute('src'),
75 type: node.querySelector('content').getAttribute('type'),
76 }
77 var miniature = node.querySelector('link');
78 if(miniature)
79 item.miniature = miniature.getAttribute('href');
80 items.push(item);
81 };
82 if(aCallback)
83 aCallback(items);
84 });
85 }
86 Lightstring.pubsubCreate = function(aConnection, aTo, aNode, aFields, aCallback) {
87 aConnection.send(Lightstring.stanza.pubsub.create(aTo, aNode, aFields), function(stanza) {
88 if(stanza.DOM.getAttribute('type') === 'result')
89 aCallback(null, stanza);
90 else
91 aCallback(stanza, null);
92 });
93 };
94 Lightstring.pubsubConfig = function(aConnection, aTo, aNode, aCallback) {
95 aConnection.send(Lightstring.stanza.pubsub.getConfig(aTo, aNode), function(stanza){
96 var accessmodel = stanza.DOM.querySelector('field[var="pubsub#access_model"]').lastChild.textContent;
97 if(accessmodel)
98 aCallback(accessmodel);
99 else
100 aCallback(null);
101 });
102 }
103 Lightstring.pubsubRetract = function(aConnection, aTo, aNode, aItem, aCallback) {
104 aConnection.send(Lightstring.stanza.pubsub.retract(aTo, aNode, aItem), function(stanza){
105 if(aCallback)
106 aCallback(stanza);
107 });
108 }
109 Lightstring.pubsubPublish = function(aConnection, aTo, aNode, aItem, aId, aCallback) {
110 aConnection.send(Lightstring.stanza.pubsub.publish(aTo, aNode, aItem, aId), function(stanza){
111 if(answer.getAttribute('type') === 'result')
112 aCallback(null, stanza);
113 else
114 aCallback(stanza, null);
115 });
116 }
117 Lightstring.pubsubDelete = function(aConnection, aTo, aNode, aCallback) {
118 aConnection.send(Lightstring.stanza.pubsub.delete(aTo, aNode), function(stanza){
119 if(aCallback)
120 aCallback(stanza);
121 });
122 }
123 Lightstring.pubsubGetAffiliations = function(aConnection, aTo, aNode, aCallback) {
124 aConnection.send(Lightstring.stanza.pubsub.affiliations(aTo, aNode), function(stanza) {
125 if((stanza.DOM.getAttribute('type') === 'result') && aCallback) {
126 var affiliations = {};
127 stanza.DOM.querySelectorAll('affiliation').forEach(function(affiliation) {
128 affiliations[affiliation.getAttribute("jid")] = affiliation.getAttribute("affiliation");
129 })
130 aCallback(affiliations);
131 }
132 });
133 };
134 Lightstring.pubsubSetAffiliations = function(aConnection, aTo, aNode, aAffiliations, aCallback) {
135 aConnection.send(Lightstring.stanza.pubsub.setAffiliations(aTo, aNode, aAffiliations));
136 };