<commit_before>var fs = require('fs');
var path = require('path');
var util = require('util');
var byline = require('byline');

var votesFilePath = path.join(__dirname, 'votes.txt');

exports.login = function (req, res) {
    
    if (req.body.username == null || req.body.username == '') {
        res.setHeader('WWW-Authenticate', 'realm="My realm"');
        res.render('login');
    } else {
        req.session.username = req.body.username;
        
        res.redirect('/vote');
    }
};

exports.vote = function (req, res) {
    
    saveVote(req);
    
    res.redirect('/results');
};

exports.voteSafe = function (req, res) {
    
    if (isCrsfTokenPresent(req) && isCrsfTokenValid(req)) {
        saveVote(req);
    }
    
    res.redirect('/results');
};

exports.results = function (req, res) {
    var stream = fs.createReadStream(votesFilePath);
    stream = byline.createStream(stream);
    
    var counts = {
        'SF': 0,
        'SEA': 0
    };
    var votes = [];
    
    stream.on('data', function (line) {
        var fields = line.toString().split('\t');
        
        var vote = {
            date: fields[0],
            username: fields[1],
            vote: fields[2]
        };
        votes.push(vote);
        
        counts[vote.vote]++;
    });
    
    stream.on('end', function () {
        var viewModel = {
            votes: votes,
            counts: counts
        };
    
        res.render('results', viewModel);
    });
};

exports.voteSearch = function (req, res) {
    
    searchVotes(req, res, 'vote-search');
};

exports.voteSearchSafe = function (req, res) {

    searchVotes(req, res, 'vote-search-safe');
};

var saveVote = function (req) {
    
    var now = new Date();
    var date = util.format('%d/%d/%d %d:%d:%d', now.getMonth() + 1, 
        now.getDate(), now.getFullYear(), now.getHours(), now.getMinutes(), 
        now.getSeconds());
    var text = util.format('%s\t%s\t%s\n', date, req.session.username, 
        req.body.vote);
    
    fs.appendFile(votesFilePath, text);
};

var isCrsfTokenPresent = function (req) {
    
    return (req.body.csrfToken != null && req.body.csrfToken != null);
};

var isCrsfTokenValid = function (req) {
    
    return (req.body.csrfToken == req.cookies.csrfToken);
};

var searchVotes = function(req, res, viewName) {
        
    var username = req.query.username || '';
    var viewModel = {
        username: username
    };
    
    if (username != '') {

        var stream = fs.createReadStream(votesFilePath);
        stream = byline.createStream(stream);

        var votes = [];
    
        stream.on('data', function (line) {
            var fields = line.toString().split('\t');
        
            if (username == fields[1]) {
                var vote = {
                    date: fields[0],
                    username: fields[1],
                    vote: fields[2]
                };
                votes.push(vote);
            }
        });

        stream.on('end', function () {
            viewModel.votes = votes;
    
            res.render(viewName, viewModel);
        });
    } else {
        res.render(viewName, viewModel);
    }
};<commit_msg>Vote search is no longer case specific<commit_after>var fs = require('fs');
var path = require('path');
var util = require('util');
var byline = require('byline');

var votesFilePath = path.join(__dirname, 'votes.txt');

exports.login = function (req, res) {
    
    if (req.body.username == null || req.body.username == '') {
        res.setHeader('WWW-Authenticate', 'realm="My realm"');
        res.render('login');
    } else {
        req.session.username = req.body.username;
        
        res.redirect('/vote');
    }
};

exports.vote = function (req, res) {
    
    saveVote(req);
    
    res.redirect('/results');
};

exports.voteSafe = function (req, res) {
    
    if (isCrsfTokenPresent(req) && isCrsfTokenValid(req)) {
        saveVote(req);
    }
    
    res.redirect('/results');
};

exports.results = function (req, res) {
    var stream = fs.createReadStream(votesFilePath);
    stream = byline.createStream(stream);
    
    var counts = {
        'SF': 0,
        'SEA': 0
    };
    var votes = [];
    
    stream.on('data', function (line) {
        var fields = line.toString().split('\t');
        
        var vote = {
            date: fields[0],
            username: fields[1],
            vote: fields[2]
        };
        votes.push(vote);
        
        counts[vote.vote]++;
    });
    
    stream.on('end', function () {
        var viewModel = {
            votes: votes,
            counts: counts
        };
    
        res.render('results', viewModel);
    });
};

exports.voteSearch = function (req, res) {
    
    searchVotes(req, res, 'vote-search');
};

exports.voteSearchSafe = function (req, res) {

    searchVotes(req, res, 'vote-search-safe');
};

var saveVote = function (req) {
    
    var now = new Date();
    var date = util.format('%d/%d/%d %d:%d:%d', now.getMonth() + 1, 
        now.getDate(), now.getFullYear(), now.getHours(), now.getMinutes(), 
        now.getSeconds());
    var text = util.format('%s\t%s\t%s\n', date, req.session.username, 
        req.body.vote);
    
    fs.appendFile(votesFilePath, text);
};

var isCrsfTokenPresent = function (req) {
    
    return (req.body.csrfToken != null && req.body.csrfToken != null);
};

var isCrsfTokenValid = function (req) {
    
    return (req.body.csrfToken == req.cookies.csrfToken);
};

var searchVotes = function(req, res, viewName) {
        
    var username = req.query.username || '';
    var viewModel = {
        username: username
    };
    
    if (username != '') {

        var stream = fs.createReadStream(votesFilePath);
        stream = byline.createStream(stream);

        var votes = [];
    
        stream.on('data', function (line) {
            var fields = line.toString().split('\t');
        
            if (username.toLowerCase() == fields[1].toLowerCase()) {
                var vote = {
                    date: fields[0],
                    username: fields[1],
                    vote: fields[2]
                };
                votes.push(vote);
            }
        });

        stream.on('end', function () {
            viewModel.votes = votes;
    
            res.render(viewName, viewModel);
        });
    } else {
        res.render(viewName, viewModel);
    }
};
{'fraction_non_alphanumeric': 0.158705461334993, 'fraction_numerical': 0.0031118717908822156, 'mean_word_length': 1.9365006852444038, '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': '7414836', 'n_tokens_mistral': 2025, 'n_tokens_neox': 2008, 'n_words': 396}