view nodes.js @ 27:a36a514e8be8

Learned that “hg addremove” can be good, sometimes.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 01 Nov 2010 02:41:04 +0100
parents 95fc43d1bd54
children 6697f394301f
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/>.
 */

require('./iso8601');
var errors = require('./errors');
var config = require('./configuration');
var service_configuration = config.service_configuration;
var Configuration = config.Configuration;
var makeRandomId = require('./util').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;
	},
};