<commit_before>/* global Notification */

import difference from 'lodash/difference'

import { NOTIFICATION_PREFERENCE_FAVOURITE_ONLY, NOTIFICATION_PREFERENCE_FRIEND_ONLY, NOTIFICATION_PREFERENCE_NON } from '../constants/settings'

export default function (store) {
  let friendsNotifiedFor = []

  const notifyFriendsOnlineHandler = () => {
    let state = store.getState()
    let notificationsPreference = state.settings.notifications.preference

    // not logged in
    if (!state.auth.authorized && !friendsNotifiedFor.length) {
      return
    }

    // logged out
    if (!state.auth.authorized) {
      friendsNotifiedFor = []
      return
    }

    // notification disabled
    if (notificationsPreference === NOTIFICATION_PREFERENCE_NON) {
      friendsNotifiedFor = []
      return
    }

    const newFriendsToNotifyFor = getOnlineFriendsForNotificationsPreference(
      state.friends.friends,
      notificationsPreference
    )

    // not first run and newFriendsToNotifyFor differ
    if (friendsNotifiedFor.length && friendsNotifiedFor !== newFriendsToNotifyFor) {
      difference(newFriendsToNotifyFor, friendsNotifiedFor).forEach(friend => notify(friend))
    }

    friendsNotifiedFor = newFriendsToNotifyFor
  }

  return store.subscribe(notifyFriendsOnlineHandler)
}

function getOnlineFriendsForNotificationsPreference (friends, notificationsPreference) {
  return friends.filter(friend => {
    if (friend.online) {
      if (notificationsPreference === NOTIFICATION_PREFERENCE_FAVOURITE_ONLY && friend.favourite) {
        return friend
      }

      if (notificationsPreference === NOTIFICATION_PREFERENCE_FRIEND_ONLY) {
        return friend
      }
    }
  })
}

function notify (friend) {
  return new Notification('Friend Online', {
    body: `${friend.gamertag} is online - ${friend.primaryInfo}`,
    icon: friend.gamerpic
  })
}
<commit_msg>fix new friend online notification<commit_after>/* global Notification */

import _ from 'lodash'

import { NOTIFICATION_PREFERENCE_FAVOURITE_ONLY, NOTIFICATION_PREFERENCE_FRIEND_ONLY, NOTIFICATION_PREFERENCE_NON } from '../constants/settings'

export default function (store) {
  let friendsNotifiedFor = []

  const notifyFriendsOnlineHandler = () => {
    let state = store.getState()
    let notificationsPreference = state.settings.notifications.preference

    // not logged in
    if (!state.auth.authorized && !friendsNotifiedFor.length) {
      return
    }

    // logged out
    if (!state.auth.authorized) {
      friendsNotifiedFor = []
      return
    }

    // notification disabled
    if (notificationsPreference === NOTIFICATION_PREFERENCE_NON) {
      friendsNotifiedFor = []
      return
    }

    const newFriendsToNotifyFor = getOnlineFriendsForNotificationsPreference(
      state.friends.friends,
      notificationsPreference
    )

    // not first run and newFriendsToNotifyFor differ
    let newFriendsMap = newFriendsToNotifyFor.map(friend => friend.gamertag)

    if (friendsNotifiedFor.length && !_.isEqual(newFriendsMap, friendsNotifiedFor)) {
      _.difference(newFriendsMap, friendsNotifiedFor).forEach(friend =>
        notify(
          _.find(newFriendsToNotifyFor, { gamertag: friend })
        ))
    }

    friendsNotifiedFor = newFriendsMap
  }

  return store.subscribe(notifyFriendsOnlineHandler)
}

function getOnlineFriendsForNotificationsPreference (friends, notificationsPreference) {
  return friends.filter(friend => {
    if (friend.online) {
      if (notificationsPreference === NOTIFICATION_PREFERENCE_FAVOURITE_ONLY && friend.favourite) {
        return friend
      }

      if (notificationsPreference === NOTIFICATION_PREFERENCE_FRIEND_ONLY) {
        return friend
      }
    }
  })
}

function notify (friend) {
  return new Notification('Friend Online', {
    body: `${friend.gamertag} is online - ${friend.primaryInfo}`,
    icon: friend.gamerpic
  })
}

{'fraction_non_alphanumeric': 0.09181841497819954, 'fraction_numerical': 0.0, 'mean_word_length': 3.6153846153846154, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '409488', 'n_tokens_mistral': 1166, 'n_tokens_neox': 1118, 'n_words': 214}