<commit_before>var config = require('./config/config'),
    express = require('express'),
    passport = require('passport'),
    ensureAuthenticated = require('./config/passport'),
    models = require('./models'),
    fixtures = require('./fixtures'),
    _ = require('underscore');

var stream = require('getstream');
var client = stream.connect(config.stream_key, config.stream_secret, config.stream_app_id);

var router = express.Router(),
    User = models.user,
    Item = models.item,
    Pin = models.pin;


router.get('/', function(req, res){
    Item.find({}).populate('user').lean().exec(function(err, popular){
        if (err)
            return console.log(err);
        if (!req.isAuthenticated())
            return res.render('trending', {location: 'trending',user: req.user, stuff: popular});

        User.findOne({username: req.user.username}).select('_id').lean().exec(function(err, user){
            Pin.find({user: user._id}).select('item -_id').lean().exec(function(err, pinned_items){
                var pinned_items_ids = _.pluck(pinned_items, 'item');

                _.each(popular, function(item){
                    if (pinned_items_ids.indexOf(item._id) >= 0){
                        item.pinned = true;
                    }
                });

                return res.render('trending', {location: 'trending', user: req.user, stuff: popular});
            })
        });
    });
});

router.get('/flat', ensureAuthenticated, function(req, res){
    User.findOne({username: req.user.username}, function(err, foundUser){
        var flatFeed = client.feed('flat', foundUser._id);
        flatFeed.get({}, function(err, response){
            var activities = response.body.results;
        // return res.render('feed', {location: 'feed', user: req.user, people: people, path: req.url, show_feed: false});
        });

    });
});

router.get('/people', ensureAuthenticated, function(req, res){
    User.find({}).nor([{username: req.user.username}, {_id: 0}]).exec(function(err, people){
        return res.render('people', {location: 'people', user: req.user, people: people, path: req.url, show_feed: false});
    })
});

router.get('/account', ensureAuthenticated, function(req, res){
    res.render('account', {user: req.user});
});

router.get('/login', function(req, res){
    if (req.isAuthenticated())
        return res.redirect('/');

    res.render('login', {location: 'people', user: req.user});
});

router.get('/auth/github', passport.authenticate('github'));

router.get('/auth/github/callback', 
    passport.authenticate('github', {failureRedirect: '/login'}),
    function(req, res) {
        User.findOne({username: req.user.username}, function(err, foundUser){
            if (!foundUser){
                User.create({username: req.user.username, avatar_url: req.user._json.avatar_url},
                function(err, newUser){
                    if (err){
                        return console.log(err);
                    }
                    return res.redirect('/');
                });
                
                return res.redirect('/');
            }
        });
    }
);

router.get('/logout', function(req, res){
    req.logout();
    res.redirect('/');
});

router.get('/fixtures', function(req, res){
    fixtures();

    res.send('fixtures ok');
});

router.post('/follow', ensureAuthenticated, function(req, res){
    User.findOne({username: req.user.username}, function(err, foundUser){
        var flatFeed = client.feed('flat', foundUser._id);<commit_msg>Fix signing in through github issue<commit_after>var config = require('./config/config'),
    express = require('express'),
    passport = require('passport'),
    ensureAuthenticated = require('./config/passport'),
    models = require('./models'),
    fixtures = require('./fixtures'),
    _ = require('underscore');

var stream = require('getstream');
var client = stream.connect(config.stream_key, config.stream_secret, config.stream_app_id);

var router = express.Router(),
    User = models.user,
    Item = models.item,
    Pin = models.pin;


router.get('/', function(req, res){
    Item.find({}).populate('user').lean().exec(function(err, popular){
        if (err)
            return console.log(err);
        if (!req.isAuthenticated())
            return res.render('trending', {location: 'trending',user: req.user, stuff: popular});

        User.findOne({username: req.user.username}).select('_id').lean().exec(function(err, user){
            Pin.find({user: user._id}).select('item -_id').lean().exec(function(err, pinned_items){
                var pinned_items_ids = _.pluck(pinned_items, 'item');

                _.each(popular, function(item){
                    if (pinned_items_ids.indexOf(item._id) >= 0){
                        item.pinned = true;
                    }
                });

                return res.render('trending', {location: 'trending', user: req.user, stuff: popular});
            })
        });
    });
});

router.get('/flat', ensureAuthenticated, function(req, res){
    User.findOne({username: req.user.username}, function(err, foundUser){
        var flatFeed = client.feed('flat', foundUser._id);
        flatFeed.get({}, function(err, response){
            var activities = response.body.results;
        // return res.render('feed', {location: 'feed', user: req.user, people: people, path: req.url, show_feed: false});
        });

    });
});

router.get('/people', ensureAuthenticated, function(req, res){
    User.find({}).nor([{username: req.user.username}, {_id: 0}]).exec(function(err, people){
        return res.render('people', {location: 'people', user: req.user, people: people, path: req.url, show_feed: false});
    })
});

router.get('/account', ensureAuthenticated, function(req, res){
    res.render('account', {user: req.user});
});

router.get('/login', function(req, res){
    if (req.isAuthenticated())
        return res.redirect('/');

    res.render('login', {location: 'people', user: req.user});
});

router.get('/auth/github', passport.authenticate('github'));

router.get('/auth/github/callback', 
    passport.authenticate('github', {failureRedirect: '/login'}),
    function(req, res) {
        User.findOne({username: req.user.username}, function(err, foundUser){
            if (!foundUser){
                User.create({username: req.user.username, avatar_url: req.user._json.avatar_url},
                function(err, newUser){
                    if (err){
                        console.log('da');
                        return console.log(err);
                    }
                    return res.redirect('/');
                });
            }
            else
                return res.redirect('/');
        });
    }
);

router.get('/logout', function(req, res){
    req.logout();
    res.redirect('/');
});

router.get('/fixtures', function(req, res){
    fixtures();

    res.send('fixtures ok');
});

router.post('/follow', ensureAuthenticated, function(req, res){
    User.findOne({username: req.user.username}, function(err, foundUser){
        var flatFeed = client.feed('flat', foundUser._id);
{'fraction_non_alphanumeric': 0.1779767734713866, 'fraction_numerical': 0.0005596753882748006, 'mean_word_length': 2.6506639427987744, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5118201', 'n_tokens_mistral': 2109, 'n_tokens_neox': 2046, 'n_words': 398}