<commit_before>'use strict'

var db = require('./schema')
  , crypto = require('crypto')
  , jwt = require('jsonwebtoken');

function User(data){
  if(data.displayname){
    this.displayname = data.displayname;
    this.username = data.displayname.toLowerCase();
  }

  if(data.displayname && data.password){
    this.password = crypto.pbkdf2Sync(data.password, data.displayname.toLowerCase(), 4096, 512/4, 'sha256').toString('hex');
  }
  if(data.email){
    this.email = data.email;
  }
  if(data.token){
    this.token = data.token;
  }
  if(data){
    delete data.password;
    this.data = data;
  }
}

User.prototype.testFunc = function(){
  var _this = this;

  return _this.displayname;

}

User.prototype.create = function(cb){
  var _this = this;

  var callback = function(err, item){
    if(err) throw err;
    if(item[0]){
      cb(200, null);
    }else{
      var newUser = new db();
      newUser.displayname = _this.displayname;
      newUser.username = _this.username;
      newUser.email = _this.email;
      newUser.password = _<PASSWORD>;
      newUser.token = jwt.sign(_this.data, _this.password);
      newUser.save(save);
    }
  };

  var save = function(err, item){
    if(err) throw err
    cb(201, item);
  };

  db.
    find({ username: _this.username }).
    limit(1).
    exec(callback);
};

User.prototype.read = function(cb){
  var _this = this;

  var callback = function(err, item){
    if(item[0]){
      cb(200, item[0]);
    }else{
      cb(400, null);
    }
  };

  db.
    find({ token: _this.token }, { password: 0 }).
    limit(1).
    exec(callback);

};

User.prototype.update = function(cb){
  var _this = this;

  var callback = function(err, item){
    if(err) throw err;
    if(item[0]){
      if(_this.email) item[0].email = _this.email;
      if(_this.password) item[0].password = _this.password;
      item[0].save(save);
    }else{
      cb(400, null);
    }
  }

  var save = function(err, item){
    if(err) throw err;
    cb(200, item);
  }

  db.
    find({ token: _this.token }).
    limit(1).
    exec(callback);

};

User.prototype.delete = function(cb){
  var _this = this;

  var callback = function(err){
    if(err) throw err;
    cb(200, null)
  };

  db.
    findOne({ token: _this.token }).
    remove(callback);

};

module.exports = User;

<commit_msg>Export Database Schema for external use<commit_after>'use strict'

var db = require('./schema')
    , crypto = require('crypto')
    , jwt = require('jsonwebtoken');

function User(data){
  if(data.displayname){
    this.displayname = data.displayname;
    this.username = data.displayname.toLowerCase();
  }

  if(data.displayname && data.password){
    this.password = crypto.pbkdf2Sync(data.password, data.displayname.toLowerCase(), 4096, 512/4, 'sha256').toString('hex');
  }
  if(data.email){
    this.email = data.email;
  }
  if(data.token){
    this.token = data.token;
  }
  if(data){
    delete data.password;
    this.data = data;
  }
}

User.prototype.create = function(cb){
  var _this = this;

  var callback = function(err, item){
    if(err) throw err;
    if(item[0]){
      cb(200, null);
    }else{
      var newUser = new db();
      newUser.displayname = _this.displayname;
      newUser.username = _this.username;
      newUser.email = _this.email;
      newUser.password = _<PASSWORD>;
      newUser.token = jwt.sign(_this.data, _this.password);
      newUser.save(save);
    }
  };

  var save = function(err, item){
    if(err) throw err
    cb(201, item);
  };

  db.
      find({ username: _this.username }).
      limit(1).
      exec(callback);
};

User.prototype.read = function(cb){
  var _this = this;

  var callback = function(err, item){
    if(item[0]){
      cb(200, item[0]);
    }else{
      cb(400, null);
    }
  };

  db.
      find({ token: _this.token }, { password: 0 }).
      limit(1).
      exec(callback);

};

User.prototype.update = function(cb){
  var _this = this;

  var callback = function(err, item){
    if(err) throw err;
    if(item[0]){
      if(_this.email) item[0].email = _this.email;
      if(_this.password) item[0].password = <PASSWORD>;
      item[0].save(save);
    }else{
      cb(400, null);
    }
  }

  var save = function(err, item){
    if(err) throw err;
    cb(200, item);
  }

  db.
      find({ token: _this.token }).
      limit(1).
      exec(callback);

};

User.prototype.delete = function(cb){
  var _this = this;

  var callback = function(err){
    if(err) throw err;
    cb(200, null)
  };

  db.
      findOne({ token: _this.token }).
      remove(callback);

};

// Export Database Schema
User.prototype.Schema = db;

// Export User Prototype
module.exports = User;


{'fraction_non_alphanumeric': 0.1613524502460946, 'fraction_numerical': 0.01883158570511449, 'mean_word_length': 2.6774193548387095, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '5148394', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1645, 'n_words': 346}