<commit_before>var express = require('express');
var router = express.Router();
var sendgrid = require('sendgrid')(process.env.SENDGRID_USERNAME, process.env.SENDGRID_PASSWORD);
var events = require('../controller/event')();
var talks = require('../controller/talk')();
var User = require('../models/user');

var formatDate = function(date) {
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var year = date.getFullYear();
    var hour = date.getHours() % 12;
    hour = hour == 0 ? hour + 12 : hour;
    var minute = date.getMinutes();
    var period = date.getHours() > 11 ? 'PM' : 'AM';
    return month + '/' + day + '/' + year + ' ' + hour + ':' + minute + ' ' + period;
}

var isAuthenticated = function(req, res, next) {
    if (req.isAuthenticated()) {
        return next();
    }
    res.redirect('/');
}

var eventToObject = function(model) {
    var obj = {};
    if (model._id) obj.id = model._id.toString();
    if (model.name) obj.name = model.name;
    if (model.website) obj.website = model.website;
    if (model.startDate) obj.startDate = formatDate(model.startDate);
    if (model.endDate) obj.endDate = formatDate(model.endDate);
    if (model.location) obj.location = model.location;
    if (model.description) obj.description = model.description;
    if (model.owner) obj.owner = userToObject(model.owner);
    return obj;
}

var talkToObject = function(model) {
    var obj = {};
    if (model._id) obj.id = model._id.toString();
    if (model.name) obj.name = model.name;
    if (model.description) obj.description = model.description;
    if (model.event) obj.event = eventToObject(model.event);
    if (model.owner) obj.owner = userToObject(model.owner);
    return obj;
}

var userToObject = function(model) {
    var obj = {};
    if (model._id) obj.id = model._id.toString();
    if (model.email) obj.email = model.email;
    if (model.name) obj.name = model.name;
    return obj;
}

var setEventIsDeleteableAndUpdatable = function(event, userLogged) {
    if (event.owner == userLogged._id.toString()) {
        event.isDeleteable = true;
        event.isUpdatable = true;
    } else {
        event.isDeleteable = false;
        event.isUpdatable = false;
    }
}

var setTalkIsDeleteableAndUpdatable = function(talk, userLogged) {
    if (talk.owner._id.toString() == userLogged._id.toString()) {
        talk.isDeleteable = true;
        talk.isUpdatable = true;
    } else if (talk.event.owner == userLogged._id.toString()) {
        talk.isDeleteable = true;
        talk.isUpdatable = false;
    } else {
        talk.isDeleteable = false;
        talk.isUpdatable = false;
    }
}

module.exports = function(passport) {
    router.get('/', function(req, res) {
        res.render('index', {
            user: req.user,
            message: req.flash('message'),
            csrfToken: req.csrfToken()
        });
    });

    router.post('/login', passport.authenticate('local', {
        successRedirect: '/',
        failureRedirect: '/',
        failureFlash: true
    }));

    router.get('/signup', function(req, res) {
        res.render('signup', { 
            message: req.flash('message'),
            csrfToken: req.csrfToken()
        });
    });

    router.post('/signup', function(req, res) {
        var user = new User();
        user.name = req.body.name;
        user.email = req.body.email;
        user.socialOauthIds[req.body.socialNetwork] = req.body.socialLoginOauthId;
        User.register(user, req.body.password, function(err, user) {
            if (err) {
                return res.render('signup', {
                    message: 'Sorry. That username already exists. Try again.',
                    socialNetwork: req.body.socialNetwork,
                    socialLoginOauthId: req.body.socialLoginOauthId,
                    csrfToken: req.csrfToken()
                });
            }

            //send email verification
            var authenticationURL = 'http://' + process.env.HOST + ':' + process.env.PORT + '/verify?authToken=' + user.authToken;
            sendgrid.send({
                to:       user.email,
                from:     '<EMAIL>',
                subject:  'Confirm your email',
                html:     '<a target=_blank href=\"' + authenticationURL + '\">Confirm your email</a>'
            }, function(err, json) {
                if (err) { return console.error(err); }

                res.redirect('/email-verification');
            });
        });
    });

    router.post('/connect-account', function(req, res, next) {<commit_msg>Change user already exists message.<commit_after>var express = require('express');
var router = express.Router();
var sendgrid = require('sendgrid')(process.env.SENDGRID_USERNAME, process.env.SENDGRID_PASSWORD);
var events = require('../controller/event')();
var talks = require('../controller/talk')();
var User = require('../models/user');

var formatDate = function(date) {
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var year = date.getFullYear();
    var hour = date.getHours() % 12;
    hour = hour == 0 ? hour + 12 : hour;
    var minute = date.getMinutes();
    var period = date.getHours() > 11 ? 'PM' : 'AM';
    return month + '/' + day + '/' + year + ' ' + hour + ':' + minute + ' ' + period;
}

var isAuthenticated = function(req, res, next) {
    if (req.isAuthenticated()) {
        return next();
    }
    res.redirect('/');
}

var eventToObject = function(model) {
    var obj = {};
    if (model._id) obj.id = model._id.toString();
    if (model.name) obj.name = model.name;
    if (model.website) obj.website = model.website;
    if (model.startDate) obj.startDate = formatDate(model.startDate);
    if (model.endDate) obj.endDate = formatDate(model.endDate);
    if (model.location) obj.location = model.location;
    if (model.description) obj.description = model.description;
    if (model.owner) obj.owner = userToObject(model.owner);
    return obj;
}

var talkToObject = function(model) {
    var obj = {};
    if (model._id) obj.id = model._id.toString();
    if (model.name) obj.name = model.name;
    if (model.description) obj.description = model.description;
    if (model.event) obj.event = eventToObject(model.event);
    if (model.owner) obj.owner = userToObject(model.owner);
    return obj;
}

var userToObject = function(model) {
    var obj = {};
    if (model._id) obj.id = model._id.toString();
    if (model.email) obj.email = model.email;
    if (model.name) obj.name = model.name;
    return obj;
}

var setEventIsDeleteableAndUpdatable = function(event, userLogged) {
    if (event.owner == userLogged._id.toString()) {
        event.isDeleteable = true;
        event.isUpdatable = true;
    } else {
        event.isDeleteable = false;
        event.isUpdatable = false;
    }
}

var setTalkIsDeleteableAndUpdatable = function(talk, userLogged) {
    if (talk.owner._id.toString() == userLogged._id.toString()) {
        talk.isDeleteable = true;
        talk.isUpdatable = true;
    } else if (talk.event.owner == userLogged._id.toString()) {
        talk.isDeleteable = true;
        talk.isUpdatable = false;
    } else {
        talk.isDeleteable = false;
        talk.isUpdatable = false;
    }
}

module.exports = function(passport) {
    router.get('/', function(req, res) {
        res.render('index', {
            user: req.user,
            message: req.flash('message'),
            csrfToken: req.csrfToken()
        });
    });

    router.post('/login', passport.authenticate('local', {
        successRedirect: '/',
        failureRedirect: '/',
        failureFlash: true
    }));

    router.get('/signup', function(req, res) {
        res.render('signup', { 
            message: req.flash('message'),
            csrfToken: req.csrfToken()
        });
    });

    router.post('/signup', function(req, res) {
        var user = new User();
        user.name = req.body.name;
        user.email = req.body.email;
        user.socialOauthIds[req.body.socialNetwork] = req.body.socialLoginOauthId;
        User.register(user, req.body.password, function(err, user) {
            if (err) {
                return res.render('signup', {
                    message: 'Sorry. That email already exists. Try again.',
                    socialNetwork: req.body.socialNetwork,
                    socialLoginOauthId: req.body.socialLoginOauthId,
                    csrfToken: req.csrfToken()
                });
            }

            //send email verification
            var authenticationURL = 'http://' + process.env.HOST + ':' + process.env.PORT + '/verify?authToken=' + user.authToken;
            sendgrid.send({
                to:       user.email,
                from:     '<EMAIL>',
                subject:  'Confirm your email',
                html:     '<a target=_blank href=\"' + authenticationURL + '\">Confirm your email</a>'
            }, function(err, json) {
                if (err) { return console.error(err); }

                res.redirect('/email-verification');
            });
        });
    });

    router.post('/connect-account', function(req, res, next) {
{'fraction_non_alphanumeric': 0.14991798797156916, 'fraction_numerical': 0.0017495899398578458, 'mean_word_length': 2.3209876543209877, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '2589935', 'n_tokens_mistral': 2823, 'n_tokens_neox': 2764, 'n_words': 588}