<commit_before>'use strict';

var crypto = require('crypto');
var db = require('../connection').database('_users');


function sha (s) {
    return crypto.createHash("sha1").update(s).digest("hex");
};


var adduser = module.exports = function(options, callback) {
    var doc_name = 'org.couchdb.user:' + encodeURIComponent(options.username);

    db.save(doc_name, adduser.create_document(options), callback);
};


// @param {Object} options
// - username
// - email
// - password
adduser.create_document = function(options) {
    var salt = crypto.randomBytes(30).toString('hex');
    var username = options.username;
    var email    = options.email;
    var password = <PASSWORD>;

    return {
        name            : username,
        salt            : salt,
        password_sha    : sha( password + salt ),
        email           : email,
        _id             : 'org.couchdb.user:' + username,
        type            : 'user',
        roles           : [],
        date            : new Date().toISOString()
    };
};

// adduser.get_user = function(doc_name, callback) {
//     db.get(doc_name, callback);
// };<commit_msg>migrate to my new couchdb module<commit_after>'use strict';

var crypto = require('crypto');
var db = require('../db');
var lang = require('../util/lang');


function sha (s) {
    return crypto.createHash("sha1").update(s).digest("hex");
};


var adduser = module.exports = function(options, callback, logger) {
    var doc_name = '/-/user/org.couchdb.user:' + encodeURIComponent(options.username);
    var user = adduser.create_document(options);

    logger.debug('{{cyan document name}}', doc_name);

    db.put(doc_name, {
        json: user
    }, function(err, res, json) {
        logger.debug('{{cyan PUT}}', err, json);

        if(
            err ||

            // if document update conflict, we will update a new rev
            json.error !== 'conflict'
        ){
            return callback(err, res, json);
        }

        logger.debug('{{cyan GET}}', doc_name);
        db.get(doc_name, {}, function(err, res, json) {
            if(err){
                return callback(err, res, json);
            }

            lang.mix(user, json, false);

            logger.debug('{{cyan PUT}}', doc_name + '/-rev/' + user._rev);
            db.put(doc_name + '/-rev/' + user._rev, {
                json: user
            }, callback);
        });
    });
};


// @param {Object} options
// - username
// - email
// - password
adduser.create_document = function(options) {
    var salt = crypto.randomBytes(30).toString('hex');
    var username = options.username;
    var email    = options.email;
    var password = <PASSWORD>;

    return {
        name            : username,
        salt            : salt,
        password_sha    : sha( password + salt ),
        email           : email,
        _id             : 'org.couchdb.user:' + username,
        type            : 'user',
        roles           : [],
        date            : new Date().toISOString()
    };
};


// santitize callback parameters
adduser.message_handler = function(err, res, callback) {
    
};


// adduser.get_user = function(doc_name, callback) {
//     db.get(doc_name, callback);
// };
{'fraction_non_alphanumeric': 0.15215370312984197, 'fraction_numerical': 0.0018593120545398203, 'mean_word_length': 2.0111940298507465, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5181301', 'n_tokens_mistral': 963, 'n_tokens_neox': 945, 'n_words': 214}