view modules/http/mod_atom.js @ 43:023f767662d3

Fix compatibility with strict mode of node 0.4 and some files without licence header.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 01 Mar 2011 11:58:15 +0100
parents bc717575e66a
children fd69d35cf2e6
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';

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;
	},
}