comparison modules/mod_publish_message.js @ 34:dcf1f09f8cee

Add message to publish module and various fixes.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 05 Nov 2010 15:31:13 +0100
parents
children 6697f394301f
comparison
equal deleted inserted replaced
33:62cbb1c49bc5 34:dcf1f09f8cee
1 var config = require('../configuration');
2 var storage = require('../storage');
3 var notifs = require('../notifs');
4 var errors = require('../errors');
5 var utils = require('../util');
6 var toBareJID = utils.toBareJID;
7 var NS = require('../namespaces');
8 var xmpp = require('xmpp');
9
10 exports.publish = {
11 stanza: 'message',
12 child: 'body',
13 func: function(response, stanza, request, to, from) {
14 if (!config.enabled('publish'))
15 return makeError(response, errors.pub.publish.item_publication_not_supported.n);
16
17 var nodeID = utils.toResource(from);
18 console.log(require('sys').inspect(nodeID));
19 if (!nodeID || nodeID == '')
20 return makeError(response, errors.nodeid_required.n);
21
22 var autocreate = false;
23 if (!storage.existsNode(nodeID)) {
24 if (config.enabled('auto-create'))
25 autocreate = true;
26 else
27 return makeError(response, errors.node_does_not_exist.n);
28 }
29
30 var affil = storage.getAffiliation(toBareJID(to), nodeID);
31 if (typeof affil == 'number' && affil != errors.node_does_not_exist.n)
32 return makeError(response, affil);
33 if (!autocreate && affil != 'super-owner' && affil != 'owner' && affil != 'publisher' && affil != 'publish-only')
34 return makeError(response, errors.forbidden.n);
35
36 if (!config.enabled('item-ids'))
37 return makeError(response, errors.itemid_required.n);
38 var itemID = utils.makeRandomId();
39
40 var now = new Date();
41
42 var item = xmpp.stanza('item', {xmlns: NS.PUBSUB, id: itemID});
43 item.c('entry', {xmlns: NS.ATOM});
44 if (stanza.getChild('subject'))
45 item.c('title').t(stanza.getChild('subject')).up();
46 item.c('summary').t(request.getText()).up();
47 item.c('id').t(itemID).up();
48 item.c('published').t(now.toString()).up();
49 item.c('updated').t(now.toString()).up();
50 console.log(require('sys').inspect(item, false, null));
51
52 if (!config.enabled('persistent-items')) {
53 var notifications = storage.purgeNode(nodeID);
54 if (typeof notifications == 'number')
55 return makeError(response, r);
56 }
57
58 if (autocreate) {
59 if (!form)
60 form = {};
61 form['pubsub#creator'] = toBareJID(to);
62
63 var r = storage.createNode(nodeID, form);
64 if (typeof r == 'number')
65 return makeError(response, r);
66 }
67
68 var content = item.getChild();
69
70 subscribers = storage.setItem(nodeID, itemID, content);
71 if (typeof subscribers == 'number')
72 return makeError(response, subscribers);
73
74 var attrs = {};
75 if (content)
76 attrs[itemID] = {content: content};
77 else
78 attrs[itemID] = {};
79 notifs.send(subscribers, 'items', nodeID, attrs);
80
81 response.c('pubsub', {xmlns: NS.PUBSUB})
82 .c('publish', {node: nodeID})
83 .c('item', {id: itemID});
84
85 return response;
86 }
87 }