<commit_before>
angular.module('cesium.wot.services', ['ngResource', 'ngApi', 'cesium.bma.services', 'cesium.crypto.services', 'cesium.utils.services',
  'cesium.settings.services'])

.factory('csWot', function($q, $timeout, BMA, Api, CacheFactory, csConfig, csSettings, csCache) {
  'ngInject';

  factory = function(id) {

    var
      api = new Api(this, "csWot-" + id),
      identityCache = csCache.get('csWot-idty-', csCache.constants.SHORT),

      // Add id, and remove duplicated id
      _addUniqueIds = function(idties) {
        var idtyKeys = {};
        return idties.reduce(function(res, idty) {
          idty.id = idty.uid + '-' + idty.pubkey;
          if (!idtyKeys[idty.id]) {
            idtyKeys[idty.id] = true;
            return res.concat(idty);
          }
          return res;
        }, []);
      },

      _sortAndSliceIdentities = function(idties, offset, size) {
        offset = offset || 0;

        // Add unique ids
        idties = _addUniqueIds(idties);

        // Sort by block and
        idties = _.sortBy(idties, function(idty){
          var score = 1;
          score += (1000000 * (idty.block));
          score += (10      * (900 - idty.uid.toLowerCase().charCodeAt(0)));
          return -score;
        });
        if (angular.isDefined(size) && idties.length > size) {
          idties = idties.slice(offset, offset+size); // limit if more than expected size
        }


        return idties;
      },

      _sortCertifications = function(certifications) {
        certifications = _.sortBy(certifications, function(cert){
          var score = 1;
          score += (1000000000000 * (cert.expiresIn ? cert.expiresIn : 0));
          score += (10000000      * (cert.isMember ? 1 : 0));
          score += (10            * (cert.block ? cert.block : 0));
          return -score;
        });
        return certifications;
      },

      loadRequirements = function(pubkey, uid) {
        return $q(function(resolve, reject) {
          // Get requirements
          BMA.wot.requirements({pubkey: pubkey})
          .then(function(res){
            if (!res.identities || res.identities.length === 0) {
              resolve();
              return;
            }
            // Sort to select the best identity
            if (res.identities.length > 1) {
              // Select the best identity, by sorting using this order
              //  - same wallet uid
              //  - is member
              //  - has a pending membership
              //  - is not expired
              //  - is not outdistanced
              //  - if has certifications
              //      max(count(certification)
              //    else
              //      max(membershipPendingExpiresIn) = must recent membership
              res.identities = _.sortBy(res.identities, function(idty) {
                var score = 0;
                score += (10000000000 * ((uid && idty.uid === uid) ? 1 : 0));
                score += (1000000000  * (idty.membershipExpiresIn > 0 ? 1 : 0));
                score += (100000000   * (idty.membershipPendingExpiresIn > 0 ? 1 : 0));
                score += (10000000    * (!idty.expired ? 1 : 0));
                score += (1000000     * (!idty.outdistanced ? 1 : 0));
                var certCount = !idty.expired && idty.certifications ? idty.certifications.length : 0;
                score += (1         * (certCount ? certCount : 0));
                score += (1         * (!certCount && idty.membershipPendingExpiresIn > 0 ? idty.membershipPendingExpiresIn/1000 : 0));
                return -score;
              });
              console.debug('Found {0} identities. Will selected the best one'.format(res.identities.length));
            }
            var requirements = res.identities[0];
            // Add useful custom fields
            requirements.hasSelf = true;
            requirements.needMembership = (requirements.membershipExpiresIn <= 0 &&
                                           requirements.membershipPendingExpiresIn <= 0 );
            requirements.needRenew = (!requirements.needMembership &&
                                      requirements.membershipExpiresIn <= csSettings.data.timeWarningExpire &&
                                      requirements.membershipPendingExpiresIn <= 0 );
            requirements.canMembershipOut = (requirements.membershipExpiresIn > 0);
            requirements.pendingMembership = (requirements.membershipExpiresIn <= 0 && requirements.membershipPendingExpiresIn > 0);
            requirements.certificationCount = (requirements.certifications) ? requirements.certifications.length : 0;
            requirements.willExpireCertificationCount = requirements.certifications ? requirements.certifications.reduce(function(count, cert){
              if (cert.expiresIn <= csSettings.data.timeWarningExpire) {
                cert.willExpire = true;
                return count + 1;
              }
              return count;
            }, 0) : 0;
            requirements.isMember = (requirements.membershipExpiresIn > 0);

            resolve(requirements);
          })
          .catch(function(err) {
            // If not a member: continue
            if (!!err &&
                (err.ucode == BMA.errorCodes.NO_MATCHING_MEMBER ||
                 err.ucode == BMA.errorCodes.NO_IDTY_MATCHING_PUB_OR_UID)) {
              resolve({
                hasSelf: false,
                needMembership: true,
                canMembershipOut: false,
                needRenew: false,
                pendingMembership: false,
                needCertifications: false,
                needCertificationCount: 0,
                willNeedCertificationCount: 0
              });
            }
            else {
              reject(err);
            }<commit_msg>- Wot identity: Force certification count to 0, is not a member yet - fix #269<commit_after>
angular.module('cesium.wot.services', ['ngResource', 'ngApi', 'cesium.bma.services', 'cesium.crypto.services', 'cesium.utils.services',
  'cesium.settings.services'])

.factory('csWot', function($q, $timeout, BMA, Api, CacheFactory, csConfig, csSettings, csCache) {
  'ngInject';

  factory = function(id) {

    var
      api = new Api(this, "csWot-" + id),
      identityCache = csCache.get('csWot-idty-', csCache.constants.SHORT),

      // Add id, and remove duplicated id
      _addUniqueIds = function(idties) {
        var idtyKeys = {};
        return idties.reduce(function(res, idty) {
          idty.id = idty.uid + '-' + idty.pubkey;
          if (!idtyKeys[idty.id]) {
            idtyKeys[idty.id] = true;
            return res.concat(idty);
          }
          return res;
        }, []);
      },

      _sortAndSliceIdentities = function(idties, offset, size) {
        offset = offset || 0;

        // Add unique ids
        idties = _addUniqueIds(idties);

        // Sort by block and
        idties = _.sortBy(idties, function(idty){
          var score = 1;
          score += (1000000 * (idty.block));
          score += (10      * (900 - idty.uid.toLowerCase().charCodeAt(0)));
          return -score;
        });
        if (angular.isDefined(size) && idties.length > size) {
          idties = idties.slice(offset, offset+size); // limit if more than expected size
        }


        return idties;
      },

      _sortCertifications = function(certifications) {
        certifications = _.sortBy(certifications, function(cert){
          var score = 1;
          score += (1000000000000 * (cert.expiresIn ? cert.expiresIn : 0));
          score += (10000000      * (cert.isMember ? 1 : 0));
          score += (10            * (cert.block ? cert.block : 0));
          return -score;
        });
        return certifications;
      },

      loadRequirements = function(pubkey, uid) {
        return $q(function(resolve, reject) {
          // Get requirements
          BMA.wot.requirements({pubkey: pubkey})
          .then(function(res){
            if (!res.identities || res.identities.length === 0) {
              resolve();
              return;
            }
            // Sort to select the best identity
            if (res.identities.length > 1) {
              // Select the best identity, by sorting using this order
              //  - same wallet uid
              //  - is member
              //  - has a pending membership
              //  - is not expired
              //  - is not outdistanced
              //  - if has certifications
              //      max(count(certification)
              //    else
              //      max(membershipPendingExpiresIn) = must recent membership
              res.identities = _.sortBy(res.identities, function(idty) {
                var score = 0;
                score += (10000000000 * ((uid && idty.uid === uid) ? 1 : 0));
                score += (1000000000  * (idty.membershipExpiresIn > 0 ? 1 : 0));
                score += (100000000   * (idty.membershipPendingExpiresIn > 0 ? 1 : 0));
                score += (10000000    * (!idty.expired ? 1 : 0));
                score += (1000000     * (!idty.outdistanced ? 1 : 0));
                var certCount = !idty.expired && idty.certifications ? idty.certifications.length : 0;
                score += (1         * (certCount ? certCount : 0));
                score += (1         * (!certCount && idty.membershipPendingExpiresIn > 0 ? idty.membershipPendingExpiresIn/1000 : 0));
                return -score;
              });
              console.debug('Found {0} identities. Will selected the best one'.format(res.identities.length));
            }
            var requirements = res.identities[0];
            // Add useful custom fields
            requirements.hasSelf = true;
            requirements.needMembership = (requirements.membershipExpiresIn <= 0 &&
                                           requirements.membershipPendingExpiresIn <= 0 );
            requirements.needRenew = (!requirements.needMembership &&
                                      requirements.membershipExpiresIn <= csSettings.data.timeWarningExpire &&
                                      requirements.membershipPendingExpiresIn <= 0 );
            requirements.canMembershipOut = (requirements.membershipExpiresIn > 0);
            requirements.pendingMembership = (requirements.membershipExpiresIn <= 0 && requirements.membershipPendingExpiresIn > 0);
            requirements.isMember = (requirements.membershipExpiresIn > 0);
            // Force certification count to 0, is not a member yet - fix #269
            requirements.certificationCount = (requirements.isMember && requirements.certifications) ? requirements.certifications.length : 0;
            requirements.willExpireCertificationCount = requirements.certifications ? requirements.certifications.reduce(function(count, cert){
              if (cert.expiresIn <= csSettings.data.timeWarningExpire) {
                cert.willExpire = true;
                return count + 1;
              }
              return count;
            }, 0) : 0;

            resolve(requirements);
          })
          .catch(function(err) {
            // If not a member: continue
            if (!!err &&
                (err.ucode == BMA.errorCodes.NO_MATCHING_MEMBER ||
                 err.ucode == BMA.errorCodes.NO_IDTY_MATCHING_PUB_OR_UID)) {
              resolve({
                hasSelf: false,
                needMembership: true,
                canMembershipOut: false,
                needRenew: false,
                pendingMembership: false,
                needCertifications: false,
                needCertificationCount: 0,
                willNeedCertificationCount: 0
              });
            }
            else {
              reject(err);
            }
{'fraction_non_alphanumeric': 0.11291143535799118, 'fraction_numerical': 0.022395656599932135, 'mean_word_length': 1.6732426303854875, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 12, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1533536', 'n_tokens_mistral': 3375, 'n_tokens_neox': 3128, 'n_words': 689}