annotate modules/mod_publish_message.js @ 54:c2c9c06d6e9d

Fix problems with unneeded text nodes.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 06 May 2011 17:51:15 +0200
parents 0d3f18bb1d36
children 99bd1d1ac071
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
1 /*
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
2 * Copyright (C) 2010 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
3 *
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
4 * This file is part of PSĜS, a PubSub server written in JavaScript.
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
5 *
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
6 * PSĜS is free software: you can redistribute it and/or modify
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
7 * it under the terms of the GNU Affero General Public License as
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
9 * License.
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
10 *
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
11 * PSĜS is distributed in the hope that it will be useful,
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
14 * GNU Affero General Public License for more details.
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
15 *
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
16 * You should have received a copy of the GNU Affero General Public License
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
17 * along with PSĜS. If not, see <http://www.gnu.org/licenses/>.
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
18 */
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
19
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
20 'use strict';
023f767662d3 Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
21
35
6697f394301f Replace util with fdsq to work around a stupid bug.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 34
diff changeset
22 exports.publishMessage = {
34
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 stanza: 'message',
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 child: 'body',
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 func: function(response, stanza, request, to, from) {
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 if (!config.enabled('publish'))
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 return makeError(response, errors.pub.publish.item_publication_not_supported.n);
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28
47
0d3f18bb1d36 Remove usage of fdsq.js in modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
29 var nodeID = JID.toResource(from);
34
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 if (!nodeID || nodeID == '')
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 return makeError(response, errors.nodeid_required.n);
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 var autocreate = false;
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 if (!storage.existsNode(nodeID)) {
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 if (config.enabled('auto-create'))
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 autocreate = true;
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 else
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 return makeError(response, errors.node_does_not_exist.n);
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 }
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40
47
0d3f18bb1d36 Remove usage of fdsq.js in modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
41 var affil = storage.getAffiliation(JID.toBare(to), nodeID);
34
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 if (typeof affil == 'number' && affil != errors.node_does_not_exist.n)
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 return makeError(response, affil);
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 if (!autocreate && affil != 'super-owner' && affil != 'owner' && affil != 'publisher' && affil != 'publish-only')
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 return makeError(response, errors.forbidden.n);
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 if (!config.enabled('item-ids'))
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 return makeError(response, errors.itemid_required.n);
47
0d3f18bb1d36 Remove usage of fdsq.js in modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
49 var itemID = makeRandomId();
34
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 var now = new Date();
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 var item = xmpp.stanza('item', {xmlns: NS.PUBSUB, id: itemID});
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 item.c('entry', {xmlns: NS.ATOM});
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 if (stanza.getChild('subject'))
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 item.c('title').t(stanza.getChild('subject')).up();
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 item.c('summary').t(request.getText()).up();
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 item.c('id').t(itemID).up();
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 item.c('published').t(now.toString()).up();
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 item.c('updated').t(now.toString()).up();
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 if (!config.enabled('persistent-items')) {
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 var notifications = storage.purgeNode(nodeID);
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 if (typeof notifications == 'number')
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 return makeError(response, r);
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 }
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 if (autocreate) {
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 if (!form)
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 form = {};
47
0d3f18bb1d36 Remove usage of fdsq.js in modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
71 form['pubsub#creator'] = JID.toBare(to);
34
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 var r = storage.createNode(nodeID, form);
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 if (typeof r == 'number')
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 return makeError(response, r);
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 }
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 var content = item.getChild();
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 subscribers = storage.setItem(nodeID, itemID, content);
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 if (typeof subscribers == 'number')
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 return makeError(response, subscribers);
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 var attrs = {};
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 if (content)
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 attrs[itemID] = {content: content};
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 else
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 attrs[itemID] = {};
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 notifs.send(subscribers, 'items', nodeID, attrs);
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 response.c('pubsub', {xmlns: NS.PUBSUB})
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 .c('publish', {node: nodeID})
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 .c('item', {id: itemID});
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 return response;
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 }
dcf1f09f8cee Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 }