3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-26 12:42:51 +01:00
dbot/modules/poll/pages.js

66 lines
2.3 KiB
JavaScript
Raw Normal View History

2013-01-15 17:21:43 +01:00
var _ = require('underscore')._;
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) {
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 = [];
/* TODO: Fix stupid fucking async issue bullshit
var voterNicks = _.map(poll.votees, function(vote, id) {
dbot.api.users.getUser(id, function(user) {
return user.primaryNick;
});
});*/
process.nextTick(function() {
console.log(voterNicks);
res.render('polls', {
'name': dbot.config.name,
'description': poll.description,
'votees': voterNicks,
'options': poll.votes,
locals: {
'totalVotes': totalVotes,
'url_regex': RegExp.prototype.url_regex()
}
});
});
} else {
console.log(err);
console.log("the thing the thing");
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);
};