3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-27 21:22:38 +01:00
dbot/modules/poll/pages.js
2014-09-04 14:48:20 +00:00

64 lines
2.1 KiB
JavaScript

var _ = require('underscore')._,
async = require('async');
var pages = function(dbot) {
var pages = {
// Shows the results of a poll
'/poll/:key': function(req, res) {
var key = req.params.key.toLowerCase();
this.db.read('poll', key, function(err, poll) {
if(!err) {
console.log(poll);
var totalVotes = _.reduce(poll.votes, function(memo, option) {
return memo += option;
}, 0);
var voterNicks = [];
async.each(_.keys(poll.votees), function(id, done) {
dbot.api.users.getUser(id, function(err, user) {
voterNicks.push(user.primaryNick);
done();
});
}, function() {
res.render('polls', {
'name': dbot.config.name,
'description': poll.description,
'votees': voterNicks,
'options': poll.votes,
'totalVotes': totalVotes,
locals: {
'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) {
var pollKeys = [];
this.db.scan('poll', function(result) {
if(result) pollKeys.push(result.name);
}, function(err) {
console.log(pollKeys);
res.render('polllist', {
'name': dbot.config.name,
'polllist': pollKeys
});
});
}
};
return pages;
};
exports.fetch = function(dbot) {
return pages(dbot);
};