comparison plugins/pubsub.js @ 34:6ce66fba0242

Merging.
author Sonny Piers <sonny.piers@gmail.com>
date Sat, 28 Jan 2012 04:39:03 +0100
parents 1506992c33e2
children bdfbd58b4835
comparison
equal deleted inserted replaced
29:1e6d2ca2daae 34:6ce66fba0242
1 'use strict';
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
19 //////////
20 //PubSub//
21 //////////
22 Lightstring.NS.x = "jabber:x:data";
23 Lightstring.NS.pubsub = "http://jabber.org/protocol/pubsub";
24 Lightstring.NS.pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
25 Lightstring.stanza.pubsub = {
26 getConfig: function(aTo, aNode) {
27 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><configure node='"+aNode+"'/></pubsub></iq>";
28 },
29 items: function(aTo, aNode) {
30 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><items node='"+aNode+"'/></pubsub></iq>";
31 },
32 affiliations: function(aTo, aNode) {
33 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><affiliations node='"+aNode+"'/></pubsub></iq>";
34 },
35 publish: function(aTo, aNode, aItem, aId) {
36 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><publish node='"+aNode+"'><item id='"+aId+"'>"+aItem+"</item></publish></pubsub></iq>";
37 },
38 retract: function(aTo, aNode, aItem) {
39 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><retract node='"+aNode+"'><item id='"+aItem+"'/></retract></pubsub></iq>";
40 },
41 'delete': function(aTo, aNode, aURI) {
42 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><delete node='"+aNode+"'/></pubsub></iq>";
43 },
44 create: function(aTo, aNode, aFields) {
45 var iq = "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><create node='"+aNode+"'/>";
46 if(aFields) {
47 iq += "<configure><x xmlns='"+Lightstring.NS.x+"' type='submit'>"
48 aFields.forEach(function(field) {
49 iq += field;
50 });
51 iq += "</x></configure>";
52 }
53 iq += "</pubsub></iq>";
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 };
65 Lightstring.pubsubItems = function(aConnection, aTo, aNode, aCallback) {
66 aConnection.send(Lightstring.stanza.pubsub.items(aTo, aNode), function(answer){
67 var items = [];
68 var elms = answer.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(answer) {
88 if(answer.getAttribute('type') === 'result')
89 aCallback(null, answer);
90 else
91 aCallback(answer, null);
92 });
93 };
94 Lightstring.pubsubConfig = function(aConnection, aTo, aNode, aCallback) {
95 aConnection.send(Lightstring.stanza.pubsub.getConfig(aTo, aNode), function(answer){
96 var accessmodel = answer.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(answer){
105 if(aCallback)
106 aCallback(answer);
107 });
108 }
109 Lightstring.pubsubPublish = function(aConnection, aTo, aNode, aItem, aId, aCallback) {
110 aConnection.send(Lightstring.stanza.pubsub.publish(aTo, aNode, aItem, aId), function(answer){
111 if(answer.getAttribute('type') === 'result')
112 aCallback(null, answer);
113 else
114 aCallback(answer, null);
115 });
116 }
117 Lightstring.pubsubDelete = function(aConnection, aTo, aNode, aCallback) {
118 aConnection.send(Lightstring.stanza.pubsub.delete(aTo, aNode), function(answer){
119 if(aCallback)
120 aCallback(answer);
121 });
122 }
123 Lightstring.pubsubGetAffiliations = function(aConnection, aTo, aNode, aCallback) {
124 aConnection.send(Lightstring.stanza.pubsub.affiliations(aTo, aNode), function(answer) {
125 if((answer.getAttribute('type') === 'result') && aCallback) {
126 var affiliations = {};
127 answer.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 };