<commit_before>import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import classNames from 'classnames';
import {autobind} from 'core-decorators';

import LoadingScreen from '@ciscospark/react-component-loading-screen';
import Spinner from '@ciscospark/react-component-spinner';
import ErrorDisplay from '@ciscospark/react-component-error-display';

import {addError, removeError} from '@ciscospark/redux-module-errors';
import {fetchCurrentUser} from '@ciscospark/redux-module-user';
import {
  acknowledgeSpace,
  fetchSpace,
  removeSpace,
  initialFetchSpaces,
  updateSpaceWithActivity,
  updateSpaceRead
} from '@ciscospark/redux-module-spaces';
import {
  fetchAvatar
} from '@ciscospark/redux-module-avatar';
import {events as metricEvents} from '@ciscospark/react-redux-spark-metrics';
import {connectToMercury} from '@ciscospark/redux-module-mercury';
import IncomingCall from '@ciscospark/react-component-incoming-call';

import messages from './messages';
import {getRecentsWidgetProps} from './selector';
import {
  updateWidgetStatus,
  updateWebRTCSupport,
  storeIncomingCall,
  deleteIncomingCall
} from './actions';
import SpacesList from './components/spaces-list';
import styles from './styles.css';
import {
  eventNames,
  constructMessagesEventData,
  constructRoomsEventData,
  constructMembershipEventData,
  constructCallEventData
} from './events';


export class RecentsWidget extends Component {

  componentWillReceiveProps(nextProps) {
    this.checkForMercuryErrors(nextProps);
    this.setup(nextProps);
    this.fetchAllAvatar(nextProps);
  }

  shouldComponentUpdate(nextProps) {
    return nextProps.spaces !== this.props.spaces
      || nextProps.avatar.get(`items`) !== this.props.avatar.get(`items`)
      || nextProps.mercury !== this.props.mercury
      || nextProps.errors !== this.props.errors
      || nextProps.user !== this.props.user
      || nextProps.widgetRecents !== this.props.widgetRecents;
  }

  componentWillUnmount() {
    this.props.removeWidget();
  }

  @autobind
  setup(props) { // eslint-disable-line complexity
    const {
      errors,
      spark,
      sparkInstance,
      sparkState,
      mercury,
      metrics,
      user,
      spaces,
      spacesList,
      widgetRecents
    } = props;

    // Check for spark device errors (auth)
    const registerErrorId = `spark.register`;
    if (sparkState.get(`registerError`) && (!errors.get(`hasError`) || !errors.get(`errors`).has(registerErrorId))) {
      const {formatMessage} = props.intl;
      const error = spark.get(`error`);
      const displayTitle = formatMessage(messages.unableToLoad);
      let displaySubtitle = formatMessage(messages.unknownError);
      if (error.statusCode === 401) {
        displaySubtitle = formatMessage(messages.errorBadToken);
      }
      props.addError({
        id: registerErrorId,
        displayTitle,
        displaySubtitle,
        temporary: false,
        code: error.statusCode
      });
    }

    else if (sparkInstance && sparkState.get(`authenticated`) && sparkState.get(`registered`)) {
      // Get current user details
      if (!user.getIn([`status`, `isFetchingCurrentUser`]) && !user.get(`currentUser`)) {
        props.fetchCurrentUser(sparkInstance);
      }

      // Setup Web Sockets
      if (!mercury.getIn([`status`, `hasConnected`])
        && !mercury.getIn([`status`, `connecting`])
        && !mercury.getIn([`status`, `connected`])
        && sparkInstance.internal.device.registered) {
        props.connectToMercury(sparkInstance);
      }

      // Grab spaces
      if (!spaces.getIn([`status`, `isFetching`])
        && mercury.getIn([`status`, `connected`])
        && !spaces.get(`items`).count()) {
        props.initialFetchSpaces(sparkInstance);
      }

      if (sparkInstance && sparkInstance.phone && !widgetRecents.getIn([`status`, `hasCheckedWebRTCSupport`])) {
        props.updateWidgetStatus({hasCheckedWebRTCSupport: true});
        sparkInstance.phone.isCallingSupported().then((supported) => {
          props.updateWebRTCSupport(supported);
        });
      }

    }
    if (spacesList.count()) {
      metrics.sendEndMetric(metricEvents.WIDGET_LOAD);
    }
  }

  @autobind
  fetchAllAvatar(nextProps) {
    const {
      user,
      spaces,
      spacesList,
      sparkInstance,
      widgetRecents
    } = nextProps;

    if (user.get(`currentUser`)
      && spaces.getIn(`status`, `hasFetched`)
      && this.props.spacesList
      && spacesList.count() > this.props.spacesList.count()
      ) {
      if (!widgetRecents.getIn([`status`, `isListeningForNewActivity`])) {
        this.listenForNewActivity(sparkInstance, nextProps);
        this.listenForCall(sparkInstance, nextProps);
      }
      spaces.get(`items`).forEach((s) => {
        if (s.get(`type`) === `direct`) {
          if (s.get(`participants`).count() > 1) {
            // Get the user ID of the participant that isn't current user
            const currentUserId = user.get(`currentUser`).id;
            const otherUser = s.get(`participants`).find((p) => p.get(`id`) !== currentUserId);
            if (otherUser) {
              const otherUserId = otherUser.get(`id`);
              this.fetchAvatar({userId: otherUserId}, nextProps);
            }<commit_msg>refactor(widget-recents): Change fetchAllAvatar method name<commit_after>import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import classNames from 'classnames';
import {autobind} from 'core-decorators';

import LoadingScreen from '@ciscospark/react-component-loading-screen';
import Spinner from '@ciscospark/react-component-spinner';
import ErrorDisplay from '@ciscospark/react-component-error-display';

import {addError, removeError} from '@ciscospark/redux-module-errors';
import {fetchCurrentUser} from '@ciscospark/redux-module-user';
import {
  acknowledgeSpace,
  fetchSpace,
  removeSpace,
  initialFetchSpaces,
  updateSpaceWithActivity,
  updateSpaceRead
} from '@ciscospark/redux-module-spaces';
import {
  fetchAvatar
} from '@ciscospark/redux-module-avatar';
import {events as metricEvents} from '@ciscospark/react-redux-spark-metrics';
import {connectToMercury} from '@ciscospark/redux-module-mercury';
import IncomingCall from '@ciscospark/react-component-incoming-call';

import messages from './messages';
import {getRecentsWidgetProps} from './selector';
import {
  updateWidgetStatus,
  updateWebRTCSupport,
  storeIncomingCall,
  deleteIncomingCall
} from './actions';
import SpacesList from './components/spaces-list';
import styles from './styles.css';
import {
  eventNames,
  constructMessagesEventData,
  constructRoomsEventData,
  constructMembershipEventData,
  constructCallEventData
} from './events';


export class RecentsWidget extends Component {

  componentWillReceiveProps(nextProps) {
    this.checkForMercuryErrors(nextProps);
    this.setup(nextProps);
    this.fetchAllAvatars(nextProps);
  }

  shouldComponentUpdate(nextProps) {
    return nextProps.spaces !== this.props.spaces
      || nextProps.avatar.get(`items`) !== this.props.avatar.get(`items`)
      || nextProps.mercury !== this.props.mercury
      || nextProps.errors !== this.props.errors
      || nextProps.user !== this.props.user
      || nextProps.widgetRecents !== this.props.widgetRecents;
  }

  componentWillUnmount() {
    this.props.removeWidget();
  }

  @autobind
  setup(props) { // eslint-disable-line complexity
    const {
      errors,
      spark,
      sparkInstance,
      sparkState,
      mercury,
      metrics,
      user,
      spaces,
      spacesList,
      widgetRecents
    } = props;

    // Check for spark device errors (auth)
    const registerErrorId = `spark.register`;
    if (sparkState.get(`registerError`) && (!errors.get(`hasError`) || !errors.get(`errors`).has(registerErrorId))) {
      const {formatMessage} = props.intl;
      const error = spark.get(`error`);
      const displayTitle = formatMessage(messages.unableToLoad);
      let displaySubtitle = formatMessage(messages.unknownError);
      if (error.statusCode === 401) {
        displaySubtitle = formatMessage(messages.errorBadToken);
      }
      props.addError({
        id: registerErrorId,
        displayTitle,
        displaySubtitle,
        temporary: false,
        code: error.statusCode
      });
    }

    else if (sparkInstance && sparkState.get(`authenticated`) && sparkState.get(`registered`)) {
      // Get current user details
      if (!user.getIn([`status`, `isFetchingCurrentUser`]) && !user.get(`currentUser`)) {
        props.fetchCurrentUser(sparkInstance);
      }

      // Setup Web Sockets
      if (!mercury.getIn([`status`, `hasConnected`])
        && !mercury.getIn([`status`, `connecting`])
        && !mercury.getIn([`status`, `connected`])
        && sparkInstance.internal.device.registered) {
        props.connectToMercury(sparkInstance);
      }

      // Grab spaces
      if (!spaces.getIn([`status`, `isFetching`])
        && mercury.getIn([`status`, `connected`])
        && !spaces.get(`items`).count()) {
        props.initialFetchSpaces(sparkInstance);
      }

      if (sparkInstance && sparkInstance.phone && !widgetRecents.getIn([`status`, `hasCheckedWebRTCSupport`])) {
        props.updateWidgetStatus({hasCheckedWebRTCSupport: true});
        sparkInstance.phone.isCallingSupported().then((supported) => {
          props.updateWebRTCSupport(supported);
        });
      }

    }
    if (spacesList.count()) {
      metrics.sendEndMetric(metricEvents.WIDGET_LOAD);
    }
  }

  @autobind
  fetchAllAvatars(nextProps) {
    const {
      user,
      spaces,
      spacesList,
      sparkInstance,
      widgetRecents
    } = nextProps;

    if (user.get(`currentUser`)
      && spaces.getIn(`status`, `hasFetched`)
      && this.props.spacesList
      && spacesList.count() > this.props.spacesList.count()
      ) {
      if (!widgetRecents.getIn([`status`, `isListeningForNewActivity`])) {
        this.listenForNewActivity(sparkInstance, nextProps);
        this.listenForCall(sparkInstance, nextProps);
      }
      spaces.get(`items`).forEach((s) => {
        if (s.get(`type`) === `direct`) {
          if (s.get(`participants`).count() > 1) {
            // Get the user ID of the participant that isn't current user
            const currentUserId = user.get(`currentUser`).id;
            const otherUser = s.get(`participants`).find((p) => p.get(`id`) !== currentUserId);
            if (otherUser) {
              const otherUserId = otherUser.get(`id`);
              this.fetchAvatar({userId: otherUserId}, nextProps);
            }
{'fraction_non_alphanumeric': 0.13530066815144767, 'fraction_numerical': 0.0007423904974016332, 'mean_word_length': 3.352584814216478, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '15690', 'n_tokens_mistral': 3245, 'n_tokens_neox': 3191, 'n_words': 580}