<commit_before>module DiscourseChat::Provider::SlackProvider
  PROVIDER_NAME = "slack".freeze

  PROVIDER_ENABLED_SETTING = :chat_integration_slack_enabled

  CHANNEL_PARAMETERS = [
                        { key: "identifier", regex: '^[@#]\S*$', unique: true }
                       ]

  def self.excerpt(post, max_length = SiteSetting.chat_integration_slack_excerpt_length)
    doc = Nokogiri::HTML.fragment(post.excerpt(max_length,
      remap_emoji: true,
      keep_onebox_source: true
    ))

    SlackMessageFormatter.format(doc.to_html)
  end

  def self.slack_message(post, channel)
    display_name = "@#{post.user.username}"
    full_name = post.user.name || ""

    if !(full_name.strip.empty?) && (full_name.strip.gsub(' ', '_').casecmp(post.user.username) != 0) && (full_name.strip.gsub(' ', '').casecmp(post.user.username) != 0)
      display_name = "#{full_name} @#{post.user.username}"
    end

    topic = post.topic

    category = ''
    if topic.category
      category = (topic.category.parent_category) ? "[#{topic.category.parent_category.name}/#{topic.category.name}]" : "[#{topic.category.name}]"
    end

    icon_url =
      if !SiteSetting.chat_integration_slack_icon_url.blank?
        "#{Discourse.base_url}#{SiteSetting.chat_integration_slack_icon_url}"
      elsif !SiteSetting.logo_small_url.blank?
        "#{Discourse.base_url}#{SiteSetting.logo_small_url}"
      end

    message = {
      channel: channel,
      username: SiteSetting.title || "Discourse",
      icon_url: icon_url,
      attachments: []
    }

    summary = {
      fallback: "#{topic.title} - #{display_name}",
      author_name: display_name,
      author_icon: post.user.small_avatar_url,
      color: topic.category ? "##{topic.category.color}" : nil,
      text: excerpt(post),
      mrkdwn_in: ["text"],
      title: "#{topic.title} #{(category == '[uncategorized]') ? '' : category} #{topic.tags.present? ? topic.tags.map(&:name).join(', ') : ''}",
      title_link: post.full_url,
      thumb_url: post.full_url
    }

    message[:attachments].push(summary)

    return message
  end

  def self.send_via_api(post, channel, message)
    http = Net::HTTP.new("slack.com", 443)
    http.use_ssl = true

    response = nil
    uri = ""<commit_msg>Remove incorrect thumb_url from mattermost<commit_after>module DiscourseChat::Provider::SlackProvider
  PROVIDER_NAME = "slack".freeze

  PROVIDER_ENABLED_SETTING = :chat_integration_slack_enabled

  CHANNEL_PARAMETERS = [
                        { key: "identifier", regex: '^[@#]\S*$', unique: true }
                       ]

  def self.excerpt(post, max_length = SiteSetting.chat_integration_slack_excerpt_length)
    doc = Nokogiri::HTML.fragment(post.excerpt(max_length,
      remap_emoji: true,
      keep_onebox_source: true
    ))

    SlackMessageFormatter.format(doc.to_html)
  end

  def self.slack_message(post, channel)
    display_name = "@#{post.user.username}"
    full_name = post.user.name || ""

    if !(full_name.strip.empty?) && (full_name.strip.gsub(' ', '_').casecmp(post.user.username) != 0) && (full_name.strip.gsub(' ', '').casecmp(post.user.username) != 0)
      display_name = "#{full_name} @#{post.user.username}"
    end

    topic = post.topic

    category = ''
    if topic.category
      category = (topic.category.parent_category) ? "[#{topic.category.parent_category.name}/#{topic.category.name}]" : "[#{topic.category.name}]"
    end

    icon_url =
      if !SiteSetting.chat_integration_slack_icon_url.blank?
        "#{Discourse.base_url}#{SiteSetting.chat_integration_slack_icon_url}"
      elsif !SiteSetting.logo_small_url.blank?
        "#{Discourse.base_url}#{SiteSetting.logo_small_url}"
      end

    message = {
      channel: channel,
      username: SiteSetting.title || "Discourse",
      icon_url: icon_url,
      attachments: []
    }

    summary = {
      fallback: "#{topic.title} - #{display_name}",
      author_name: display_name,
      author_icon: post.user.small_avatar_url,
      color: topic.category ? "##{topic.category.color}" : nil,
      text: excerpt(post),
      mrkdwn_in: ["text"],
      title: "#{topic.title} #{(category == '[uncategorized]') ? '' : category} #{topic.tags.present? ? topic.tags.map(&:name).join(', ') : ''}",
      title_link: post.full_url
    }

    message[:attachments].push(summary)

    return message
  end

  def self.send_via_api(post, channel, message)
    http = Net::HTTP.new("slack.com", 443)
    http.use_ssl = true

    response = nil
    uri = ""
{'fraction_non_alphanumeric': 0.15032100951959265, 'fraction_numerical': 0.002213858755811379, 'mean_word_length': 3.2382739212007503, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5995030', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1505, 'n_words': 230}