view modules/mod_retrieve.js @ 57:addbf6bbfaa8

Various fixes for the migration to ltx.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 12 Sep 2011 23:45:00 +0200
parents c2c9c06d6e9d
children
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';

// SECTION 5.6: Retrieve Subscriptions
exports.retrieveSubscriptions = {
	type: 'get',
	child: 'pubsub',
	ns: NS.PUBSUB,
	child2: 'subscriptions',
	func: function(response, stanza, request, to) {
		if (!config.enabled('retrieve-subscriptions'))
			return makeError(response, errors.subscriptions_retrieval_not_supported.n);

		var nodeID = request.getAttribute('node');
		if (nodeID && nodeID != '') {
			if (!storage.existsNode(nodeID))
				return makeError(response, errors.node_does_not_exist.n);
			var subs = storage.getSubscription(JID.toBare(to), nodeID);
		} else
			var subs = storage.getSubscription(JID.toBare(to));

		response.c('pubsub', {xmlns: NS.PUBSUB})
			.c('subscriptions');

		for (i in subs)
			response.c('subscription', {node: i, jid: to, subscription: subs[i].type, subid: subs[i].subid}).up();

		return response;
	}
}

// SECTION 5.7: Retrieve Affiliations
exports.retrieveAffiliations = {
	type: 'get',
	child: 'pubsub',
	ns: NS.PUBSUB,
	child2: 'affiliations',
	func: function(response, stanza, request, to) {
		if (!config.enabled('retrieve-affiliations'))
			return makeError(response, errors.affiliations_retrieval_not_supported.n);

		var nodeID = request.getAttribute('node');
		if (nodeID && nodeID != '') {
			if (!storage.existsNode(nodeID))
				return makeError(response, errors.node_does_not_exist.n);
			var affils = {};
			affils[nodeID] = storage.getAffiliation(JID.toBare(to), nodeID);
		} else
			var affils = storage.getAffiliationsFromJID(JID.toBare(to));

		response.c('pubsub', {xmlns: NS.PUBSUB})
			.c('affiliations');

		for (i in affils)
			response.c('affiliation', {node: i, affiliation: affils[i]}).up();

		return response;
	}
}

// SECTION 6.5: Retrieve Items from a Node
exports.retrieveItems = {
	type: 'get',
	child: 'pubsub',
	ns: NS.PUBSUB,
	child2: 'items',
	func: function(response, attrs, request, to) {
		if (!config.enabled('retrieve-items'))
			return makeError(response, errors.sub.default_options.node_configuration_not_supported.n);

		var nodeID = request.getAttribute('node');
		if (!nodeID || nodeID == '')
			return makeError(response, errors.nodeid_required.n);
		if (!storage.existsNode(nodeID))
			return makeError(response, errors.node_does_not_exist.n);

		var configuration = storage.getConfiguration(nodeID);
		if (configuration['pubsub#access_model'] == 'whitelist') {
			var affil = storage.getAffiliation(JID.toBare(to), nodeID);
			if (affil != 'super-owner' && affil != 'owner' && affil != 'publisher' && affil != 'member')
				return makeError(response, errors.pub.publish.insufficient_privileges.n);
		}

		var item = [];
		for (var i=0; i<request.children.length; i++) {
			var j = request.children[i];
			if (j.name == 'item' && j.attr['id'] && j.attr['id'] != '')
				item.push(j.attr['id']);
		}

		var max_items = request.getAttribute('max_items');
		if (max_items)
			max_items = Number (max_items);

		var items = new Element('items', {node: nodeID});

		response.c('pubsub', {xmlns: NS.PUBSUB})
			.cnode(items);

		if (item.length) {
			for (var i=0; i<item.length; i++) {
				var j = storage.getItem(nodeID, item[i]);
				if (typeof j == 'number')
					return makeError(response, j);
				if (j == errors.success)
					continue;

				items.c('item', {id: item[i]})
					.cnode(j).up().up();
			}
		} else {
			var j;
			if (max_items)
				j = storage.getLastItem(nodeID, max_items);
			else
				j = storage.getItems(nodeID);
			if (typeof j == 'number')
				return makeError(response, j);

			var k = 0;
			for (var i in j)
				items.c('item', {id: i}).t(j[i].content).up();
		}

		return response;
	}
}