dbot/modules/poll/pages.js

64 lines
2.1 KiB
JavaScript
Raw Normal View History

2014-01-29 23:42:16 +01:00
var _ = require('underscore')._,
async = require('async');
2013-01-15 17:21:43 +01:00
var pages = function(dbot) {
var pages = {
// Shows the results of a poll
'/poll/:key': function(req, res) {
2013-01-15 17:21:43 +01:00
var key = req.params.key.toLowerCase();
2013-05-06 21:43:14 +02:00
this.db.read('poll', key, function(err, poll) {
if(!err) {
2014-01-29 23:42:16 +01:00
console.log(poll);
2013-05-06 21:43:14 +02:00
var totalVotes = _.reduce(poll.votes, function(memo, option) {
2013-01-15 17:21:43 +01:00
return memo += option;
}, 0);
2013-05-06 21:43:14 +02:00
var voterNicks = [];
2014-01-29 23:42:16 +01:00
async.each(_.keys(poll.votees), function(id, done) {
2014-09-04 16:48:20 +02:00
dbot.api.users.getUser(id, function(err, user) {
2014-01-29 23:42:16 +01:00
voterNicks.push(user.primaryNick);
done();
2013-05-06 21:43:14 +02:00
});
2014-01-29 23:42:16 +01:00
}, function() {
2013-05-06 21:43:14 +02:00
res.render('polls', {
'name': dbot.config.name,
'description': poll.description,
'votees': voterNicks,
'options': poll.votes,
2014-01-29 23:42:16 +01:00
'totalVotes': totalVotes,
2013-05-06 21:43:14 +02:00
locals: {
'url_regex': RegExp.prototype.url_regex()
}
});
2014-01-29 23:42:16 +01:00
2013-05-06 21:43:14 +02:00
});
} else {
res.render('error', {
'name': dbot.config.name,
'message': 'No polls under that key.'
});
}
});
},
2013-01-15 17:21:43 +01:00
// 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
});
2013-01-15 17:21:43 +01:00
});
}
2013-01-15 17:21:43 +01:00
};
return pages;
};
exports.fetch = function(dbot) {
return pages(dbot);
};