comparison modules/mod_publish.js @ 24:b80ab94da447

Add new modules files.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 01 Nov 2010 00:02:27 +0100
parents
children b2faacfefb90
comparison
equal deleted inserted replaced
23:5fc4ee90c1bc 24:b80ab94da447
1 var config = require('../configuration');
2 var storage = require('../storage');
3 var toBareJID = require('../util').toBareJID;
4 var NS = require('../namespaces');
5
6 // SECTION 7.1: Publish an Item to a Node
7 exports.retrieveSub = {
8 type: 'set',
9 child: 'pubsub',
10 ns: NS.PUBSUB,
11 pschild: 'publish',
12 func: function(response, stanza, request, to) {
13 if (!config.enabled('publish'))
14 return makeError(response, errors.pub.publish.item_publication_not_supported.n);
15
16 var nodeID = request.getAttribute('node');
17 if (!nodeID || nodeID == '')
18 return makeError(response, errors.nodeid_required.n);
19
20 var autocreate = false;
21 if (!storage.existsNode(nodeID)) {
22 if (config.enabled('auto-create'))
23 autocreate = true;
24 else
25 return makeError(response, errors.node_does_not_exist.n);
26 }
27
28 var affil = storage.getAffiliation(toBareJID(to), nodeID);
29 if (typeof affil == 'number' && affil != errors.node_does_not_exist.n)
30 return makeError(response, affil);
31 if (!autocreate && affil != 'super-owner' && affil != 'owner' && affil != 'publisher' && affil != 'publish-only')
32 return makeError(response, errors.forbidden.n);
33
34 var item = request.getChild('item');
35 var itemID = item.getAttribute('id');
36 if (!config.enabled('item-ids') && itemID)
37 return makeError(response, errors.itemid_required.n);
38 itemID = itemID? itemID: utils.makeRandomId();
39
40 if (item.tags.length != 1)
41 return makeError(response, errors.pub.publish.bad_payload.n);
42
43 var conf = storage.getConfiguration(nodeID);
44 var publishOptions = pubsub.getChild('publish-options');
45 if (publishOptions && config.enabled('publish-options')) {
46 var x = publishOptions.getChild('x', 'jabber:x:data');
47 if (!x || x.getAttribute('type') != 'submit')
48 return makeError(response, errors.bad_request.n);
49
50 var form = forms.parse(x, true);
51 if (form.access_model != conf['pubsub#access_model'] && !autocreate)
52 return makeError(response, errors.pub.configuration.precondition.n);
53 }
54
55 if (!config.enabled('persistent-items')) {
56 var notifs = storage.purgeNode(nodeID);
57 if (typeof notifs == 'number')
58 return makeError(response, r);
59 }
60
61 if (autocreate) {
62 if (!form)
63 form = {};
64 form['pubsub#creator'] = toBareJID(to);
65
66 var r = storage.createNode(nodeID, form);
67 if (typeof r == 'number')
68 return makeError(response, r);
69 }
70
71 var content = item.getChild();
72
73 subscribers = storage.setItem(nodeID, itemID, content);
74 if (typeof subscribers == 'number')
75 return makeError(response, subscribers);
76
77 var attrs = {};
78 if (content)
79 attrs[itemID] = {content: content};
80 else
81 attrs[itemID] = {};
82 notifs.send(subscribers, 'items', nodeID, attrs);
83
84 response.c('pubsub', {xmlns: NS.PUBSUB})
85 .c('publish', {node: nodeID})
86 .c('item', {id: itemID});
87
88 return response;
89 }
90 }
91
92 // SECTION 7.2: Delete an Item from a Node
93 exports.retrieveSub = {
94 type: 'set',
95 child: 'pubsub',
96 ns: NS.PUBSUB,
97 pschild: 'retract',
98 func: function(response, stanza, request, to) {
99 if (!config.enabled('retract-items'))
100 return makeError(response, errors.pub.retract.item_deletion_not_supported.n);
101
102 var nodeID = request.getAttribute('node');
103 if (!nodeID || nodeID == '')
104 return makeError(response, errors.nodeid_required.n);
105 if (!storage.existsNode(nodeID))
106 return makeError(response, errors.node_does_not_exist.n);
107
108 var item = request.getChild('item');
109 if (!item)
110 return makeError(response, errors.pub.retract.item_or_itemid_required.n);
111
112 var itemID = item.getAttribute('id')
113 if (!itemID || itemID == '')
114 return makeError(response, errors.pub.retract.item_or_itemid_required.n);
115
116 var affil = storage.getAffiliation(toBareJID(to), nodeID);
117 if (affil != 'super-owner' && affil != 'owner' && affil != 'publish-only')
118 return makeError(response, errors.forbidden.n);
119
120 var subscribers = storage.deleteItem(nodeID, itemID);
121 if (typeof subscribers == 'number')
122 return makeError(response, subscribers);
123
124 var attrs = {};
125 attrs[itemID] = {};
126 notifs.send(subscribers, 'items', nodeID, attrs, 'retract')
127
128 return response;
129 }
130 }