Mercurial > psgxs
annotate modules/mod_publish_message.js @ 41:bc717575e66a
Much better handling of modules.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 20 Feb 2011 15:51:12 +0100 |
parents | 6697f394301f |
children | 023f767662d3 |
rev | line source |
---|---|
35
6697f394301f
Replace util with fdsq to work around a stupid bug.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
1 exports.publishMessage = { |
34
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 stanza: 'message', |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 child: 'body', |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
4 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
|
5 if (!config.enabled('publish')) |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 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
|
7 |
35
6697f394301f
Replace util with fdsq to work around a stupid bug.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
8 var nodeID = fdsq.toResource(from); |
34
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 if (!nodeID || nodeID == '') |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 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
|
11 |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 var autocreate = false; |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 if (!storage.existsNode(nodeID)) { |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
14 if (config.enabled('auto-create')) |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
15 autocreate = true; |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
16 else |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
17 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
|
18 } |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
20 var affil = storage.getAffiliation(toBareJID(to), nodeID); |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
21 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
|
22 return makeError(response, affil); |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
23 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
|
24 return makeError(response, errors.forbidden.n); |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
26 if (!config.enabled('item-ids')) |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
27 return makeError(response, errors.itemid_required.n); |
35
6697f394301f
Replace util with fdsq to work around a stupid bug.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
28 var itemID = fdsq.makeRandomId(); |
34
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
30 var now = new Date(); |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
31 |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
32 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
|
33 item.c('entry', {xmlns: NS.ATOM}); |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
34 if (stanza.getChild('subject')) |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 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
|
36 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
|
37 item.c('id').t(itemID).up(); |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
38 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
|
39 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
|
40 |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 if (!config.enabled('persistent-items')) { |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
42 var notifications = storage.purgeNode(nodeID); |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 if (typeof notifications == 'number') |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
44 return makeError(response, r); |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
45 } |
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 (autocreate) { |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
48 if (!form) |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
49 form = {}; |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
50 form['pubsub#creator'] = toBareJID(to); |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
51 |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
52 var r = storage.createNode(nodeID, form); |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
53 if (typeof r == 'number') |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
54 return makeError(response, r); |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
55 } |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
56 |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
57 var content = item.getChild(); |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
58 |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
59 subscribers = storage.setItem(nodeID, itemID, content); |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
60 if (typeof subscribers == 'number') |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
61 return makeError(response, subscribers); |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
62 |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
63 var attrs = {}; |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
64 if (content) |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
65 attrs[itemID] = {content: content}; |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
66 else |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
67 attrs[itemID] = {}; |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
68 notifs.send(subscribers, 'items', nodeID, attrs); |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
69 |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
70 response.c('pubsub', {xmlns: NS.PUBSUB}) |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
71 .c('publish', {node: nodeID}) |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
72 .c('item', {id: itemID}); |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
73 |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
74 return response; |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
75 } |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
76 } |