<commit_before>class ChatAppController extends AppController

  KD.registerAppClass this,
    name         : "Chat"
    route        : "/:name?/Chat"
    background   : yes

  constructor:(options, data)->
    super options, data

    notificationController = KD.getSingleton 'notificationController'
    notificationController.on 'chatRequest', @bound 'handleChatRequest'
    notificationController.on 'chatOpen', @bound 'handleChatOpen'

    @channels = {}

  handleChatOpen:({routingKey, bindingKey, publicName})->
    channel = KD.remote.mq.setP2PKeys \
      publicName, { routingKey, bindingKey }, 'secret'
    @channels[publicName] = channel  if channel?

  create:(invitees, callback)->
    chatPanel = KD.getSingleton 'chatPanel'
    {JChatConversation} = KD.remote.api
    JChatConversation.create invitees, (err, conversation)=>
      return callback err  if err
      chatChannel = @subscribe conversation.publicName
      callback null, new ChannelWrapper chatChannel

  subscribe:(publicName)->
    chatChannel = KD.remote.subscribe publicName,
      serviceType : 'chat'
      exchange    : 'chat'
      isP2P       : yes
    @channels[publicName] = chatChannel

  handleChatRequest:(request, callback)->
    {invitee, publicName} = request

    if invitee isnt KD.nick()
      throw new Error 'Red alert!  Security breach detected!'

    @addConversationToChatPanel publicName

  addConversationToChatPanel:(publicName, conversation)->
    @subscribe publicName
    chatPanel = KD.getSingleton 'chatPanel'

    if conversation
      chatChannel = @channels[publicName]
      chatPanel.createConversation {chatChannel, conversation}
    else
      {JChatConversation} = KD.remote.api
      JChatConversation.fetch publicName, (err, conversation)=>
        chatChannel = @channels[publicName]
        chatPanel.createConversation {chatChannel, conversation}

  leave:({conversation, chatChannel}, callback)->
    content = """ When you leave conversation you will not
                  receive any messages. And you will also loose
                  current messages in this conversation.
                  Do you want to continue? """

    modal = new KDModalView
      title          : "Do you want to leave this conversation?"
      content        : "<div class='modalformline'><p>#{content}</p></div>"
      height         : "auto"
      overlay        : yes
      buttons        :
        Leave       :
          style      : "modal-clean-red"
          callback   : ->
            chatChannel?.close()?.off()
            conversation.leave (err)=>
              warn err  if err
              modal.destroy()
              callback?()
        Cancel       :
          style      : "modal-cancel"
          title      : "cancel"
          callback   : ->
            modal.destroy()

    modal.buttons.Leave.blur()

class ChannelWrapper extends KDObject
  constructor:(@channel)-> super {}
  sendMessage:(message) -> @channel.publish JSON.stringify message<commit_msg>Fixed fat-arrow<commit_after>class ChatAppController extends AppController

  KD.registerAppClass this,
    name         : "Chat"
    route        : "/:name?/Chat"
    background   : yes

  constructor:(options, data)->
    super options, data

    notificationController = KD.getSingleton 'notificationController'
    notificationController.on 'chatRequest', @bound 'handleChatRequest'
    notificationController.on 'chatOpen', @bound 'handleChatOpen'

    @channels = {}

  handleChatOpen:({routingKey, bindingKey, publicName})->
    channel = KD.remote.mq.setP2PKeys \
      publicName, { routingKey, bindingKey }, 'secret'
    @channels[publicName] = channel  if channel?

  create:(invitees, callback)->
    chatPanel = KD.getSingleton 'chatPanel'
    {JChatConversation} = KD.remote.api
    JChatConversation.create invitees, (err, conversation)=>
      return callback err  if err
      chatChannel = @subscribe conversation.publicName
      callback null, new ChannelWrapper chatChannel

  subscribe:(publicName)->
    chatChannel = KD.remote.subscribe publicName,
      serviceType : 'chat'
      exchange    : 'chat'
      isP2P       : yes
    @channels[publicName] = chatChannel

  handleChatRequest:(request, callback)->
    {invitee, publicName} = request

    if invitee isnt KD.nick()
      throw new Error 'Red alert!  Security breach detected!'

    @addConversationToChatPanel publicName

  addConversationToChatPanel:(publicName, conversation)->
    @subscribe publicName
    chatPanel = KD.getSingleton 'chatPanel'

    if conversation
      chatChannel = @channels[publicName]
      chatPanel.createConversation {chatChannel, conversation}
    else
      {JChatConversation} = KD.remote.api
      JChatConversation.fetch publicName, (err, conversation)=>
        chatChannel = @channels[publicName]
        chatPanel.createConversation {chatChannel, conversation}

  leave:({conversation, chatChannel}, callback)->
    content = """ When you leave conversation you will not
                  receive any messages. And you will also loose
                  current messages in this conversation.
                  Do you want to continue? """

    modal = new KDModalView
      title          : "Do you want to leave this conversation?"
      content        : "<div class='modalformline'><p>#{content}</p></div>"
      height         : "auto"
      overlay        : yes
      buttons        :
        Leave       :
          style      : "modal-clean-red"
          callback   : ->
            chatChannel?.close()?.off()
            conversation.leave (err)->
              warn err  if err
              modal.destroy()
              callback?()
        Cancel       :
          style      : "modal-cancel"
          title      : "cancel"
          callback   : ->
            modal.destroy()

    modal.buttons.Leave.blur()

class ChannelWrapper extends KDObject
  constructor:(@channel)-> super {}
  sendMessage:(message) -> @channel.publish JSON.stringify message
{'fraction_non_alphanumeric': 0.09554886211512717, 'fraction_numerical': 0.0006693440428380187, 'mean_word_length': 2.5158823529411767, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 39, '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': '5526482', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1518, 'n_words': 404}