<commit_before>kookies            = require 'kookies'
getGroup           = require './util/getGroup'
whoami             = require './util/whoami'
envDataProvider    = require 'app/userenvironmentdataprovider'
kd                 = require 'kd'
KDModalView        = kd.ModalView
KDNotificationView = kd.NotificationView
KDObject           = kd.Object


module.exports = class NotificationController extends KDObject

  deleteUserCookie = -> kookies.expire 'clientId'

  displayEmailConfirmedNotification = (modal)->
    modal.off 'KDObjectWillBeDestroyed'
    new KDNotificationView
      title    : 'Thanks for confirming your e-mail address'
      duration : 5000

    return modal.destroy()


  constructor:->

    super

    kd.getSingleton('mainController').ready @bound 'init'


  init: ->

    @setListeners()
    @subscribeToRealtimeUpdates()


  subscribeToRealtimeUpdates:->

    @notificationChannel = null

    {realtime} = kd.singletons
    realtime.subscribeNotification (err, notificationChannel) =>

      @notificationChannel = notificationChannel

      return kd.warn 'notification subscription error', err  if err

      @notificationChannel.on 'message', (notification)=>

        @emit 'NotificationHasArrived', notification

        { contents, context, event } = notification

        return unless contents

        event = if contents.event then contents.event else event
        # TODO enable this with team product
        # event = unless context is getGroup().slug then "#{event}-off-context" else event
        # event is mainly -> ChannelUpdateHappened // do not delete this line. here for search purposes. - SY
        @emit event, contents  if event

      @notificationChannel.on 'social', (notification) =>
        { contents, context, event } = notification

        @emit event, contents  if event


  setListeners:->

    @on 'GuestTimePeriodHasEnded', deleteUserCookie

    @once 'EmailShouldBeConfirmed', ->
      {firstName, nickname} = whoami().profile
      kd.getSingleton('appManager').tell 'Account', 'displayConfirmEmailModal', name, nickname, (modal)=>
        @once 'EmailConfirmed', displayEmailConfirmedNotification.bind this, modal
        modal.on "KDObjectWillBeDestroyed", deleteUserCookie.bind this

    @on 'MachineListUpdated', ({machineUId, action}) ->
      envDataProvider.setLastUpdatedMachineUId machineUId
      kd.singletons.computeController.reset yes

    @on 'UsernameChanged', ({username, oldUsername}) ->
      # FIXME: because of this (https://app.asana.com/0/search/6604719544802/6432131515387)
      deleteUserCookie()

      new KDModalView
        title         : "Your username was changed"
        overlay       : yes<commit_msg>IDE: Show user removed modal<commit_after>kookies            = require 'kookies'
getGroup           = require './util/getGroup'
whoami             = require './util/whoami'
envDataProvider    = require 'app/userenvironmentdataprovider'
kd                 = require 'kd'
KDModalView        = kd.ModalView
KDNotificationView = kd.NotificationView
KDObject           = kd.Object


module.exports = class NotificationController extends KDObject

  deleteUserCookie = -> kookies.expire 'clientId'

  displayEmailConfirmedNotification = (modal)->
    modal.off 'KDObjectWillBeDestroyed'
    new KDNotificationView
      title    : 'Thanks for confirming your e-mail address'
      duration : 5000

    return modal.destroy()


  constructor:->

    super

    kd.getSingleton('mainController').ready @bound 'init'


  init: ->

    @setListeners()
    @subscribeToRealtimeUpdates()


  subscribeToRealtimeUpdates:->

    @notificationChannel = null

    {realtime} = kd.singletons
    realtime.subscribeNotification (err, notificationChannel) =>

      @notificationChannel = notificationChannel

      return kd.warn 'notification subscription error', err  if err

      @notificationChannel.on 'message', (notification)=>

        @emit 'NotificationHasArrived', notification

        { contents, context, event } = notification

        return unless contents

        event = if contents.event then contents.event else event
        # TODO enable this with team product
        # event = unless context is getGroup().slug then "#{event}-off-context" else event
        # event is mainly -> ChannelUpdateHappened // do not delete this line. here for search purposes. - SY
        @emit event, contents  if event

      @notificationChannel.on 'social', (notification) =>
        { contents, context, event } = notification

        @emit event, contents  if event


  setListeners:->

    @on 'GuestTimePeriodHasEnded', deleteUserCookie

    @once 'EmailShouldBeConfirmed', ->
      {firstName, nickname} = whoami().profile
      kd.getSingleton('appManager').tell 'Account', 'displayConfirmEmailModal', name, nickname, (modal)=>
        @once 'EmailConfirmed', displayEmailConfirmedNotification.bind this, modal
        modal.on "KDObjectWillBeDestroyed", deleteUserCookie.bind this

    @on 'MachineListUpdated', ({machineUId, action}) ->
      envDataProvider.setLastUpdatedMachineUId machineUId

      switch action
        when 'removed'
          if ideInstance = envDataProvider.getIDEFromUId machineUId
            ideInstance.showUserRemovedModal()

      kd.singletons.computeController.reset yes

    @on 'UsernameChanged', ({username, oldUsername}) ->
      # FIXME: because of this (https://app.asana.com/0/search/6604719544802/6432131515387)
      deleteUserCookie()

      new KDModalView
        title         : "Your username was changed"
        overlay       : yes
{'fraction_non_alphanumeric': 0.09131602506714413, 'fraction_numerical': 0.011101163831692032, 'mean_word_length': 3.0158159597411935, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 2, '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': '1359468', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1447, 'n_words': 390}