<commit_before>var express = require('express');
var router = express.Router();
var config = require('../config.js');
var db = require('orchestrate')(config.dbkey);
var passport = require('passport');
var util = require('util');
var session = require('express-session');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var GitHubStrategy = require('passport-github2').Strategy;
var partials = require('express-partials');
var logger = require('morgan');
var cookieParser = require('cookie-parser');

//----------Github passport things----------------
var GITHUB_CLIENT_ID = config.github_client_id;
var GITHUB_CLIENT_SECRET = config.github_client_secret;


passport.serializeUser(function(user, done) {
  done(null, user);
});

passport.deserializeUser(function(obj, done) {
  done(null, obj);
});

passport.use('github', new GitHubStrategy({
    clientID: GITHUB_CLIENT_ID,
    clientSecret: GITHUB_CLIENT_SECRET,
    callbackURL: "http://127.0.0.1:3000/auth/github/callback"
  },
  function(accessToken, refreshToken, profile, done) {
    process.nextTick(function () {
      return done(null, profile);
    });
  }
));

router.use(passport.initialize());
router.use(passport.session());
router.use(logger('dev'));
router.use(cookieParser());

//----------------------------------------------

router.get('/', function(req, res, next) {
  if(req.user){
    console.log("cookie", req.cookies.user);
    res.render('user_session', { title: "Hello, " + req.user.displayName ,layout: 'layout', logged_user:req.cookies.url , banana:'yellow' , client_user_session: true});
  }
  else{
    console.log("!req.user");
    res.render('index', { title: 'OPP' , layout: 'layout' , banana:'red' , client_user_session: false });
  }
});

router.get('/test', function(req, res, next) {
  res.render('index', { title: 'OPP' , layout: 'tests_layout'});
});

function ensureAuthenticated(req, res, next) {
  if (req.isAuthenticated()) {
    // console.log("req.path: ", req.path);
    req.session.returnTo = req.path;
    return next();
  }
  else {
    console.log("ensure authentication failed");
  }
}

router.put("/profiles/:id", ensureAuthenticated, function(req, res, next){
    var id = req.params.id;
    
    console.log("profile updated(not really)");
    // console.log("request body", req.body);
    console.log("twitter:", req.body.twitter_url);
    console.log("linkedin_url:", req.body.linkedin_url);
    
    // db.newPatchBuilder("OPP_users", key)
    //   .replace("profile_content.social_urls.twitter", req.body.twitter_url)
    //   .replace("profile_content.social_urls.linkedin", req.body.linkedin_url)
    //   .then(function(result){
    //     console.log("profile updated");
    //     res.send({id: id});
    //   })
    //   .fail(function(err){
    //     console.log("profiles update failed", err);
    //     send(err);
    //   });
});

router.get('/profiles', function(req, res, next) {
  db.list('OPP_users')
      .then(function (result) {
        var data = result.body.results;
        var mapped = data.map(function (element, index) {
          // console.log("profile: " ,element.path.key);
          // console.log("element values: ", element.value.profile_content.editable_text);
          /*
          console.log(element);
          console.log(element.value);
          console.log(element.value.profile_content);
          console.log(element.value.profile_content.editable_text.name);
          console.log(element.value.profile_content.editable_text.title);
          console.log(element.value.github_api_data.github_url);
          console.log(element.value.profile_content.social_urls);
          console.log(element.value.profile_content.social_urls.personal);
          console.log(element.value.profile_content.social_urls.linkedin);
          console.log(element.value.profile_content.social_urls.twitter);
          console.log(element.value.profile_content.editable_text.url_id);
          console.log(element.value.profile_content.checkbox_content.work_status);<commit_msg>added missing apply function to the patchbuilder and added ability to update personal site.<commit_after>var express = require('express');
var router = express.Router();
var config = require('../config.js');
var db = require('orchestrate')(config.dbkey);
var passport = require('passport');
var util = require('util');
var session = require('express-session');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var GitHubStrategy = require('passport-github2').Strategy;
var partials = require('express-partials');
var logger = require('morgan');
var cookieParser = require('cookie-parser');

//----------Github passport things----------------
var GITHUB_CLIENT_ID = config.github_client_id;
var GITHUB_CLIENT_SECRET = config.github_client_secret;


passport.serializeUser(function(user, done) {
  done(null, user);
});

passport.deserializeUser(function(obj, done) {
  done(null, obj);
});

passport.use('github', new GitHubStrategy({
    clientID: GITHUB_CLIENT_ID,
    clientSecret: GITHUB_CLIENT_SECRET,
    callbackURL: "http://127.0.0.1:3000/auth/github/callback"
  },
  function(accessToken, refreshToken, profile, done) {
    process.nextTick(function () {
      return done(null, profile);
    });
  }
));

router.use(passport.initialize());
router.use(passport.session());
router.use(logger('dev'));
router.use(cookieParser());

//----------------------------------------------

router.get('/', function(req, res, next) {
  if(req.user){
    console.log("cookie", req.cookies.user);
    res.render('user_session', { title: "Hello, " + req.user.displayName ,layout: 'layout', logged_user:req.cookies.url , banana:'yellow' , client_user_session: true});
  }
  else{
    console.log("!req.user");
    res.render('index', { title: 'OPP' , layout: 'layout' , banana:'red' , client_user_session: false });
  }
});

router.get('/test', function(req, res, next) {
  res.render('index', { title: 'OPP' , layout: 'tests_layout'});
});

function ensureAuthenticated(req, res, next) {
  if (req.isAuthenticated()) {
    // console.log("req.path: ", req.path);
    req.session.returnTo = req.path;
    return next();
  }
  else {
    console.log("ensure authentication failed");
  }
}

router.put("/profiles/:id", ensureAuthenticated, function(req, res, next){
    var id = req.params.id;
    
    console.log("profile updated(not really)");
    // console.log("request body", req.body);
    console.log("twitter:", req.body.twitter_url);
    console.log("linkedin_url:", req.body.linkedin_url);
    console.log("personal_site_url:", req.body.personal_site_url);
    
    // db.newPatchBuilder("OPP_users", id)
    //   .replace("profile_content.social_urls.twitter", req.body.twitter_url)
    //   .replace("profile_content.social_urls.linkedin", req.body.linkedin_url)
    //   .replace("profile_content.social_urls.personal", req.body.personal_site_url)
    //   .apply()
    //   .fail(function(err){
    //     console.log("profiles update failed", err);
    //     send(err);
    //   });
});

router.get('/profiles', function(req, res, next) {
  db.list('OPP_users')
      .then(function (result) {
        var data = result.body.results;
        var mapped = data.map(function (element, index) {
          // console.log("profile: " ,element.path.key);
          // console.log("element values: ", element.value.profile_content.editable_text);
          /*
          console.log(element);
          console.log(element.value);
          console.log(element.value.profile_content);
          console.log(element.value.profile_content.editable_text.name);
          console.log(element.value.profile_content.editable_text.title);
          console.log(element.value.github_api_data.github_url);
          console.log(element.value.profile_content.social_urls);
          console.log(element.value.profile_content.social_urls.personal);
          console.log(element.value.profile_content.social_urls.linkedin);
          console.log(element.value.profile_content.social_urls.twitter);
          console.log(element.value.profile_content.editable_text.url_id);
          console.log(element.value.profile_content.checkbox_content.work_status);
{'fraction_non_alphanumeric': 0.17672047578589634, 'fraction_numerical': 0.0026702269692923898, 'mean_word_length': 3.537444933920705, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '1252018', 'n_tokens_mistral': 2556, 'n_tokens_neox': 2558, 'n_words': 404}