Mercurial > eldonilo > lightstring
comparison plugins/pubsub.js @ 61:d1ba6f0e2a92
Add PubSub events emitter.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 01 Feb 2012 18:09:28 +0100 |
parents | d1a7895b3dce |
children | 7cb0037bf43c |
comparison
equal
deleted
inserted
replaced
60:0e86fca6a596 | 61:d1ba6f0e2a92 |
---|---|
17 */ | 17 */ |
18 | 18 |
19 ////////// | 19 ////////// |
20 //PubSub// | 20 //PubSub// |
21 ////////// | 21 ////////// |
22 Lightstring.plugins['pubsub'] = { | 22 (function() { |
23 namespaces: { | 23 var event_tags = ['collection', 'configuration', 'delete', 'items', 'purge', 'subscription']; |
24 x: "jabber:x:data", //XXX | 24 |
25 pubsub: "http://jabber.org/protocol/pubsub", | 25 Lightstring.plugins['pubsub'] = { |
26 pubsub_owner: "http://jabber.org/protocol/pubsub#owner", | 26 namespaces: { |
27 pubsub_error: "http://jabber.org/protocol/pubsub#error" | 27 x: "jabber:x:data", //XXX |
28 }, | 28 pubsub: "http://jabber.org/protocol/pubsub", |
29 stanzas: { | 29 pubsub_owner: "http://jabber.org/protocol/pubsub#owner", |
30 getConfig: function(aTo, aNode) { | 30 pubsub_event: "http://jabber.org/protocol/pubsub#event", |
31 return "<iq type='get' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub_owner + "'><configure node='" + aNode + "'/></pubsub></iq>"; | 31 pubsub_error: "http://jabber.org/protocol/pubsub#error" |
32 }, | 32 }, |
33 items: function(aTo, aNode) { | 33 stanzas: { |
34 return "<iq type='get' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub + "'><items node='" + aNode + "'/></pubsub></iq>"; | 34 getConfig: function(aTo, aNode) { |
35 return "<iq type='get' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub_owner + "'><configure node='" + aNode + "'/></pubsub></iq>"; | |
36 }, | |
37 items: function(aTo, aNode) { | |
38 return "<iq type='get' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub + "'><items node='" + aNode + "'/></pubsub></iq>"; | |
39 }, | |
40 affiliations: function(aTo, aNode) { | |
41 return "<iq type='get' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub_owner + "'><affiliations node='" + aNode + "'/></pubsub></iq>"; | |
42 }, | |
43 publish: function(aTo, aNode, aItem, aId) { | |
44 return "<iq type='set' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub + "'><publish node='" + aNode + "'><item id='" + aId + "'>" + aItem + "</item></publish></pubsub></iq>"; | |
45 }, | |
46 retract: function(aTo, aNode, aItem) { | |
47 return "<iq type='set' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub + "'><retract node='" + aNode + "'><item id='" + aItem + "'/></retract></pubsub></iq>"; | |
48 }, | |
49 'delete': function(aTo, aNode, aURI) { | |
50 return "<iq type='set' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub_owner + "'><delete node='" + aNode + "'/></pubsub></iq>"; | |
51 }, | |
52 create: function(aTo, aNode, aFields) { | |
53 var iq = "<iq type='set' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub + "'><create node='" + aNode + "'/>"; | |
54 if (aFields) { | |
55 iq += "<configure><x xmlns='" + Lightstring.NS.x + "' type='submit'>" | |
56 aFields.forEach(function(field) { | |
57 iq += field; | |
58 }); | |
59 iq += "</x></configure>"; | |
60 } | |
61 iq += "</pubsub></iq>"; | |
62 return iq; | |
63 }, | |
64 setAffiliations: function(aTo, aNode, aAffiliations) { | |
65 var iq = "<iq type='set' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub_owner + "'><affiliations node='" + aNode + "'>"; | |
66 for (var i = 0; i < aAffiliations.length; i++) | |
67 iq += "<affiliation jid='" + aAffiliations[i][0] + "' affiliation='" + aAffiliations[i][1] + "'/>"; | |
68 iq += "</affiliations></pubsub></iq>"; | |
69 return iq; | |
70 }, | |
35 }, | 71 }, |
36 affiliations: function(aTo, aNode) { | 72 methods: { |
37 return "<iq type='get' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub_owner + "'><affiliations node='" + aNode + "'/></pubsub></iq>"; | 73 items: function(aTo, aNode, aResult, aError) { |
74 this.send(Lightstring.stanza.pubsub.items(aTo, aNode), function(stanza) { | |
75 var items = []; | |
76 var elms = stanza.DOM.querySelectorAll('item'); | |
77 for (var i = 0; i < elms.length; i++) { | |
78 var node = elms[i]; | |
79 var item = { | |
80 id: node.getAttribute('id'), | |
81 name: node.querySelector('title').textContent, | |
82 src: node.querySelector('content').getAttribute('src'), | |
83 type: node.querySelector('content').getAttribute('type'), | |
84 } | |
85 var miniature = node.querySelector('link'); | |
86 if (miniature) | |
87 item.miniature = miniature.getAttribute('href'); | |
88 items.push(item); | |
89 }; | |
90 | |
91 if (aResult) | |
92 aResult(items); | |
93 }, aError); | |
94 }, | |
95 create: function(aTo, aNode, aFields, aResult, aError) { | |
96 this.send(Lightstring.stanza.pubsub.create(aTo, aNode, aFields), aResult, aError); | |
97 }, | |
98 config: function(aTo, aNode, aResult, aError) { | |
99 this.send(Lightstring.stanza.pubsub.getConfig(aTo, aNode), function(stanza) { | |
100 //FIXME: wtf? | |
101 var accessmodel = stanza.DOM.querySelector('field[var="pubsub#access_model"]').lastChild.textContent; | |
102 if(accessmodel) | |
103 aResult, aError(accessmodel); | |
104 else | |
105 aResult, aError(null); | |
106 }); | |
107 }, | |
108 retract: function(aTo, aNode, aItem, aResult, aError) { | |
109 this.send(Lightstring.stanza.pubsub.retract(aTo, aNode, aItem), aResult, aError); | |
110 }, | |
111 publish = function(aTo, aNode, aItem, aId, aResult, aError) { | |
112 this.send(Lightstring.stanza.pubsub.publish(aTo, aNode, aItem, aId), aResult, aError); | |
113 }, | |
114 'delete': function(aTo, aNode, aResult, aError) { | |
115 this.send(Lightstring.stanza.pubsub.delete(aTo, aNode), aResult, aError); | |
116 }, | |
117 getAffiliations: function(aTo, aNode, aResult, aError) { | |
118 this.send(Lightstring.stanza.pubsub.affiliations(aTo, aNode), function(stanza) { | |
119 if((stanza.DOM.getAttribute('type') === 'result') && aResult, aError) { | |
120 var affiliations = {}; | |
121 stanza.DOM.querySelectorAll('affiliation').forEach(function(affiliation) { | |
122 affiliations[affiliation.getAttribute("jid")] = affiliation.getAttribute("affiliation"); | |
123 }) | |
124 if (aResult) | |
125 aResult(affiliations); | |
126 } | |
127 }, aError); | |
128 }, | |
129 setAffiliations: function(aTo, aNode, aAffiliations, aResult, aError) { | |
130 this.send(Lightstring.stanza.pubsub.setAffiliations(aTo, aNode, aAffiliations), aResult, aError); | |
131 } | |
38 }, | 132 }, |
39 publish: function(aTo, aNode, aItem, aId) { | 133 init: function() { |
40 return "<iq type='set' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub + "'><publish node='" + aNode + "'><item id='" + aId + "'>" + aItem + "</item></publish></pubsub></iq>"; | 134 //TODO: find a way to put that in handlers, it’s UGLY! |
41 }, | 135 this.on('in-message-*-' + Lightstring.namespaces['pubsub_event'] + ':event', function(stanza) { |
42 retract: function(aTo, aNode, aItem) { | 136 var payload = stanza.firstChild.firstChild; //XXX |
43 return "<iq type='set' to='" + aTo + "'><pubsub xmlns='" + Lightstring.NS.pubsub + "'><retract node='" + aNode + "'><item id='" + aItem + "'/></retract></pubsub></iq>"; | 137 if (payload.namespaceURI !== Lightstring.namespaces['pubsub_event']) |
44 }, | 138 return; //TODO: emit something. |
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 }, | |
67 }, | |
68 methods: { | |
69 items: function(aTo, aNode, aResult, aError) { | |
70 this.send(Lightstring.stanza.pubsub.items(aTo, aNode), function(stanza) { | |
71 var items = []; | |
72 var elms = stanza.DOM.querySelectorAll('item'); | |
73 for (var i = 0; i < elms.length; i++) { | |
74 var node = elms[i]; | |
75 var item = { | |
76 id: node.getAttribute('id'), | |
77 name: node.querySelector('title').textContent, | |
78 src: node.querySelector('content').getAttribute('src'), | |
79 type: node.querySelector('content').getAttribute('type'), | |
80 } | |
81 var miniature = node.querySelector('link'); | |
82 if (miniature) | |
83 item.miniature = miniature.getAttribute('href'); | |
84 items.push(item); | |
85 }; | |
86 | 139 |
87 if (aResult) | 140 var name = payload.localName; |
88 aResult(items); | 141 if (event_tags.indexOf(name) === -1) |
89 }, aError); | 142 return; //TODO: emit something. |
90 }, | 143 |
91 create: function(aTo, aNode, aFields, aResult, aError) { | 144 this.emit('pubsub:' + name); |
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); | |
102 }); | 145 }); |
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); | |
127 } | 146 } |
128 } | 147 }; |
129 }; | 148 })(); |