<commit_before>var Promise = require('bluebird');
var async   = require('async');

exports = module.exports = User;

exports.create = function(user, rollout) {
  return new User(user, rollout);
};

/**
 * User constructor
 */

function User(user, rollout) {
  if (!user || !rollout) {
    throw new Error("The User constructor expected two params.");
  }

  this.user    = user;
  this.rollout = rollout;

  if ('object' === typeof this.rollout) {
    var name = this.rollout.name('rollout:users');
    this.rollout.client.sadd(name, user[this.rollout._id], function(err, result) {});
  }
}

/**
 * Active
 */

User.prototype.active = function(feature) {
  var self = this;
  var user = this.user;

  if (!feature) {
    throw new Error("Expected a feature name.");
  }

  return new Promise(function(resolve, reject) {
    var name = this.rollout.name('rollout:users:' + this[this.rollout._id]);
    this.rollout.client.sismember(name, feature, function(err, result) {
      if (err) {
        return reject(err);
      }

      if (result === 1) {
        resolve(true);
      } else {
        resolve(false);
      }
    });
  }.bind(this)).then(function(active) {

    if (active) {
      return new Promise(function(resolve) { resolve(true); })
    }

    var groups = [];
    var fns    = [];

    for(var key in this.rollout._groups) {
      var group = this.rollout._groups[key];
      if (group._fn(user)) {
        groups.push(group);
      }
    }

    groups.forEach(function(group) {
      fns.push(function(cb) {
        var name = self.rollout.name('rollout:groups:' + group.name);
        self.rollout.client.sismember(name, feature, function(err, result) {
          if (err) {
            return cb(err);
          }

          if (result === 1) {
            cb(null, true);
          } else {
            cb(null, false);
          }

        });
      });
    });

    return new Promise(function(resolve, reject) {
      async.parallel(fns, function(err, results) {
        if (err) {
          return reject(err);
        }

        if (results.indexOf(true) !== -1) {
          resolve(true);
        } else {
          resolve(false);
        }
      });
    });

  }.bind(this));
};

/**
 * Activate
 */

User.prototype.activate = function(feature) {
  var self = this;

  if (!feature) {
    throw new Error("Expected a feature.");
  }

  return new Promise(function(resolve, reject) {
    var name = self.rollout.name('rollout:users:' + self[self.rollout._id]);
    self.rollout.client.sadd(name, feature, function(err, result) {
      if (err) {
        return reject(err);
      }

      resolve();<commit_msg>Corrected to honor rollout._id to override field in User input<commit_after>var Promise = require('bluebird');
var async   = require('async');

exports = module.exports = User;

exports.create = function(user, rollout) {
  return new User(user, rollout);
};

/**
 * User constructor
 */

function User(user, rollout) {
  if (!user || !rollout) {
    throw new Error("The User constructor expected two params.");
  }

  this.user    = user;
  this.rollout = rollout;

  if ('object' === typeof this.rollout) {
    var name = this.rollout.name('rollout:users');
    this.rollout.client.sadd(name, user[this.rollout._id], function(err, result) {});
  }
}

/**
 * Active
 */

User.prototype.active = function(feature) {
  var self = this;
  var user = this.user;

  if (!feature) {
    throw new Error("Expected a feature name.");
  }

  return new Promise(function(resolve, reject) {
    var name = this.rollout.name('rollout:users:' + user[this.rollout._id]);
    this.rollout.client.sismember(name, feature, function(err, result) {
      if (err) {
        return reject(err);
      }

      if (result === 1) {
        resolve(true);
      } else {
        resolve(false);
      }
    });
  }.bind(this)).then(function(active) {

    if (active) {
      return new Promise(function(resolve) { resolve(true); })
    }

    var groups = [];
    var fns    = [];

    for(var key in this.rollout._groups) {
      var group = this.rollout._groups[key];
      if (group._fn(user)) {
        groups.push(group);
      }
    }

    groups.forEach(function(group) {
      fns.push(function(cb) {
        var name = self.rollout.name('rollout:groups:' + group.name);
        self.rollout.client.sismember(name, feature, function(err, result) {
          if (err) {
            return cb(err);
          }

          if (result === 1) {
            cb(null, true);
          } else {
            cb(null, false);
          }

        });
      });
    });

    return new Promise(function(resolve, reject) {
      async.parallel(fns, function(err, results) {
        if (err) {
          return reject(err);
        }

        if (results.indexOf(true) !== -1) {
          resolve(true);
        } else {
          resolve(false);
        }
      });
    });

  }.bind(this));
};

/**
 * Activate
 */

User.prototype.activate = function(feature) {
  var self = this;

  if (!feature) {
    throw new Error("Expected a feature.");
  }

  return new Promise(function(resolve, reject) {
    var name = self.rollout.name('rollout:users:' + self.user[self.rollout._id]);
    self.rollout.client.sadd(name, feature, function(err, result) {
      if (err) {
        return reject(err);
      }

      resolve();
{'fraction_non_alphanumeric': 0.15673040869891264, 'fraction_numerical': 0.0011248593925759281, 'mean_word_length': 2.289149198520345, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3979537', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1677, 'n_words': 362}