<commit_before>var bodyParser = require('body-parser');
var request = require('request');
var jwt = require('jwt-simple');

var User = require('./db-config.js');

exports.signupUser = function(req, res) {
  var username = req.body.username;
  var password = req.body.password;

  User.findOne({ username: username })
    .exec(function(err, user) {
      if (!user) {
        var newUser = new User({
          username: username,
          password: password
        });

        newUser.save(function(err, newUser) {
          if (err) {
            res.status(418).end();
          } else {
            var token = jwt.encode(user, '<PASSWORD>');
            res.json({token: token});
            console.log('Success: Account added to database.');
            res.status(201).end();
          }
        });
      } else {
        console.log('Error: Account already exists');
        res.status(418).end();
      }
    });
};

exports.loginUser = function(req, res) {
  var username = req.body.username;
  var password = req.<PASSWORD>;

  User.findOne({ username: username })
    .exec(function(err, user) {

      if (!user) {
        res.status(418).end();
      } else {
        var savedPassword = user.password;
        user.comparePassword(password, savedPassword, function(err, match) {
          if (match) {
            var token = jwt.encode(user, '<PASSWORD>');
            res.json({token: token});
            console.log('Success: Logged in');
            res.status(201).end();

          } else {
            console.log('Error: Incorrect password');
            res.status(418).end();
          }
        });
      }
  });
};


exports.toTabs = function(req, res){
  //Here we distribute the data we received from the request
  var reciever = req.body.user;
  //since we got a token we need to decode it first
  var decoded = jwt.decode(req.body.token, '<PASSWORD>');
  var sender = decoded.username;
  //we need a temporal variable to use the update method on the db.
  var temp;
  //This query finds the sender in the db
  User.findOne({ username: sender })
    .exec(function(err, user) {
      if(!user) {
        console.log('attempted to route to tabs, but person not found!');
        res.status(500).end();
      } else {

          //if the receiver is on the network of the sender, the number is incremented 
          if(user.network.hasOwnProperty(reciever)){
            user.network[reciever]++;

          } else {
            //otherwise, we create the relationship
            user.network[reciever] = 1;
          }
          //here we assign the entire user object to teh temp variable
          temp = user;
          //We use the update method, here we replace the old
          //network object, with the one insede temp
          User.update({_id: user._id}, {$set: {network: temp.network}}, function(err){
            if (err) return err;
          });

          //this does the exact same thing, but from the receiver's perspective  
          User.findOne({ username: reciever })
            .exec(function(err, user) {
              if(!user) {
                console.log('attempted to route to tabs, but person not found!');
                res.status(500).end();
              } else {
                  //instead of incrementing, the number decreases
                  if(user.network.hasOwnProperty(sender)){
                    user.network[sender]--;
                  } else {
                    //the default in this case is negative
                    user.network[sender] = -1;
                  }
                  //here we assign the entire user object to teh temp variable
                  temp = user;
                  //We use the update method, here we replace the old
                  //network object, with the one insede temp
                  User.update({_id: user._id}, {$set: {network: temp.network}}, function(err){
                    if (err) return err;
                  });

                  res.status(201).send(user).end();
                }
            });  
        }
    });


};<commit_msg>Fix toTabs handler, send the current user object in the response<commit_after>var bodyParser = require('body-parser');
var request = require('request');
var jwt = require('jwt-simple');

var User = require('./db-config.js');

exports.signupUser = function(req, res) {
  var username = req.body.username;
  var password = req.body.password;

  User.findOne({ username: username })
    .exec(function(err, user) {
      if (!user) {
        var newUser = new User({
          username: username,
          password: password
        });

        newUser.save(function(err, newUser) {
          if (err) {
            res.status(418).end();
          } else {
            var token = jwt.encode(user, 'arg<PASSWORD>Rosson');
            res.json({token: token});
            console.log('Success: Account added to database.');
            res.status(201).end();
          }
        });
      } else {
        console.log('Error: Account already exists');
        res.status(418).end();
      }
    });
};

exports.loginUser = function(req, res) {
  var username = req.body.username;
  var password = req.body.password;

  User.findOne({ username: username })
    .exec(function(err, user) {

      if (!user) {
        res.status(418).end();
      } else {
        var savedPassword = user.password;
        user.comparePassword(password, savedPassword, function(err, match) {
          if (match) {
            var token = jwt.encode(user, '<PASSWORD>');
            res.json({token: token});
            console.log('Success: Logged in');
            res.status(201).end();

          } else {
            console.log('Error: Incorrect password');
            res.status(418).end();
          }
        });
      }
  });
};


exports.toTabs = function(req, res){
  //Here we distribute the data we received from the request
  var reciever = req.body.user;
  //since we got a token we need to decode it first
  var decoded = jwt.decode(req.body.token, 'argleDavidBargleRosson');
  var sender = decoded.username;
  //we need a temporal variable to use the update method on the db.
  var temp;
  //This query finds the sender in the db
  User.findOne({ username: sender })
    .exec(function(err, user) {
      if(!user) {
        console.log('attempted to route to tabs, but person not found!');
        res.status(500).end();
      } else {

          //if the receiver is on the network of the sender, the number is incremented 
          if(user.network.hasOwnProperty(reciever)){
            user.network[reciever]++;

          } else {
            //otherwise, we create the relationship
            user.network[reciever] = 1;
          }
          //here we assign the entire user object to teh temp variable
          temp = user;
          //We use the update method, here we replace the old
          //network object, with the one insede temp
          User.update({_id: user._id}, {$set: {network: temp.network}}, function(err){
            if (err) return err;
          });

          res.send(user);
          //this does the exact same thing, but from the receiver's perspective  
          User.findOne({ username: reciever })
            .exec(function(err, user) {
              if(!user) {
                console.log('attempted to route to tabs, but person not found!');
                res.status(500).end();
              } else {
                  //instead of incrementing, the number decreases
                  if(user.network.hasOwnProperty(sender)){
                    user.network[sender]--;
                  } else {
                    //the default in this case is negative
                    user.network[sender] = -1;
                  }
                  //here we assign the entire user object to teh temp variable
                  temp = user;
                  //We use the update method, here we replace the old
                  //network object, with the one insede temp
                  User.update({_id: user._id}, {$set: {network: temp.network}}, function(err){
                    if (err) return err;
                  })
                  res.status(201).end();
                }
            });  
        }
    });


};
{'fraction_non_alphanumeric': 0.1228495275018173, 'fraction_numerical': 0.007026896050399806, 'mean_word_length': 1.738885202388852, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4801677', 'n_tokens_mistral': 2305, 'n_tokens_neox': 2193, 'n_words': 665}