<commit_before>var Promise = require('bluebird'),
    bases = require('bases'),
    RateLimit = require('express-rate-limit'),
    logger = require('../utils/logger'),
    nconf = require('nconf'),
    emapicOpinionFreq = parseFloat(nconf.get('app').emapicOpinionFreq),
    sequelize = models.sequelize;

module.exports = function(app) {

    function handleInternalError(req, res) {
        return function(err) {
            logger.error('Internal server error during API request: ' + err.message);
            res.status(500).json({ error_code: 'internal_error', error: 'an internal server error has occured.' });
        };
    }

    app.get('/survey/:id', function(req, res) {
        models.Survey.scope('includeAuthor').findById(Utils.decryptSurveyId(req.params.id)).then(function(survey) {
            if (survey === null) {
                return res.redirect('/');
            }
            // Survey is in draft mode and it's not being tested by its owner
            if ((survey.active === null) && (!req.user || (survey.owner.id !== req.user.id))) {
                return res.redirect('/');
            }
            // Survey is closed
            if (survey.active === false) {
                return res.redirect('/survey/' + req.params.id + '/results');
            }
            return Promise.join(survey.getHtml(), survey.getSubTitle(), survey.getUserFullResponses(req.user ? req.user.id : null), function(html, subTitle, responses) {
                // If the survey allows multiple answers, then we don't
                // set an already voted position
                var position = survey.multiple_answer ? null : responses.geometry.coordinates;
                res.render('templates/survey', {
                    title : survey.title,
                    survey : survey,
                    subTitle : subTitle,
                    questions_html : html,
                    responses : JSON.stringify(responses.properties),
                    position : JSON.stringify(position),
                    emapicOpinion : (survey.active === null) ? false : (Math.random() < emapicOpinionFreq),
                    layout : 'layouts/map'
                });
            });
        });
    });

    app.get('/survey/:id/results', function(req, res) {
        models.Survey.scope('includeAuthor').findById(Utils.decryptSurveyId(req.params.id)).then(function(survey) {
            if (survey === null) {
                return res.redirect('/');
            }
            // Survey is closed
            if ((survey.active === false) ||
            // Data required by the owner
            (req.user && survey.owner.id === req.user.id) ||
            // Survey is active and results are always public
            (survey.active === true && survey.public_results) ||
            // It's a local request from the server itself
            (req.ip === '127.0.0.1' && (req.host === '127.0.0.1' || req.host === 'localhost'))) {
                return Promise.join(survey.getHtml(), survey.getSubTitle(), survey.getUserFullResponses(req.user ? req.user.id : null), function(html, subTitle, responses) {
                    // If the survey allows multiple answers, then we don't
                    // set an already voted position
                    var position = survey.multiple_answer ? null : responses.geometry.coordinates;
                    res.render('templates/survey', {
                        title : survey.title,
                        survey : survey,
                        results : true,
                        subTitle : subTitle,
                        questions_html : html,
                        responses : JSON.stringify(responses.properties),
                        position : JSON.stringify(position),
                        emapicOpinion : false,
                        layout : 'layouts/map'
                    });
                });
            }
            return res.redirect('/survey/' + req.params.id);
        });
    });

    app.get('/api/survey/:id/results', function(req, res) {
        models.Survey.scope('includeAuthor').findById(Utils.decryptSurveyId(req.params.id)).then(function(survey) {
            if (survey === null) {
                return res.status(404).json({ error_code: 'invalid_resource', error: 'requested survey doesn\'t exist.' });
            }
            // Survey is closed
            if ((survey.active === false) ||
            // Data required by the owner
            (req.user && survey.owner.id === req.user.id) ||
            // Survey is active and results are always public or shown after vote
            (survey.active === true && (survey.public_results || survey.results_after_vote)) ||
            // It's a local request from the server itself
            (req.ip === '127.0.0.1' && (req.host === '127.0.0.1' || req.host === 'localhost'))) {
                return Promise.join(survey.getAnonymizedResponses(), survey.getQuestions({
                    scope: 'includeAnswers'
                }), function(responses, questions) {
                    var answers = [];
                    for (var i = 0, iLen = questions.length; i<iLen; i++) {
                        answers.push(questions[i].Answers);
                    }
                    res.json(postGISQueryToFeatureCollection(addIndivVotePopupMessage(responses, questions, answers)));
                });
            } else {
                return res.status(403).json({ error_code: 'forbidden_access', error: 'you don\'t have the required permissions.' });
            }
        }).catch(handleInternalError(req, res));
    });

    app.get('/api/surveys', function(req, res) {
        var login = req.query.login && req.query.login.trim() !== '' ? req.query.login : null,
            userId = isNaN(req.query.userId) ? null : req.query.userId,
            query = req.query.q && req.query.q.trim() !== '' ? req.query.q : null,
            tag = req.query.tag && req.query.tag.trim() !== '' ? req.query.tag : null,
            status = req.query.status && req.query.status.trim() !== '' ? req.query.status : null,
            pageNr = isNaN(req.query.page) ? null : req.query.page,
            pageSize = isNaN(req.query.size) ? null : req.query.size,<commit_msg>API route for retrieving the logged-in user data<commit_after>var Promise = require('bluebird'),
    bases = require('bases'),
    RateLimit = require('express-rate-limit'),
    logger = require('../utils/logger'),
    nconf = require('nconf'),
    emapicOpinionFreq = parseFloat(nconf.get('app').emapicOpinionFreq),
    sequelize = models.sequelize;

module.exports = function(app) {

    function handleInternalError(req, res) {
        return function(err) {
            logger.error('Internal server error during API request: ' + err.message);
            res.status(500).json({ error_code: 'internal_error', error: 'an internal server error has occured.' });
        };
    }

    app.get('/survey/:id', function(req, res) {
        models.Survey.scope('includeAuthor').findById(Utils.decryptSurveyId(req.params.id)).then(function(survey) {
            if (survey === null) {
                return res.redirect('/');
            }
            // Survey is in draft mode and it's not being tested by its owner
            if ((survey.active === null) && (!req.user || (survey.owner.id !== req.user.id))) {
                return res.redirect('/');
            }
            // Survey is closed
            if (survey.active === false) {
                return res.redirect('/survey/' + req.params.id + '/results');
            }
            return Promise.join(survey.getHtml(), survey.getSubTitle(), survey.getUserFullResponses(req.user ? req.user.id : null), function(html, subTitle, responses) {
                // If the survey allows multiple answers, then we don't
                // set an already voted position
                var position = survey.multiple_answer ? null : responses.geometry.coordinates;
                res.render('templates/survey', {
                    title : survey.title,
                    survey : survey,
                    subTitle : subTitle,
                    questions_html : html,
                    responses : JSON.stringify(responses.properties),
                    position : JSON.stringify(position),
                    emapicOpinion : (survey.active === null) ? false : (Math.random() < emapicOpinionFreq),
                    layout : 'layouts/map'
                });
            });
        });
    });

    app.get('/survey/:id/results', function(req, res) {
        models.Survey.scope('includeAuthor').findById(Utils.decryptSurveyId(req.params.id)).then(function(survey) {
            if (survey === null) {
                return res.redirect('/');
            }
            // Survey is closed
            if ((survey.active === false) ||
            // Data required by the owner
            (req.user && survey.owner.id === req.user.id) ||
            // Survey is active and results are always public
            (survey.active === true && survey.public_results) ||
            // It's a local request from the server itself
            (req.ip === '127.0.0.1' && (req.host === '127.0.0.1' || req.host === 'localhost'))) {
                return Promise.join(survey.getHtml(), survey.getSubTitle(), survey.getUserFullResponses(req.user ? req.user.id : null), function(html, subTitle, responses) {
                    // If the survey allows multiple answers, then we don't
                    // set an already voted position
                    var position = survey.multiple_answer ? null : responses.geometry.coordinates;
                    res.render('templates/survey', {
                        title : survey.title,
                        survey : survey,
                        results : true,
                        subTitle : subTitle,
                        questions_html : html,
                        responses : JSON.stringify(responses.properties),
                        position : JSON.stringify(position),
                        emapicOpinion : false,
                        layout : 'layouts/map'
                    });
                });
            }
            return res.redirect('/survey/' + req.params.id);
        });
    });

    app.get('/api/survey/:id/results', function(req, res) {
        models.Survey.scope('includeAuthor').findById(Utils.decryptSurveyId(req.params.id)).then(function(survey) {
            if (survey === null) {
                return res.status(404).json({ error_code: 'invalid_resource', error: 'requested survey doesn\'t exist.' });
            }
            // Survey is closed
            if ((survey.active === false) ||
            // Data required by the owner
            (req.user && survey.owner.id === req.user.id) ||
            // Survey is active and results are always public or shown after vote
            (survey.active === true && (survey.public_results || survey.results_after_vote)) ||
            // It's a local request from the server itself
            (req.ip === '127.0.0.1' && (req.host === '127.0.0.1' || req.host === 'localhost'))) {
                return Promise.join(survey.getAnonymizedResponses(), survey.getQuestions({
                    scope: 'includeAnswers'
                }), function(responses, questions) {
                    var answers = [];
                    for (var i = 0, iLen = questions.length; i<iLen; i++) {
                        answers.push(questions[i].Answers);
                    }
                    res.json(postGISQueryToFeatureCollection(addIndivVotePopupMessage(responses, questions, answers)));
                });
            } else {
                return res.status(403).json({ error_code: 'forbidden_access', error: 'you don\'t have the required permissions.' });
            }
        }).catch(handleInternalError(req, res));
    });

    app.get('/api/user', requireRole(null, true), function(req, res) {
        res.json(req.user.getDescription());
    });

    app.get('/api/surveys', function(req, res) {
        var login = req.query.login && req.query.login.trim() !== '' ? req.query.login : null,
            userId = isNaN(req.query.userId) ? null : req.query.userId,
            query = req.query.q && req.query.q.trim() !== '' ? req.query.q : null,
            tag = req.query.tag && req.query.tag.trim() !== '' ? req.query.tag : null,
            status = req.query.status && req.query.status.trim() !== '' ? req.query.status : null,
            pageNr = isNaN(req.query.page) ? null : req.query.page,
            pageSize = isNaN(req.query.size) ? null : req.query.size,
{'fraction_non_alphanumeric': 0.13936372269705602, 'fraction_numerical': 0.0053814498258942705, 'mean_word_length': 1.7676303109943057, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, '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': '5257649', 'n_tokens_mistral': 3456, 'n_tokens_neox': 3323, 'n_words': 726}