<commit_before>class Bot::Slack < ActiveRecord::Base

  check_settings

  def self.default
    Bot::Slack.where(name: 'Slack Bot').last
  end

  def should_notify?(team, model)
    team.present? && !model.skip_notifications && team.setting(:slack_notifications_enabled).to_i === 1
  end

  def should_notify_super_admin?(model)
    should_notify_annotation?(model) && !model.skip_notifications && self.setting(:slack_notifications_enabled).to_i === 1
  end

  def should_notify_annotation?(model)
    (model.is_annotation? && model.annotated_type != 'ProjectMedia') ? false : true
  end

  def notify_slack(model)
    # Get team & project
    p = self.get_project(model)
    t = self.get_team(model, p)

    if self.should_notify?(t, model)
      webhook = t.setting(:slack_webhook)
      channel = p.setting(:slack_channel) unless p.nil?
      channel ||= t.setting(:slack_channel)
      message = model.slack_notification_message if model.respond_to?(:slack_notification_message)
      self.bot_send_slack_notification(model, webhook, channel, message)
    end
    self.notify_super_admin(model, t, p)
  end

  def notify_super_admin(model, team, project)
    if self.should_notify_super_admin?(model)
      webhook = self.setting(:slack_webhook)
      channel = self.setting(:slack_channel)
      message = model.slack_notification_message if model.respond_to?(:slack_notification_message)
      unless message.blank?
        prefix = team.name
        prefix += ": #{project.title}" unless project.nil?
        message  = "[#{prefix}] - #{message}"
      end
      self.bot_send_slack_notification(model, webhook, channel, message)
    end
  end

  def bot_send_slack_notification(model, webhook, channel, message)
    return if webhook.blank? || channel.blank? || message.blank?

      data = {
        payload: {
          channel: channel,
          text: message.gsub('\\n', "\n")
        }.to_json
      }

      Rails.env === 'test' ? self.request_slack(model, webhook, data) : SlackNotificationWorker.perform_async(webhook, YAML::dump(data), YAML::dump(User.current))
  end

  def request_slack(model, webhook, data)
    self.request(webhook, data)
    model.sent_to_slack = true
  end

  def request(webhook, data)
    url = URI.parse(webhook)
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    request = Net::HTTP::Post.new(url.request_uri)
    request.set_form_data(data)
    http.request(request)
  end
  class << self
    def to_slack(text)
      # https://api.slack.com/docs/message-formatting#how_to_escape_characters
      { '&' => '&amp;', '<' => '&lt;', '>' => '&gt;' }.each { |k,v|
        text = text.gsub(k,v)
      }
      text.truncate(140)
    end

    def to_slack_url(url, text)
      url.insert(0, "#{CONFIG['checkdesk_client']}/") unless url.start_with? "#{CONFIG['checkdesk_client']}/"
      text = self.to_slack(text)
      text = text.tr("\n", ' ')
      "<#{url}|#{text}>"
    end<commit_msg>Truncate Slack messages, part 2 #6733<commit_after>class Bot::Slack < ActiveRecord::Base

  check_settings

  def self.default
    Bot::Slack.where(name: 'Slack Bot').last
  end

  def should_notify?(team, model)
    team.present? && !model.skip_notifications && team.setting(:slack_notifications_enabled).to_i === 1
  end

  def should_notify_super_admin?(model)
    should_notify_annotation?(model) && !model.skip_notifications && self.setting(:slack_notifications_enabled).to_i === 1
  end

  def should_notify_annotation?(model)
    (model.is_annotation? && model.annotated_type != 'ProjectMedia') ? false : true
  end

  def notify_slack(model)
    # Get team & project
    p = self.get_project(model)
    t = self.get_team(model, p)

    if self.should_notify?(t, model)
      webhook = t.setting(:slack_webhook)
      channel = p.setting(:slack_channel) unless p.nil?
      channel ||= t.setting(:slack_channel)
      message = model.slack_notification_message if model.respond_to?(:slack_notification_message)
      self.bot_send_slack_notification(model, webhook, channel, message)
    end
    self.notify_super_admin(model, t, p)
  end

  def notify_super_admin(model, team, project)
    if self.should_notify_super_admin?(model)
      webhook = self.setting(:slack_webhook)
      channel = self.setting(:slack_channel)
      message = model.slack_notification_message if model.respond_to?(:slack_notification_message)
      unless message.blank?
        prefix = team.name
        prefix += ": #{project.title}" unless project.nil?
        message  = "[#{prefix}] - #{message}"
      end
      self.bot_send_slack_notification(model, webhook, channel, message)
    end
  end

  def bot_send_slack_notification(model, webhook, channel, message)
    return if webhook.blank? || channel.blank? || message.blank?

      data = {
        payload: {
          channel: channel,
          text: message.gsub('\\n', "\n")
        }.to_json
      }

      Rails.env === 'test' ? self.request_slack(model, webhook, data) : SlackNotificationWorker.perform_async(webhook, YAML::dump(data), YAML::dump(User.current))
  end

  def request_slack(model, webhook, data)
    self.request(webhook, data)
    model.sent_to_slack = true
  end

  def request(webhook, data)
    url = URI.parse(webhook)
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    request = Net::HTTP::Post.new(url.request_uri)
    request.set_form_data(data)
    http.request(request)
  end
  class << self
    def to_slack(text)
      # https://api.slack.com/docs/message-formatting#how_to_escape_characters
      { '&' => '&amp;', '<' => '&lt;', '>' => '&gt;' }.each { |k,v|
        text = text.gsub(k,v)
      }
      text.nil? ? nil : text.truncate(140)
    end

    def to_slack_url(url, text)
      url.insert(0, "#{CONFIG['checkdesk_client']}/") unless url.start_with? "#{CONFIG['checkdesk_client']}/"
      text = self.to_slack(text)
      text = text.tr("\n", ' ')
      "<#{url}|#{text}>"
    end
{'fraction_non_alphanumeric': 0.1377104377104377, 'fraction_numerical': 0.002861952861952862, 'mean_word_length': 3.4435302916978308, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 13, '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': '1514581', 'n_tokens_mistral': 2032, 'n_tokens_neox': 2004, 'n_words': 362}