<commit_before>import Rx from 'rx-lite'
import NylasStore from 'nylas-store';
import {ipcRenderer, remote} from 'electron';
import request from 'request';
import url from 'url'

import Utils from '../models/utils';
import Actions from '../actions';
import {APIError} from '../errors'
import KeyManager from '../../key-manager'
import DatabaseStore from './database-store'

// Note this key name is used when migrating to Nylas Pro accounts from
// old N1.
const KEYCHAIN_NAME = 'Nylas Account';

class IdentityStore extends NylasStore {

  constructor() {
    super();
    this._identity = null
  }

  async activate() {
    if (NylasEnv.isEmptyWindow()) {
      NylasEnv.onWindowPropsReceived(() => {
        this.deactivate();
        this.activate();
      })
      return
    }

    NylasEnv.config.onDidChange('env', this._onEnvChanged);
    this._onEnvChanged();

    this.listenTo(Actions.logoutNylasIdentity, this._onLogoutNylasIdentity);

    const q = DatabaseStore.findJSONBlob("NylasID");
    this._disp = Rx.Observable.fromQuery(q).subscribe(this._onIdentityChanged)

    const identity = await DatabaseStore.run(q)
    this._onIdentityChanged(identity)

    this._fetchAndPollRemoteIdentity()
  }

  deactivate() {
    if (this._disp) this._disp.dispose();
    this.stopListeningToAll()
  }

  identity() {
    if (!this._identity || !this._identity.id) return null
    return Utils.deepClone(this._identity);
  }

  hasProAccess() {
    return this._identity && this._identity.has_pro_access
  }

  identityId() {
    if (!this._identity) {
      return null;
    }
    return this._identity.id;
  }

  _fetchAndPollRemoteIdentity() {
    if (!NylasEnv.isMainWindow()) return;
    if (!['staging', 'production'].includes(NylasEnv.config.get('env'))) return;<commit_msg>[client-app] Always fetch and update identity regardless of env<commit_after>import Rx from 'rx-lite'
import NylasStore from 'nylas-store';
import {ipcRenderer, remote} from 'electron';
import request from 'request';
import url from 'url'

import Utils from '../models/utils';
import Actions from '../actions';
import {APIError} from '../errors'
import KeyManager from '../../key-manager'
import DatabaseStore from './database-store'

// Note this key name is used when migrating to Nylas Pro accounts from
// old N1.
const KEYCHAIN_NAME = 'Nylas Account';

class IdentityStore extends NylasStore {

  constructor() {
    super();
    this._identity = null
  }

  async activate() {
    if (NylasEnv.isEmptyWindow()) {
      NylasEnv.onWindowPropsReceived(() => {
        this.deactivate();
        this.activate();
      })
      return
    }

    NylasEnv.config.onDidChange('env', this._onEnvChanged);
    this._onEnvChanged();

    this.listenTo(Actions.logoutNylasIdentity, this._onLogoutNylasIdentity);

    const q = DatabaseStore.findJSONBlob("NylasID");
    this._disp = Rx.Observable.fromQuery(q).subscribe(this._onIdentityChanged)

    const identity = await DatabaseStore.run(q)
    this._onIdentityChanged(identity)

    this._fetchAndPollRemoteIdentity()
  }

  deactivate() {
    if (this._disp) this._disp.dispose();
    this.stopListeningToAll()
  }

  identity() {
    if (!this._identity || !this._identity.id) return null
    return Utils.deepClone(this._identity);
  }

  hasProAccess() {
    return this._identity && this._identity.has_pro_access
  }

  identityId() {
    if (!this._identity) {
      return null;
    }
    return this._identity.id;
  }

  _fetchAndPollRemoteIdentity() {
    if (!NylasEnv.isMainWindow()) return;
{'fraction_non_alphanumeric': 0.1280746395250212, 'fraction_numerical': 0.0005654509471303364, 'mean_word_length': 3.5534105534105533, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5103536', 'n_tokens_mistral': 1114, 'n_tokens_neox': 1082, 'n_words': 251}