<commit_before>'use strict';

var async       = require('async');
var debug       = require('debug')('users');
var updateCouch = require('../../lib/update-couch-doc');

module.exports = function (cfg) {
  var authenticate = require('../../lib/authentication')(cfg);

  var model     = {};
  var db        = cfg.usersdb;
  var storiesdb = cfg.storiesdb;

  model.add = function (user, cb) {

    debug('creating %s', user.username);

    var doc = {
      _id:      'org.couchdb.user:' + user.username,
      name:     user.username,
      type:     'user',
      roles:    [],
      scope:    [],
      password: <PASSWORD>
    };

    db.insert(doc, function couchInsert (err, body) {

      if (err) return cb(err);

      return cb(null, body);

    });

  };

  model.update = function (username, delta, cb) {

    debug('updating: %s', username);

    updateCouch('org.couchdb.user:' + username, db, delta, function (err) {

      if (err) return cb(err);

      cb();

    });
  };

  model.destroy = function (username, cb) {

    debug('removing: %s', username);

    var jobs = [

      function getLatestRevision (done) {

      /* eslint-disable camelcase */

        db.get('org.couchdb.user:' + username, { revs_info: true  }, function (err, body) {

      /* eslint-enable camelcase */

          if (err) return done(err);

          return done(null, body._rev);

        });

      },

      function deleteDocument (revision, done) {

        db.destroy('org.couchdb.user:' + username, revision, function (err, body) {

          if (err) return done(err);

          return done(null, body);

        });
      }

    ];

    async.waterfall(jobs, function asyncFinished (err, result) {

      debug(err);

      if (err) return cb(err);

      return cb(null, result);
    });

  };

  model.get = function (username, cb) {

    debug('getting: %s', username);

    db.get('org.couchdb.user:' + username, function (err, body) {

      if (err) return cb(err);

      cb(null, body);

    });

  };

  model.getStories = function (username, userToList, cb) {

    debug('getting stories for %s', userToList);

    storiesdb.view('story', 'byUser', { key: userToList }, function (err, body) {
      if (err) return cb(err);

      var list = body.rows.map(function (value) {
        return {
          id:    value.id,
          title: value.value
        };
      });

      cb(null, list);
    });

  };

  model.list = function (cb) {

    db.list(function getDBList (err, body) {

      if(err) return cb(err);

      var list = body.rows.filter(function (val) {

        if(val.id.match(/^org\.couchdb\.user:+/)) {
          return true;
        } else {
          return false;
        }<commit_msg>Fixed story title not being sent properly<commit_after>'use strict';

var async       = require('async');
var debug       = require('debug')('users');
var updateCouch = require('../../lib/update-couch-doc');

module.exports = function (cfg) {
  var authenticate = require('../../lib/authentication')(cfg);

  var model     = {};
  var db        = cfg.usersdb;
  var storiesdb = cfg.storiesdb;

  model.add = function (user, cb) {

    debug('creating %s', user.username);

    var doc = {
      _id:      'org.couchdb.user:' + user.username,
      name:     user.username,
      type:     'user',
      roles:    [],
      scope:    [],
      password: <PASSWORD>
    };

    db.insert(doc, function couchInsert (err, body) {

      if (err) return cb(err);

      return cb(null, body);

    });

  };

  model.update = function (username, delta, cb) {

    debug('updating: %s', username);

    updateCouch('org.couchdb.user:' + username, db, delta, function (err) {

      if (err) return cb(err);

      cb();

    });
  };

  model.destroy = function (username, cb) {

    debug('removing: %s', username);

    var jobs = [

      function getLatestRevision (done) {

      /* eslint-disable camelcase */

        db.get('org.couchdb.user:' + username, { revs_info: true  }, function (err, body) {

      /* eslint-enable camelcase */

          if (err) return done(err);

          return done(null, body._rev);

        });

      },

      function deleteDocument (revision, done) {

        db.destroy('org.couchdb.user:' + username, revision, function (err, body) {

          if (err) return done(err);

          return done(null, body);

        });
      }

    ];

    async.waterfall(jobs, function asyncFinished (err, result) {

      debug(err);

      if (err) return cb(err);

      return cb(null, result);
    });

  };

  model.get = function (username, cb) {

    debug('getting: %s', username);

    db.get('org.couchdb.user:' + username, function (err, body) {

      if (err) return cb(err);

      cb(null, body);

    });

  };

  model.getStories = function (username, userToList, cb) {

    debug('getting stories for %s', userToList);

    storiesdb.view('story', 'byUser', { key: userToList }, function (err, body) {
      if (err) return cb(err);

      var list = body.rows.map(function (value) {
        return {
          id:    value.id,
          title: value.value.title
        };
      });

      cb(null, list);
    });

  };

  model.list = function (cb) {

    db.list(function getDBList (err, body) {

      if(err) return cb(err);

      var list = body.rows.filter(function (val) {

        if(val.id.match(/^org\.couchdb\.user:+/)) {
          return true;
        } else {
          return false;
        }
{'fraction_non_alphanumeric': 0.1604014598540146, 'fraction_numerical': 0.0, 'mean_word_length': 2.1142045454545455, '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': '3794634', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1721, 'n_words': 418}