view modules/http/mod_atom.js @ 41:bc717575e66a

Much better handling of modules.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 20 Feb 2011 15:51:12 +0100
parents 62cbb1c49bc5
children 023f767662d3
line wrap: on
line source

exports.atom = {
	url: /^\/atom/,
	func: function (req, res) {
		var url = require('url').parse(req.url);
		nodeID = url.pathname.substr(url.pathname.indexOf('/', 1)+1);

		var children;
		if (nodeID && nodeID != '') {
			var md = storage.getMetadata(nodeID);
			if (md['pubsub#type'] != NS.ATOM)
				return false;

			var response = xmpp.stanza('feed', {xmlns: 'http://www.w3.org/2005/Atom'});
			res.writeHead(200, {'Content-Type': 'text/xml'});
			if (!storage.existsNode(nodeID))
				return false;

			if (md['pubsub#title'])
				response.c('title').t(md['pubsub#title']).up();
			if (md['pubsub#description'])
				response.c('subtitle').t(md['pubsub#description']).up();
			if (md['pubsub#creation_date'])
				response.c('published').t(md['pubsub#creation_date'].toString()).up();

			children = storage.getItems(nodeID);
			if (typeof children == 'number')
				return false;

			for (var i in children)
				response.cnode(children[i].content).up();
		} else {
			res.writeHead(200, {'Content-Type': 'text/xml'});
			var response = xmpp.stanza('ul', {xmlns: 'http://www.w3.org/1999/xhtml'});

			children = storage.getChildren();
			if (typeof children == 'number')
				return false;

			for (var i in children)
				response.c('li').c('a', {href: i}).t(i).up().up();
		}

		res.end(response.toString());

		return true;
	},
}