<commit_before>var request = require('request');
var qs = require('querystring');
var models = require('../../../models');
var async = require('async');
var util = require('util');

var discussion_google_to_objid = function(value, idx, collection) {
    return discussion_google_to_objid.mapping[value];
};
discussion_google_to_objid.mapping = {
    'entry.6.group':  '6',
    'entry.7.group':  '502cefe6abfc52020000002a',
    'entry.25.group': '502117271aff910200000c14',
    'entry.23.group': '23',
    'entry.21.group': '21',
    'entry.19.group': '5022def369668c0200020d1e',
    'entry.17.group': '17',
    'entry.15.group': '15',
    'entry.13.group': '5023af9b61a325020000efbe',
    'entry.11.group': '501e69e17555f60200001f2e',
    'entry.9.group':  '502a91a90893a502000000ce',
    'entry.31.group': '5030eaf0e840450200000412',
    'entry.33.group': '4ff436ba47d7fa010000071f',
    'entry.35.group': '4fcdf7180a381201000005b3'
};


module.exports = function(req, res) {
    var user = req.session.user;
    if (!user) {
        res.json(403 , "not_loged_in");
        return;
    }
    if (user.has_voted && user.has_voted.length) {
        res.json(403 , "has_voted");
        return;
    }
    // Two lines of google voodoo
    req.body.backupCache = '';
    req.body.submit = 'Submit';
    // Record the user's IP
    req.body['entry.39.single'] = ('x-forwarded-for' in req.headers) ? req.headers['x-forwarded-for'] : req.connection.remoteAddress;
    // e-mail
    req.body['entry.40.single'] = user.email || "";
    // e-mail
    req.body['entry.41.group'] = user.facebook_id ? "X" : "";
    // e-mail
    req.body['entry.43.single'] = util.format("%s %s", user.first_name, user.last_name);

    var vote_data = qs.stringify(req.body);
    var form_key = (req.app.settings.env == 'production') ? 'dDI5dUg1TFMzUENKc09lSXp5aTh2alE6MQ' : '<KEY>';

    async.series([
        // send post to google
        function(cbk) {
            request.post({
                uri:  util.format('https://docs.google.com/spreadsheet/formResponse?formkey=%s&ifq', form_key),
                headers: { 'content-type': 'application/x-www-form-urlencoded' },
                method: 'POST',
                body: vote_data
            },
            cbk);
        },
        // mark voting on user
        function (cbk) {
            // this is for mongoose compatibility (breaks when nested obj has '.' in name)
            var discussion_ids = Object.keys(req.body).map(discussion_google_to_objid).filter(function(x){return x});
            models.User.findByIdAndUpdate(user._id, {has_voted: discussion_ids}, cbk);
        },
        // delete user from session if needed
        function(cbk) {
            if(!req.session.delete) {
                cbk();
                return;
            }
            delete req.session.user;
            req.session.save(cbk);
        }],
        // respond
        function(err) {
            if (err){
                res.statusCode = 500;
            }
            res.json(err || "ok")
        }
    );
};

<commit_msg>pull lior changes<commit_after>var request = require('request');
var qs = require('querystring');
var models = require('../../../models');
var async = require('async');
var util = require('util');

var discussion_google_to_objid = function(value, idx, collection) {
    return discussion_google_to_objid.mapping[value];
};
discussion_google_to_objid.mapping = {
    'entry.6.group':  '6',
    'entry.7.group':  '502cefe6abfc52020000002a',
    'entry.25.group': '502117271aff910200000c14',
    'entry.23.group': '23',
    'entry.21.group': '21',
    'entry.19.group': '5022def369668c0200020d1e',
    'entry.17.group': '17',
    'entry.15.group': '15',
    'entry.13.group': '5023af9b61a325020000efbe',
    'entry.11.group': '501e69e17555f60200001f2e',
    'entry.9.group':  '502a91a90893a502000000ce',
    'entry.31.group': '5030eaf0e840450200000412',
    'entry.33.group': '4ff436ba47d7fa010000071f',
    'entry.35.group': '4fcdf7180a381201000005b3'
};


module.exports = function(req, res) {
    var user = req.session.user;
    if (!user) {
        res.json(403 , "not_loged_in");
        return;
    }
    if (user.has_voted && user.has_voted.length) {
        res.json(403 , "has_voted");
        return;
    }
    // Two lines of google voodoo
    req.body.backupCache = '';
    req.body.submit = 'Submit';
    // Record the user's IP
    req.body['entry.39.single'] = ('x-forwarded-for' in req.headers) ? req.headers['x-forwarded-for'] : req.connection.remoteAddress;
    // e-mail
    req.body['entry.40.single'] = user.email || "";
    // e-mail
    req.body['entry.41.group'] = user.facebook_id ? "X" : "";
    // e-mail
    req.body['entry.43.single'] = util.format("%s %s", user.first_name, user.last_name);

    var vote_data = qs.stringify(req.body);
    var form_key = (req.app.settings.env == 'production') ? 'dDI5dUg1TFMzUENKc09lSXp5aTh2alE6MQ' : '<KEY>';

    async.series([
        // send post to google
        function(cbk) {
            request.post({
                uri:  util.format('https://docs.google.com/spreadsheet/formResponse?formkey=%s&ifq', form_key),
                headers: { 'content-type': 'application/x-www-form-urlencoded' },
                method: 'POST',
                body: vote_data
            },
            cbk);
        },
        // mark voting on user
        function (cbk) {
            // this is for mongoose compatibility (breaks when nested obj has '.' in name)
            var discussion_ids = Object.keys(req.body).map(discussion_google_to_objid).filter(function(x){return x});
            models.User.findByIdAndUpdate(user._id, {has_voted: discussion_ids}, cbk);
        },
        // delete user from session if needed
        function(cbk) {
            if(!req.session.delete) {
                cbk();
                return;
            }
            delete req.session.user;
            req.session.save(cbk);
        }],
        // respond
        function(err) {
            if (err){
                res.statusCode = 500;
            }
            res.json(err || user._id)
        }
    );
};


{'fraction_non_alphanumeric': 0.1581266318537859, 'fraction_numerical': 0.0714751958224543, 'mean_word_length': 2.312972972972973, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '5606939', 'n_tokens_mistral': 2361, 'n_tokens_neox': 2085, 'n_words': 348}