3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-25 20:22:39 +01:00
dbot/modules/poll/pages.js

46 lines
1.5 KiB
JavaScript
Raw Normal View History

2013-01-15 17:21:43 +01:00
var _ = require('underscore')._;
var pages = function(dbot) {
var polls = dbot.db.polls;
var pages = {
// Shows the results of a poll
'/polls/:key': function(req, res) {
var key = req.params.key.toLowerCase();
if(_.has(dbot.db.polls, key)) {
var totalVotes = _.reduce(dbot.db.polls[key].votes,
function(memo, option) {
return memo += option;
}, 0);
res.render('polls', {
'name': dbot.config.name,
'description': dbot.db.polls[key].description,
'votees': Object.keys(dbot.db.polls[key].votees),
'options': dbot.db.polls[key].votes,
locals: {
'totalVotes': totalVotes,
'url_regex': RegExp.prototype.url_regex()
}
});
} else {
res.render('error', {
'name': dbot.config.name,
'message': 'No polls under that key.'
});
}
},
// Lists all of the polls
'/poll': function(req, res) {
2013-01-15 17:21:43 +01:00
res.render('polllist', {
'name': dbot.config.name,
'polllist': Object.keys(dbot.db.polls)
});
}
2013-01-15 17:21:43 +01:00
};
return pages;
};
exports.fetch = function(dbot) {
return pages(dbot);
};