class NotificationServices::SlackService < NotificationService
  Label = "slack"
  Fields += [
    [:subdomain, {
      :placeholder => 'subdomain',
      :label => 'Subdomain portion for Slack service'
    }],
    [:api_token, {
      :placeholder => 'Slack Integration Token',
      :label => 'Token'
    }],
    [:room_id, {
      :placeholder => '#general',
      :label => 'Room where Slack should notify'
    }]
  ]

  def check_params
    if Fields.detect {|f| self[f[0]].blank? unless f[0] == :room_id }
      errors.add :base, "You must specify your Slack subdomain and token."
    end
  end

  def url
    "https://#{subdomain}.slack.com/services/hooks/incoming-webhook?token=#{api_token}"
  end

  def message_for_slack(problem)
    "*#{problem.app.name}:* #{problem.message.to_s} #{problem_url(problem)}"
  end

  def post_payload(problem)
    payload = {:text => message_for_slack(problem) }
    payload[:channel] = room_id unless room_id.empty?
    payload[:channel] = '#staging' if problem.environment == 'staging'
    payload.to_json
  end

  def create_notification(problem)
    HTTParty.post(url, :body => post_payload(problem), :headers => { 'Content-Type' => 'application/json' })
  end
end

class NotificationServices::SlackService < NotificationService
  Label = "slack"
  Fields += [
    [:subdomain, {
      :placeholder => 'subdomain',
      :label => 'Subdomain portion for Slack service'
    }],
    [:api_token, {
      :placeholder => 'Slack Integration Token',
      :label => 'Token'
    }],
    [:room_id, {
      :placeholder => '#general',
      :label => 'Room where Slack should notify'
    }]
  ]

  def check_params
    if Fields.detect {|f| self[f[0]].blank? unless f[0] == :room_id }
      errors.add :base, "You must specify your Slack subdomain and token."
    end
  end

  def url
    "https://#{subdomain}.slack.com/services/hooks/incoming-webhook?token=#{api_token}"
  end

  def message_for_slack(problem)
    "*#{problem.app.name}:* #{problem.message.to_s.truncate(100)} #{problem_url(problem)}"
  end

  def post_payload(problem)
    payload = {:text => message_for_slack(problem) }
    payload[:channel] = room_id unless room_id.empty?
    payload[:channel] = '#staging' if problem.environment == 'staging'
    payload.to_json
  end

  def create_notification(problem)
    HTTParty.post(url, :body => post_payload(problem), :headers => { 'Content-Type' => 'application/json' })
  end
end

Revert "Don't truncate Slack messages."
Revert "Don't truncate Slack messages."

This reverts commit 121125d7448c51745b11d409f71aac3e5a13f85e.
