view notifs.js @ 58:b98e545a94f7 default tip

Always use children instead of tags. Might break something.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 13 Sep 2011 00:54:55 +0200
parents 99bd1d1ac071
children
line wrap: on
line source

'use strict';

var Element = require('ltx').Element;
var storage = require('./storage');
var config = require('./configuration');
var conn;

function _(obj, color) {
	var str = require('sys').inspect(obj, false, null);
	if (color)
		console.log('\x1b['+c+';1m' + str + '\x1b[0m');
	else
		console.log(str);
};

exports.send = function(notifs, type, nodeID, a1, a2) {
	var ev = new Element('event', {xmlns: 'http://jabber.org/protocol/pubsub#event'});

	if (type == 'affiliations') {
		ev.attrs.xmlns = 'http://jabber.org/protocol/pubsub';

		var args = {};
		for (i in a1) {
			var attrs = a1[i];
			if (i == 'affiliation')
				args.affiliation = attrs;
			else if (i == 'jid')
				args.jid = attrs;
		}
		var affiliations = new Element('affiliations', {node: nodeID})
			.c('affiliation', args);
		ev.cnode(affiliations);
	} else if (type == 'collection') {
		var collection = new Element('collection', {node: nodeID});
		if (a1 == 'associate')
			collection.cnode('associate', {node: nodeID});
		else
			collection.cnode('disassociate', {node: nodeID});
		ev.cnode(collection);
	} else if (type == 'configuration') {
		if (!config.enabled('config-node')) {
			_('Error #4', 41)
			return;
		}

		var configuration = new Element('configuration', {node: nodeID});
		if (a1) {
			var x = forms.build('node_config', config.service_configuration.node_config, storage.getConfiguration(nodeID));
			if (x)
				configuration.cnode(x); //TODO: voir exemple 150
		}
		ev.cnode(configuration);
	} else if (type == 'delete') {
		var del = new Element('delete', {node: nodeID});
		if (a1)
			del.c('redirect', {uri: a1});
		ev.cnode(del);
	} else if (type == 'items') {
		var items = new Element(type, {node: nodeID});
		if (a2 == 'retract')
			for (var i in a1)
				items.s('retract', {id: i});
		else {
			for (var i in a1) {
				var item = a1[i];
				var args = {};
				if (i != '')
					args.id = i;
				if (item.node)
					args.node = item.node;
				if (item.publisher)
					args.publisher = item.publisher;
				var it = new Element('item', args);
				if (item.content)
					it.cnode(item.content);
				items.cnode(it);
			}
		}
		ev.cnode(items);
	} else if (type == 'purge') {
		ev.c('purge', {node: nodeID});
	} else if (type == 'subscription') {
		if (!config.enabled('subscription-notifications'))
			return;

		var args = {node: nodeID};
		for (i in a1) {
			var attrs = a1[i];
			if (i == 'subscription') {
				if (attrs == 'none' || attrs == 'pending' || attrs == 'subscribed' || attrs == 'unconfigured')
					args[i] = attrs;
				else {
					_('Error #3', 41)
					return;
				}
			} else if (i == 'jid' || i == 'subid')
				args[i] = attrs;
			else if (i == 'expiry')
				args[i] = attrs.toString();
		}
		if (!args.jid || args.jid == '') {
			_('Error #2', 41)
			return;
		}
		var sub = new Element('subscription', args);
		ev.cnode(sub);
	} else {
		_('Error #1', 41)
		return;
	}

	var subs;
	if (typeof notifs == 'string') {
		subs = {};
		subs[notifs] = storage.getSubscription(notifs, nodeID);
	} else
		subs = notifs;

	var affils = storage.getAffiliationsFromNodeID(nodeID);
	for (var i in affils)
		if (!(i in subs) || affils[i] == 'owner')
			subs[i] = new config.Configuration(config.service_configuration.subscribe_options);

	for (var i in subs) {
		var sub = subs[i];

		if (sub.options) {
			if (typeof sub.options['pubsub#deliver'] != 'undefined' && !sub.options['pubsub#deliver'])
				continue;

			if (typeof sub.options['pubsub#digest'] != 'undefined' && sub.options['pubsub#digest']) {
				if (!sub.digest)
					sub.digest = [];
				sub.digest.push(ev)

				if (sub.digestTimeout)
					continue;

				var freq;
				if (typeof sub.options['pubsub#digest_frequency'] == 'undefined')
					freq = 0;
				else
					freq = parseInt(sub.options['pubsub#digest_frequency']);

				if (freq == 0)
					freq = 24*60*60*1000;

				setTimeout(sendDigest, freq, notifs[i], nodeID);
				sub.digestTimeout = true;
				continue;
			}
		}

		var message = new Element('message', {to: i, from: config.jid, id: conn.getUniqueId(), type: 'headline'});
		message.cnode(ev);
		conn.send(message);
	}
}

exports.sendDigest = function(jid, nodeID) {
	var sub = storage.getSubscription(jid, nodeID);
	if (sub.digestTimeout)
		sub.digestTimeout = false;

	var message = new Element('message', {to: jid, from: config.jid, id: conn.getUniqueId(), type: 'headline'});
	for (var i in sub.digest)
		message.cnode(sub.digest[i]);
	conn.send(message);
}

exports.setConnection = function(c) {
	conn = c;
}