view modules/mod_publish_message.js @ 51:22b06eade9a7

Fix typo error.
author Sonny Piers <sonny.piers@gmail.com>
date Mon, 21 Mar 2011 15:40:32 +0100
parents 0d3f18bb1d36
children 99bd1d1ac071
line wrap: on
line source

/*
 *  Copyright (C) 2010  Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 *
 *  This file is part of PSĜS, a PubSub server written in JavaScript.
 *
 *  PSĜS is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License.
 *
 *  PSĜS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with PSĜS.  If not, see <http://www.gnu.org/licenses/>.
 */

'use strict';

exports.publishMessage = {
	stanza: 'message',
	child: 'body',
	func: function(response, stanza, request, to, from) {
		if (!config.enabled('publish'))
			return makeError(response, errors.pub.publish.item_publication_not_supported.n);

		var nodeID = JID.toResource(from);
		if (!nodeID || nodeID == '')
			return makeError(response, errors.nodeid_required.n);

		var autocreate = false;
		if (!storage.existsNode(nodeID)) {
			if (config.enabled('auto-create'))
				autocreate = true;
			else
				return makeError(response, errors.node_does_not_exist.n);
		}

		var affil = storage.getAffiliation(JID.toBare(to), nodeID);
		if (typeof affil == 'number' && affil != errors.node_does_not_exist.n)
			return makeError(response, affil);
		if (!autocreate && affil != 'super-owner' && affil != 'owner' && affil != 'publisher' && affil != 'publish-only')
			return makeError(response, errors.forbidden.n);

		if (!config.enabled('item-ids'))
			return makeError(response, errors.itemid_required.n);
		var itemID = makeRandomId();

		var now = new Date();

		var item = xmpp.stanza('item', {xmlns: NS.PUBSUB, id: itemID});
		item.c('entry', {xmlns: NS.ATOM});
		if (stanza.getChild('subject'))
			item.c('title').t(stanza.getChild('subject')).up();
		item.c('summary').t(request.getText()).up();
		item.c('id').t(itemID).up();
		item.c('published').t(now.toString()).up();
		item.c('updated').t(now.toString()).up();

		if (!config.enabled('persistent-items')) {
			var notifications = storage.purgeNode(nodeID);
			if (typeof notifications == 'number')
				return makeError(response, r);
		}

		if (autocreate) {
			if (!form)
				form = {};
			form['pubsub#creator'] = JID.toBare(to);

			var r = storage.createNode(nodeID, form);
			if (typeof r == 'number')
				return makeError(response, r);
		}

		var content = item.getChild();

		subscribers = storage.setItem(nodeID, itemID, content);
		if (typeof subscribers == 'number')
			return makeError(response, subscribers);

		var attrs = {};
		if (content)
			attrs[itemID] = {content: content};
		else
			attrs[itemID] = {};
		notifs.send(subscribers, 'items', nodeID, attrs);

		response.c('pubsub', {xmlns: NS.PUBSUB})
			.c('publish', {node: nodeID})
			.c('item', {id: itemID});

		return response;
	}
}