<commit_before>/**
 * user.ts
 *
 * This is the UI-specific representation of a User.
 */
/// <reference path='../../../../third_party/freedom-typings/social.d.ts' />
/// <reference path='../../../../third_party/typings/lodash/lodash.d.ts' />

import social = require('../../interfaces/social');
import user_interface = require('./ui');
import _ = require('lodash');

export enum GettingConsentState {
  LOCAL_REQUESTED_REMOTE_GRANTED = 100,
  LOCAL_REQUESTED_REMOTE_NO_ACTION,
  REMOTE_OFFERED_LOCAL_NO_ACTION,
  REMOTE_OFFERED_LOCAL_IGNORED,
  NO_OFFER_OR_REQUEST
}

export enum SharingConsentState {
  LOCAL_OFFERED_REMOTE_ACCEPTED = 200,
  LOCAL_OFFERED_REMOTE_NO_ACTION,
  REMOTE_REQUESTED_LOCAL_NO_ACTION,
  REMOTE_REQUESTED_LOCAL_IGNORED,
  NO_OFFER_OR_REQUEST
}

/**
 * UI-specific user.
 */
export class User implements social.BaseUser {

  public name              :string;
  public imageData         :string;
  public url               :string;
  public isGettingFromMe   :boolean = false;
  public isSharingWithMe   :boolean = false;
  // 'filter'-related flags which indicate whether the user should be
  // currently visible in the UI.
  public offeringInstances :social.InstanceData[] = [];
  public allInstanceIds :string[] = [];

  public getExpanded :boolean = false;
  public shareExpanded :boolean = false;

  private consent_ :social.ConsentState;
  public gettingConsentState :GettingConsentState =
      GettingConsentState.NO_OFFER_OR_REQUEST;
  public sharingConsentState :SharingConsentState =
      SharingConsentState.NO_OFFER_OR_REQUEST;

  private isOnline_ :boolean = false;

  /**
   * Initialize the user to an 'empty' default.
   */
  constructor(public userId :string, public network :user_interface.Network,
      private ui_ :user_interface.UserInterface) {
    console.log('new user: ' + this.userId);
    this.name = '';
    this.offeringInstances = [];
  }

  /**
   * Update user details.
   */
  public update = (payload :social.UserData) => {
    var profile :social.UserProfileMessage = payload.user;
    if (this.userId !== profile.userId) {
      console.error('Unexpected userId: ' + profile.userId);
    }

    // if we do not have stored state, no use in checking for changes
    if (this.consent_) {
      // notifications for get mode
      if (!payload.consent.ignoringRemoteUserOffer) {
        if (this.offeringInstances.length === 0 && payload.offeringInstances.length > 0) {
          if (payload.consent.localRequestsAccessFromRemote) {
            this.ui_.showNotification(profile.name + ' granted you access',
                         { mode: 'get', user: this.userId });
          } else {
            this.ui_.showNotification(profile.name + ' offered you access',
                         { mode: 'get', user: this.userId });
          }
        }
      }

      // notifications for share mode
      if (!payload.consent.ignoringRemoteUserRequest) {
        if (!this.consent_.remoteRequestsAccessFromLocal && payload.consent.remoteRequestsAccessFromLocal) {
          if (payload.consent.localGrantsAccessToRemote) {
            this.ui_.showNotification(profile.name + ' has accepted your offer for access',
                         { mode: 'share', user: this.userId });
          } else {
            this.ui_.showNotification(profile.name + ' is requesting access',
                         { mode: 'share', user: this.userId });
          }
        }
      }
    }

    this.name = profile.name;
    this.imageData = profile.imageData || user_interface.DEFAULT_USER_IMG;
    this.url = profile.url;

    // iterate backwards to allow removing elements
    var i = this.offeringInstances.length;
    while (i--) {
      var found = _.findIndex(payload.offeringInstances, (obj) => {
        return obj.instanceId === this.offeringInstances[i].instanceId;
      });

      if (found !== -1) {
        _.merge(this.offeringInstances[i], payload.offeringInstances[found]);
        payload.offeringInstances.splice(found, 1);
      } else {
        this.offeringInstances.splice(i, 1);
      }
    }

    for (var j in payload.offeringInstances) {
      this.offeringInstances.push(payload.offeringInstances[j]);
    }

    //this.offeringInstances = payload.offeringInstances;
    this.allInstanceIds = payload.allInstanceIds;
    this.updateInstanceDescriptions();
    this.consent_ = payload.consent;
    this.isOnline_ = payload.isOnline;

    // Update gettingConsentState, used to display correct getting buttons.
    if (this.offeringInstances.length > 0) {
      if (this.consent_.localRequestsAccessFromRemote) {
        this.gettingConsentState =
            GettingConsentState.LOCAL_REQUESTED_REMOTE_GRANTED;
      } else if (this.consent_.ignoringRemoteUserOffer) {
        this.gettingConsentState =
            GettingConsentState.REMOTE_OFFERED_LOCAL_IGNORED;
      } else {
        this.gettingConsentState =
            GettingConsentState.REMOTE_OFFERED_LOCAL_NO_ACTION;
      }
    } else {
      if (this.consent_.localRequestsAccessFromRemote) {
        this.gettingConsentState =
            GettingConsentState.LOCAL_REQUESTED_REMOTE_NO_ACTION;
      } else {
        this.gettingConsentState = GettingConsentState.NO_OFFER_OR_REQUEST;
      }
    }

    // Update sharingConsentState, used to display correct sharing buttons.
    if (this.consent_.remoteRequestsAccessFromLocal) {
      if (this.consent_.localGrantsAccessToRemote) {
        this.sharingConsentState =
            SharingConsentState.LOCAL_OFFERED_REMOTE_ACCEPTED;
      } else if (this.consent_.ignoringRemoteUserRequest) {
        this.sharingConsentState =
            SharingConsentState.REMOTE_REQUESTED_LOCAL_IGNORED;
      } else {
        this.sharingConsentState =
            SharingConsentState.REMOTE_REQUESTED_LOCAL_NO_ACTION;
      }
    } else {
      if (this.consent_.localGrantsAccessToRemote) {
        this.sharingConsentState =
            SharingConsentState.LOCAL_OFFERED_REMOTE_NO_ACTION;
      } else {
        this.sharingConsentState = SharingConsentState.NO_OFFER_OR_REQUEST;
      }<commit_msg>Auto expand contact on new offer/request<commit_after>/**
 * user.ts
 *
 * This is the UI-specific representation of a User.
 */
/// <reference path='../../../../third_party/freedom-typings/social.d.ts' />
/// <reference path='../../../../third_party/typings/lodash/lodash.d.ts' />

import social = require('../../interfaces/social');
import user_interface = require('./ui');
import _ = require('lodash');

export enum GettingConsentState {
  LOCAL_REQUESTED_REMOTE_GRANTED = 100,
  LOCAL_REQUESTED_REMOTE_NO_ACTION,
  REMOTE_OFFERED_LOCAL_NO_ACTION,
  REMOTE_OFFERED_LOCAL_IGNORED,
  NO_OFFER_OR_REQUEST
}

export enum SharingConsentState {
  LOCAL_OFFERED_REMOTE_ACCEPTED = 200,
  LOCAL_OFFERED_REMOTE_NO_ACTION,
  REMOTE_REQUESTED_LOCAL_NO_ACTION,
  REMOTE_REQUESTED_LOCAL_IGNORED,
  NO_OFFER_OR_REQUEST
}

/**
 * UI-specific user.
 */
export class User implements social.BaseUser {

  public name              :string;
  public imageData         :string;
  public url               :string;
  public isGettingFromMe   :boolean = false;
  public isSharingWithMe   :boolean = false;
  // 'filter'-related flags which indicate whether the user should be
  // currently visible in the UI.
  public offeringInstances :social.InstanceData[] = [];
  public allInstanceIds :string[] = [];

  public getExpanded :boolean = false;
  public shareExpanded :boolean = false;

  private consent_ :social.ConsentState;
  public gettingConsentState :GettingConsentState =
      GettingConsentState.NO_OFFER_OR_REQUEST;
  public sharingConsentState :SharingConsentState =
      SharingConsentState.NO_OFFER_OR_REQUEST;

  private isOnline_ :boolean = false;

  /**
   * Initialize the user to an 'empty' default.
   */
  constructor(public userId :string, public network :user_interface.Network,
      private ui_ :user_interface.UserInterface) {
    console.log('new user: ' + this.userId);
    this.name = '';
    this.offeringInstances = [];
  }

  /**
   * Update user details.
   */
  public update = (payload :social.UserData) => {
    var profile :social.UserProfileMessage = payload.user;
    if (this.userId !== profile.userId) {
      console.error('Unexpected userId: ' + profile.userId);
    }

    // if we do not have stored state, no use in checking for changes
    if (this.consent_) {
      // notifications for get mode
      if (!payload.consent.ignoringRemoteUserOffer) {
        if (this.offeringInstances.length === 0 && payload.offeringInstances.length > 0) {
          if (payload.consent.localRequestsAccessFromRemote) {
            this.ui_.showNotification(profile.name + ' granted you access',
                         { mode: 'get', user: this.userId });
          } else {
            this.ui_.showNotification(profile.name + ' offered you access',
                         { mode: 'get', user: this.userId });
          }
        }
      }

      // notifications for share mode
      if (!payload.consent.ignoringRemoteUserRequest) {
        if (!this.consent_.remoteRequestsAccessFromLocal && payload.consent.remoteRequestsAccessFromLocal) {
          if (payload.consent.localGrantsAccessToRemote) {
            this.ui_.showNotification(profile.name + ' has accepted your offer for access',
                         { mode: 'share', user: this.userId });
          } else {
            this.ui_.showNotification(profile.name + ' is requesting access',
                         { mode: 'share', user: this.userId });
          }
        }
      }
    }

    this.name = profile.name;
    this.imageData = profile.imageData || user_interface.DEFAULT_USER_IMG;
    this.url = profile.url;

    // iterate backwards to allow removing elements
    var i = this.offeringInstances.length;
    while (i--) {
      var found = _.findIndex(payload.offeringInstances, (obj) => {
        return obj.instanceId === this.offeringInstances[i].instanceId;
      });

      if (found !== -1) {
        _.merge(this.offeringInstances[i], payload.offeringInstances[found]);
        payload.offeringInstances.splice(found, 1);
      } else {
        this.offeringInstances.splice(i, 1);
      }
    }

    for (var j in payload.offeringInstances) {
      this.offeringInstances.push(payload.offeringInstances[j]);
    }

    //this.offeringInstances = payload.offeringInstances;
    this.allInstanceIds = payload.allInstanceIds;
    this.updateInstanceDescriptions();
    this.consent_ = payload.consent;
    this.isOnline_ = payload.isOnline;

    // Update gettingConsentState, used to display correct getting buttons.
    if (this.offeringInstances.length > 0) {
      if ((this.gettingConsentState ==
          GettingConsentState.NO_OFFER_OR_REQUEST ||
          this.gettingConsentState ==
          GettingConsentState.LOCAL_REQUESTED_REMOTE_NO_ACTION) &&
          !this.consent_.ignoringRemoteUserOffer) {
        this.getExpanded = true;
      }
      if (this.consent_.localRequestsAccessFromRemote) {
        this.gettingConsentState =
            GettingConsentState.LOCAL_REQUESTED_REMOTE_GRANTED;
      } else if (this.consent_.ignoringRemoteUserOffer) {
        this.gettingConsentState =
            GettingConsentState.REMOTE_OFFERED_LOCAL_IGNORED;
      } else {
        this.gettingConsentState =
            GettingConsentState.REMOTE_OFFERED_LOCAL_NO_ACTION;
      }
    } else {
      if (this.consent_.localRequestsAccessFromRemote) {
        this.gettingConsentState =
            GettingConsentState.LOCAL_REQUESTED_REMOTE_NO_ACTION;
      } else {
        this.gettingConsentState = GettingConsentState.NO_OFFER_OR_REQUEST;
      }
    }

    // Update sharingConsentState, used to display correct sharing buttons.
    if (this.consent_.remoteRequestsAccessFromLocal) {
      if ((this.sharingConsentState ==
          SharingConsentState.NO_OFFER_OR_REQUEST ||
          this.sharingConsentState ==
          SharingConsentState.LOCAL_OFFERED_REMOTE_NO_ACTION) &&
          !this.consent_.ignoringRemoteUserRequest) {
        this.shareExpanded = true;
      }
      if (this.consent_.localGrantsAccessToRemote) {
        this.sharingConsentState =
            SharingConsentState.LOCAL_OFFERED_REMOTE_ACCEPTED;
      } else if (this.consent_.ignoringRemoteUserRequest) {
        this.sharingConsentState =
            SharingConsentState.REMOTE_REQUESTED_LOCAL_IGNORED;
      } else {
        this.sharingConsentState =
            SharingConsentState.REMOTE_REQUESTED_LOCAL_NO_ACTION;
      }
    } else {
      if (this.consent_.localGrantsAccessToRemote) {
        this.sharingConsentState =
            SharingConsentState.LOCAL_OFFERED_REMOTE_NO_ACTION;
      } else {
        this.sharingConsentState = SharingConsentState.NO_OFFER_OR_REQUEST;
      }
{'fraction_non_alphanumeric': 0.09986684420772303, 'fraction_numerical': 0.0018798464792041984, 'mean_word_length': 2.810205908683975, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '5875898', 'n_tokens_mistral': 3922, 'n_tokens_neox': 3764, 'n_words': 685}