<commit_before>var Promise = require('bluebird');
var request = Promise.promisifyAll(require('request'));
var socketio = require('../config/socketio');

var Poll = require('../models/Poll');
var PollOption = require('../models/PollOption');

module.exports.start = function (roomMember, command) {
	var match = /^\/poll?(?:\s+(.+)?|$)/ig.exec(command);
	var question = match[1];
	var defaultPollOptions = ["True", "False", "I don't care"];
	var optionVotes = [];

	return Poll.findOne({room: roomMember.room, isOpen: true}).populate('user')
		.then(function (poll) {
			if(poll) {
				return defaultPollResponse(poll);
			} else if(!poll && !question) {
				return {activePoll: false, hasQuestion: false};
			}



			_.each(defaultPollOptions, function(option, index){
				optionVotes.push(0);
			});
			return Poll.create({
				user: roomMember.user,
				room: roomMember.room,
				question: question,
			}).then(function (newPoll) {

				return Promise.each(defaultPollOptions, function(option, index) {
					return PollOption.create({
						poll: newPoll._id,
						optionString: option,
						optionNumber: index + 1,
						numberOfVotes: 0,
					});
				})
					.spread(function(option1, option2, option3) {
						return defaultPollResponse(newPoll);
					});
			});
		});
};

module.exports.vote = function(roomMember, command) {
	var match = /^\/vote?(?:\s+(.+)?|$)/ig.exec(command);
	var optionNumber = match[1];

	return Poll.findOne({room: roomMember.room, isOpen: true}).populate('user')
		.then(function (activePoll) {

			var respondees = activePoll.respondees;
			if (respondees.indexOf(roomMember.user._id) != -1) {
				return {newVote: false, message: "You have already voted"}

			} else {
				return PollOption.findOne({poll: activePoll._id, optionNumber: optionNumber})
					.then(function (selectedOption) {
						var num = parseInt(selectedOption.numberOfVotes);
						num = num + 1;
						selectedOption.numberOfVotes = num;

						respondees.push(roomMember.user._id);

						activePoll.resondees = respondees;

						return Promise.join(
							selectedOption.save(),
							activePoll.save(),
							updatedVotePollResponse(activePoll)
						)
							.spread(function(pollOpts,activePoll, response) {
								return response;
							});
					});
			}
		});
};

module.exports.close = function(roomMember, command) {
	var match = /^\/poll(\s?)close?(?:\s*)/ig.exec(command);
	var optionNumber = match[1];

	return Poll.findOne({room: roomMember.room, isOpen: true}).populate('user')
		.then(function (activePoll) {
			// must create error handling for no active polls
			if (!activePoll) {
				return {activePoll: false};
			}

			if ( roomMember.role == 'administrator' || roomMember.role == 'moderator' || roomMember.user._id == activePoll.user._id ) {
				activePoll.isOpen = false;

				return Promise.join(
					activePoll.save(),
					finalPollResults(activePoll)
				)
					.spread(function(poll, response) {
						return response;
					});

			} else {
				// you do not have perms to close poll
				return {activePoll: true, canClose: false};
				//return defaultPollResponse(activePoll);
			}
		});
};

function finalPollResults(newPoll) {
	var user = newPoll.user.nick;
	var question = newPoll.question;
	var responseString = [];
	responseString.push(user + " is asking: " + question);

	return PollOption.find({poll: newPoll._id})
		.then(function (pollOptions) {
			var ascendingSort = _.sortBy(pollOptions, "numberOfVotes");
			var sortedList = ascendingSort.reverse();
			_.each(pollOptions, function(option, index) {
				var optionString = option.optionString;
				var optionNum = option.optionNumber;<commit_msg>Fixing some error handling for closing polls<commit_after>var Promise = require('bluebird');
var request = Promise.promisifyAll(require('request'));
var socketio = require('../config/socketio');

var Poll = require('../models/Poll');
var PollOption = require('../models/PollOption');

module.exports.start = function (roomMember, command) {
	var match = /^\/poll?(?:\s+(.+)?|$)/ig.exec(command);
	var question = match[1];
	var defaultPollOptions = ["True", "False", "I don't care"];
	var optionVotes = [];

	return Poll.findOne({room: roomMember.room, isOpen: true}).populate('user')
		.then(function (poll) {
			if(poll) {
				return defaultPollResponse(poll);
			} else if(!poll && !question) {
				return {activePoll: false, hasQuestion: false};
			}



			_.each(defaultPollOptions, function(option, index){
				optionVotes.push(0);
			});
			return Poll.create({
				user: roomMember.user,
				room: roomMember.room,
				question: question,
			}).then(function (newPoll) {

				return Promise.each(defaultPollOptions, function(option, index) {
					return PollOption.create({
						poll: newPoll._id,
						optionString: option,
						optionNumber: index + 1,
						numberOfVotes: 0,
					});
				})
					.spread(function(option1, option2, option3) {
						return defaultPollResponse(newPoll);
					});
			});
		});
};

module.exports.vote = function(roomMember, command) {
	var match = /^\/vote?(?:\s+(.+)?|$)/ig.exec(command);
	var optionNumber = match[1];

	return Poll.findOne({room: roomMember.room, isOpen: true}).populate('user')
		.then(function (activePoll) {

			var respondees = activePoll.respondees;
			if (respondees.indexOf(roomMember.user._id) != -1) {
				return {newVote: false, message: "You have already voted"}

			} else {
				return PollOption.findOne({poll: activePoll._id, optionNumber: optionNumber})
					.then(function (selectedOption) {
						var num = parseInt(selectedOption.numberOfVotes);
						num = num + 1;
						selectedOption.numberOfVotes = num;

						respondees.push(roomMember.user._id);

						activePoll.resondees = respondees;

						return Promise.join(
							selectedOption.save(),
							activePoll.save(),
							updatedVotePollResponse(activePoll)
						)
							.spread(function(pollOpts,activePoll, response) {
								return response;
							});
					});
			}
		});
};

module.exports.close = function(roomMember, command) {
	var match = /^\/poll(\s?)close?(?:\s*)/ig.exec(command);
	var optionNumber = match[1];
	var userId = roomMember.user._id;

	return Poll.findOne({room: roomMember.room, isOpen: true}).populate('user')
		.then(function (activePoll) {
			// must create error handling for no active polls
			if (!activePoll) {
				return {activePoll: false};
			}

			var ownerId = activePoll.user._id;
			if ( roomMember.role == 'administrator' || roomMember.role == 'moderator' || ownerId.equals(userId) ) {
				activePoll.isOpen = false;

				return Promise.join(
					activePoll.save(),
					finalPollResults(activePoll)
				)
					.spread(function(poll, response) {
						return response;
					});

			} else {
				// you do not have perms to close poll
				return {activePoll: true, canClose: false};
				//return defaultPollResponse(activePoll);
			}
		});
};

function finalPollResults(newPoll) {
	var user = newPoll.user.nick;
	var question = newPoll.question;
	var responseString = [];
	responseString.push(user + " is asking: " + question);

	return PollOption.find({poll: newPoll._id})
		.then(function (pollOptions) {
			var ascendingSort = _.sortBy(pollOptions, "numberOfVotes");
			var sortedList = ascendingSort.reverse();
			_.each(pollOptions, function(option, index) {
				var optionString = option.optionString;
				var optionNum = option.optionNumber;
{'fraction_non_alphanumeric': 0.14979702300405953, 'fraction_numerical': 0.002976995940460081, 'mean_word_length': 3.55672009864365, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4203507', 'n_tokens_mistral': 2904, 'n_tokens_neox': 2357, 'n_words': 492}