view nodes.js @ 48:c979c4d0932b

Little fixes for strict mode compatibility.
author Sonny Piers <sonny.piers@gmail.com>
date Sat, 19 Mar 2011 14:57:13 +0100
parents 023f767662d3
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';

require('./iso8601');
var errors = require('./errors');
var config = require('./configuration');
var service_configuration = config.service_configuration;
var Configuration = config.Configuration;
var makeRandomId = require('./fdsq').makeRandomId;

exports.Item = function() {
	this.content = null;
	this.date = new Date();
	return this;
};

exports.Node = function(params) {
	this.items = {};
	if (config.enabled('subscribe'))
		this.subscribers = {};

	this.owner = [];
	if (params && params['pubsub#creator'])
		this.owner.push(params['pubsub#creator']);

	if (config.enabled('publisher-affiliation'))
		this.publisher = [];

	if (config.enabled('publish-only-affiliation'))
		this.publishOnly = [];

	if (config.enabled('member-affiliation'))
		this.member = [];

	if (config.enabled('outcast-affiliation'))
		this.outcast = [];

	if (config.enabled('meta-data'))
		this.metadata = new Configuration(service_configuration.node_metadata, params);

	if (config.enabled('config-node'))
		this.configuration = new Configuration(service_configuration.node_config, params);

	if (config.enabled('subscription-options'))
		this.subsConfig = new Configuration(service_configuration.subscribe_options, params);

	return this;
};

exports.Node.prototype = {
	setItem: function(name, content) {
		if (typeof content == 'undefined') {
			if (this.items[name]) {
				delete this.items[name];
				return errors.success;
			}
			return 42; //XXX
		}

		if (!this.items[name])
			this.items[name] = new exports.Item();

		this.items[name].content = content;

		return errors.success;
	},

	setSubscriber: function(jid, type, subid, params) {
		if (type == 'none') {
			delete this.subscribers[jid];
			this.metadata['pubsub#num_subscribers']--;
			return errors.success;
		}

		if (!subid) {
			if (this.subscribers[jid])
				subid = this.subscribers[jid].subid;
			else
				subid = makeRandomId();
		}

		this.subscribers[jid] = {
			type: type,
			subid: subid,
			options: new Configuration(service_configuration.subscribe_options, params),
		}

		this.metadata['pubsub#num_subscribers']++;

		return subid;
	},
};