Mercurial > eldonilo > lightstring
annotate plugins/pubsub.js @ 52:48e2bd6b1885
Fix roster plugin.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 01 Feb 2012 03:51:12 +0100 |
parents | bdfbd58b4835 |
children | d1a7895b3dce |
rev | line source |
---|---|
0 | 1 'use strict'; |
2 | |
2 | 3 /** |
4 Copyright (c) 2011, Sonny Piers <sonny at fastmail dot net> | |
5 | |
6 Permission to use, copy, modify, and/or distribute this software for any | |
7 purpose with or without fee is hereby granted, provided that the above | |
8 copyright notice and this permission notice appear in all copies. | |
9 | |
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
7 | 19 ////////// |
20 //PubSub// | |
21 ////////// | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
22 Lightstring.NS.x = "jabber:x:data"; |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
23 Lightstring.NS.pubsub = "http://jabber.org/protocol/pubsub"; |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
24 Lightstring.NS.pubsub_owner = "http://jabber.org/protocol/pubsub#owner"; |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
25 Lightstring.stanza.pubsub = { |
20
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
26 getConfig: function(aTo, aNode) { |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
27 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><configure node='"+aNode+"'/></pubsub></iq>"; |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
28 }, |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
29 items: function(aTo, aNode) { |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
30 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><items node='"+aNode+"'/></pubsub></iq>"; |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
31 }, |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
32 affiliations: function(aTo, aNode) { |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
33 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><affiliations node='"+aNode+"'/></pubsub></iq>"; |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
34 }, |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
35 publish: function(aTo, aNode, aItem, aId) { |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
36 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><publish node='"+aNode+"'><item id='"+aId+"'>"+aItem+"</item></publish></pubsub></iq>"; |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
37 }, |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
38 retract: function(aTo, aNode, aItem) { |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
39 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><retract node='"+aNode+"'><item id='"+aItem+"'/></retract></pubsub></iq>"; |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
40 }, |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
41 'delete': function(aTo, aNode, aURI) { |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
42 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><delete node='"+aNode+"'/></pubsub></iq>"; |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
43 }, |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
44 create: function(aTo, aNode, aFields) { |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
45 var iq = "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><create node='"+aNode+"'/>"; |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
46 if(aFields) { |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
47 iq += "<configure><x xmlns='"+Lightstring.NS.x+"' type='submit'>" |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
48 aFields.forEach(function(field) { |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
49 iq += field; |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
50 }); |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
51 iq += "</x></configure>"; |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
52 } |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
53 iq += "</pubsub></iq>"; |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
54 return iq; |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
55 }, |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
56 setAffiliations: function(aTo, aNode, aAffiliations) { |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
57 var iq = "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><affiliations node='"+aNode+"'>"; |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
58 for(var i = 0; i < aAffiliations.length; i++) { |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
59 iq += "<affiliation jid='"+aAffiliations[i][0]+"' affiliation='"+aAffiliations[i][1]+"'/>" |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
60 } |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
61 iq += "</affiliations></pubsub></iq>"; |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
62 return iq; |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
63 }, |
0 | 64 }; |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
65 Lightstring.pubsubItems = function(aConnection, aTo, aNode, aCallback) { |
35 | 66 aConnection.send(Lightstring.stanza.pubsub.items(aTo, aNode), function(stanza){ |
20
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
67 var items = []; |
35 | 68 var elms = stanza.DOM.querySelectorAll('item'); |
8 | 69 for(var i = 0; i < elms.length; i++) { |
70 var node = elms[i]; | |
20
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
71 var item = { |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
72 id: node.getAttribute('id'), |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
73 name: node.querySelector('title').textContent, |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
74 src: node.querySelector('content').getAttribute('src'), |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
75 type: node.querySelector('content').getAttribute('type'), |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
76 } |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
77 var miniature = node.querySelector('link'); |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
78 if(miniature) |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
79 item.miniature = miniature.getAttribute('href'); |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
80 items.push(item); |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
81 }; |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
82 if(aCallback) |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
83 aCallback(items); |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
84 }); |
0 | 85 } |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
86 Lightstring.pubsubCreate = function(aConnection, aTo, aNode, aFields, aCallback) { |
35 | 87 aConnection.send(Lightstring.stanza.pubsub.create(aTo, aNode, aFields), function(stanza) { |
88 if(stanza.DOM.getAttribute('type') === 'result') | |
89 aCallback(null, stanza); | |
20
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
90 else |
35 | 91 aCallback(stanza, null); |
20
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
92 }); |
0 | 93 }; |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
94 Lightstring.pubsubConfig = function(aConnection, aTo, aNode, aCallback) { |
35 | 95 aConnection.send(Lightstring.stanza.pubsub.getConfig(aTo, aNode), function(stanza){ |
96 var accessmodel = stanza.DOM.querySelector('field[var="pubsub#access_model"]').lastChild.textContent; | |
20
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
97 if(accessmodel) |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
98 aCallback(accessmodel); |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
99 else |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
100 aCallback(null); |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
101 }); |
0 | 102 } |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
103 Lightstring.pubsubRetract = function(aConnection, aTo, aNode, aItem, aCallback) { |
35 | 104 aConnection.send(Lightstring.stanza.pubsub.retract(aTo, aNode, aItem), function(stanza){ |
20
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
105 if(aCallback) |
35 | 106 aCallback(stanza); |
20
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
107 }); |
0 | 108 } |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
109 Lightstring.pubsubPublish = function(aConnection, aTo, aNode, aItem, aId, aCallback) { |
35 | 110 aConnection.send(Lightstring.stanza.pubsub.publish(aTo, aNode, aItem, aId), function(stanza){ |
20
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
111 if(answer.getAttribute('type') === 'result') |
35 | 112 aCallback(null, stanza); |
20
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
113 else |
35 | 114 aCallback(stanza, null); |
20
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
115 }); |
0 | 116 } |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
117 Lightstring.pubsubDelete = function(aConnection, aTo, aNode, aCallback) { |
35 | 118 aConnection.send(Lightstring.stanza.pubsub.delete(aTo, aNode), function(stanza){ |
20
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
119 if(aCallback) |
35 | 120 aCallback(stanza); |
20
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
121 }); |
0 | 122 } |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
123 Lightstring.pubsubGetAffiliations = function(aConnection, aTo, aNode, aCallback) { |
35 | 124 aConnection.send(Lightstring.stanza.pubsub.affiliations(aTo, aNode), function(stanza) { |
125 if((stanza.DOM.getAttribute('type') === 'result') && aCallback) { | |
20
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
126 var affiliations = {}; |
35 | 127 stanza.DOM.querySelectorAll('affiliation').forEach(function(affiliation) { |
20
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
128 affiliations[affiliation.getAttribute("jid")] = affiliation.getAttribute("affiliation"); |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
129 }) |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
130 aCallback(affiliations); |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
131 } |
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
132 }); |
0 | 133 }; |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
134 Lightstring.pubsubSetAffiliations = function(aConnection, aTo, aNode, aAffiliations, aCallback) { |
20
7fcccf59e6ec
Always use two-spaces indent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
16
diff
changeset
|
135 aConnection.send(Lightstring.stanza.pubsub.setAffiliations(aTo, aNode, aAffiliations)); |
0 | 136 }; |