<commit_before>AM = Artificial.Mirage
LOI = LandsOfIllusions
PAA = PixelArtAcademy

class PAA.PixelBoy.Apps.Components.Conversation extends AM.Component
  @register "PixelArtAcademy.PixelBoy.Apps.Components.Conversation"

  onCreated: ->
    super

    conversation = @data()
    @subscribe 'LandsOfIllusions.Conversations.Line.linesForConversation', conversation._id

  lines: ->
    conversation = @data()

    LOI.Conversations.Line.documents.find
      'conversation._id': conversation._id
    ,
      sort:
        time: 1

  textStyle: ->
    line = @currentData()

    # Set the color to character's color.
    color: "##{line.character.colorObject()?.getHexString()}"

  formattedText: ->
    line = @currentData()
    text = line.text

    # DANGER ZONE:
    # We are using direct HTML injection with triple braces, i.e. {{{formattedText}}}, so make sure we escape properly.
    div = document.createElement 'div'
    div.appendChild document.createTextNode text
    escapedText = div.innerHTML
    
    # Replace urls with links.
    urlRegex = /(https?):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/

    formattedText = escapedText.replace urlRegex, (url, protocol, domain, path) =>
      urlText = domain

      if path
        # Make sure the path is not longer than 10 characters.
        path = "/…#{path.substring(path.length-8)}" if path.length > 10

        # Add it to the domain.
        urlText = "#{urlText}#{path}"

      # Link should be 2 shades lighter than the text.
      linkColor = line.character.colorObject 2

      "<a href='#{url}' target='_blank' style='color:##{linkColor.getHexString()};'>#{urlText}</a>"

    formattedText

  events: ->
    super.concat
      'submit .new-line-form': @submitNewLineForm

  submitNewLineForm: (event) ->
    event.preventDefault()

    conversation = @data()
    text = @$('.new-line-text').val()

    Meteor.call 'LandsOfIllusions.Conversations.Line.insert', conversation._id, LOI.characterId(), text, (error) =>
      if error
        console.error error
        return

      # Clear the entry form.
      @$('.new-line-text').val('')
<commit_msg>Replace all the links, not just the first one.<commit_after>AM = Artificial.Mirage
LOI = LandsOfIllusions
PAA = PixelArtAcademy

class PAA.PixelBoy.Apps.Components.Conversation extends AM.Component
  @register "PixelArtAcademy.PixelBoy.Apps.Components.Conversation"

  onCreated: ->
    super

    conversation = @data()
    @subscribe 'LandsOfIllusions.Conversations.Line.linesForConversation', conversation._id

  lines: ->
    conversation = @data()

    LOI.Conversations.Line.documents.find
      'conversation._id': conversation._id
    ,
      sort:
        time: 1

  textStyle: ->
    line = @currentData()

    # Set the color to character's color.
    color: "##{line.character.colorObject()?.getHexString()}"

  formattedText: ->
    line = @currentData()
    text = line.text

    # DANGER ZONE:
    # We are using direct HTML injection with triple braces, i.e. {{{formattedText}}}, so make sure we escape properly.
    div = document.createElement 'div'
    div.appendChild document.createTextNode text
    escapedText = div.innerHTML
    
    # Replace urls with links.
    urlRegex = /(https?):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/g

    formattedText = escapedText.replace urlRegex, (url, protocol, domain, path) =>
      urlText = domain

      if path
        # Make sure the path is not longer than 10 characters.
        path = "/…#{path.substring(path.length-8)}" if path.length > 10

        # Add it to the domain.
        urlText = "#{urlText}#{path}"

      # Link should be 2 shades lighter than the text.
      linkColor = line.character.colorObject 2

      "<a href='#{url}' target='_blank' style='color:##{linkColor.getHexString()};'>#{urlText}</a>"

    formattedText

  events: ->
    super.concat
      'submit .new-line-form': @submitNewLineForm

  submitNewLineForm: (event) ->
    event.preventDefault()

    conversation = @data()
    text = @$('.new-line-text').val()

    Meteor.call 'LandsOfIllusions.Conversations.Line.insert', conversation._id, LOI.characterId(), text, (error) =>
      if error
        console.error error
        return

      # Clear the entry form.
      @$('.new-line-text').val('')

{'fraction_non_alphanumeric': 0.15196304849884526, 'fraction_numerical': 0.003695150115473441, 'mean_word_length': 2.9301270417422867, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, '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': '6889377', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1358, 'n_words': 318}