Mercurial > psgxs
diff nodes.js @ 0:9ee956af41e3
Initial commit
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 27 Jun 2010 22:05:12 +0200 |
parents | |
children | c2954a9e5665 |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/nodes.js @@ -0,0 +1,93 @@ +/* + * 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 = ['lm@slam']; + 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) + subid = makeRandomId(); + + this.subscribers[jid] = { + type: type, + subid: subid, + options: new Configuration(service_configuration.subscribe_options, params), + } + + this.metadata['pubsub#num_subscribers']++; + + return subid; + }, +};