<commit_before>// Module to manage users
var sys = require('sys');
var rclient = require('./redisclient');
var pw = require('./passwords');
var _ = require('underscore');
// We store users by a unique numeric ID.
// We also maintain a mapping of email addresses -> ID's

const user_incr = "user_incr";
const user_prefix = "user:";
const user_mail_prefix = "user_email:";

// Get user key from email
exports.key_from_email = function (email,callback) {
    var client = rclient.getClient();
    client.get(email_key(email),callback);
}

// Create email -> ID mapping
var set_email_key = function(email, user_id, callback) {
    var client = rclient.getClient();
    // use setnx to prevent emails from overwriting others.
    client.setnx(email_key(email),user_id,callback);
}

// Form the key used for looking up a userID from an email.
// Performing a GET on the result returns the ID of the user.
var email_key = function(email) {
    return user_mail_prefix+email;
}

var key_from_id = function (id) {
    return (user_prefix+id+":info");
}

// Create a new unique user ID
var make_user_id = function(callback) {
    var client = rclient.getClient();
    client.incr(user_incr,callback);
}

exports.get_by_email = function(email, callback) {
    // Map email to user ID
    exports.key_from_email(email,function(err,result) {
        if (err) {
            callback("User does not exist",null);
        } else {
            exports.get_by_id(result,callback);
        }
    });
}

exports.get_by_id = function(id, callback) {
    var client = rclient.getClient();
    // Lookup user by ID directly
    client.get(key_from_id(id), function(err,result) {
        var json = JSON.parse(result);
        var user = new User(json);
        callback(err,user);
    });
}

// Creating new user
//  users.create({name="foo", email="<EMAIL>", name="<NAME>", password="fb"},callback)
exports.create = function(attrs, callback) {
    if (!_.isDate(attrs.creation_date)) {
        attrs.creation_date = new Date();
    }
    // Attempt to find an existing account ID with the same email.
    exports.key_from_email(attrs.email,function(err,result) {
        if (err) {
            console.log("Error: ",err);
            callback(err,null);
        } else if (!_.isUndefined(result)) {
            make_user_id(function(err,result) {
                if (err) {
                    console.log("Error: ",err);
                } else {
                    attrs.id = result;
                    var user = new User(attrs);
                    user.save(callback);
                }
            });
        } else {
            callback("User with email already exists",null);
        }
    });
}<commit_msg>Formatting.<commit_after>// Module to manage users
var sys = require('sys');
var rclient = require('./redisclient');
var pw = require('./passwords');
var _ = require('underscore');
// We store users by a unique numeric ID.
// We also maintain a mapping of email addresses -> ID's

const user_incr = "user_incr";
const user_prefix = "user:";
const user_mail_prefix = "user_email:";

// Get user key from email
exports.key_from_email = function (email,callback) {
    var client = rclient.getClient();
    client.get(email_key(email),callback);
}

// Create email -> ID mapping
var set_email_key = function(email, user_id, callback) {
    var client = rclient.getClient();
    // use setnx to prevent emails from overwriting others.
    client.setnx(email_key(email),user_id,callback);
}

// Form the key used for looking up a userID from an email.
// Performing a GET on the result returns the ID of the user.
var email_key = function(email) {
    return user_mail_prefix+email;
}

var key_from_id = function (id) {
    return (user_prefix+id+":info");
}

// Create a new unique user ID
var make_user_id = function(callback) {
    var client = rclient.getClient();
    client.incr(user_incr,callback);
}

exports.get_by_email = function(email, callback) {
    // Map email to user ID
    exports.key_from_email(email,function(err,result) {
        if (err) {
            callback("User does not exist",null);
        } else {
            exports.get_by_id(result,callback);
        }
    });
}

exports.get_by_id = function(id, callback) {
    var client = rclient.getClient();
    // Lookup user by ID directly
    client.get(key_from_id(id), function(err,result) {
        var json = JSON.parse(result);
        var user = new User(json);
        callback(err,user);
    });
}

// Creating new user
//  users.create({name="foo", email="<EMAIL>", name="<NAME>", password="fb"},callback)
exports.create = function(attrs, callback) {
   if (!_.isDate(attrs.creation_date)) {
        attrs.creation_date = new Date();
    }
    // Attempt to find an existing account ID with the same email.
    exports.key_from_email(attrs.email,function(err,result) {
        if (err) {
            console.log("Error: ",err);
            callback(err,null);
        } else if (!_.isUndefined(result)) {
            make_user_id(function(err,result) {
                if (err) {
                    console.log("Error: ",err);
                } else {
                    attrs.id = result;
                    var user = new User(attrs);
                    user.save(callback);
                }
            });
        } else {
            callback("User with email already exists",null);
        }
    });
}
{'fraction_non_alphanumeric': 0.13007979216923363, 'fraction_numerical': 0.0, 'mean_word_length': 2.576642335766423, 'pattern_counts': {'":': 2, '<': 7, '<?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': '2286841', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1598, 'n_words': 430}