2013-01-15 17:21:43 +01:00
|
|
|
var _ = require('underscore')._;
|
|
|
|
|
|
|
|
var pages = function(dbot) {
|
|
|
|
var pages = {
|
|
|
|
// Shows the results of a poll
|
2013-05-06 20:12:25 +02:00
|
|
|
'/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 {
|
|
|
|
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
|
2013-04-30 21:33:26 +02:00
|
|
|
'/poll': function(req, res) {
|
2013-05-06 20:12:25 +02:00
|
|
|
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-04-30 21:33:26 +02:00
|
|
|
}
|
2013-01-15 17:21:43 +01:00
|
|
|
};
|
|
|
|
return pages;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return pages(dbot);
|
|
|
|
};
|