Class: HelpCenter::SupportThreadNotificationJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/help_center/support_thread_notification_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(support_thread) ⇒ Object



4
5
6
7
# File 'app/jobs/help_center/support_thread_notification_job.rb', line 4

def perform(support_thread)
  send_emails(support_thread) if HelpCenter.send_email_notifications
  send_webhook(support_thread) if HelpCenter.send_slack_notifications
end

#send_emails(support_thread) ⇒ Object



9
10
11
12
13
# File 'app/jobs/help_center/support_thread_notification_job.rb', line 9

def send_emails(support_thread)
  support_thread.notify_users.each do |user|
    HelpCenter::UserMailer.new_thread(support_thread, user).deliver_later
  end
end

#send_webhook(support_thread) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/jobs/help_center/support_thread_notification_job.rb', line 15

def send_webhook(support_thread)
  slack_webhook_url = Rails.application.secrets.simple_discussion_slack_url
  return if slack_webhook_url.blank?

  support_post = support_thread.support_posts.first
  payload = {
    fallback: "A new discussion was started: <#{support_thread_url(support_thread, anchor: ActionView::RecordIdentifier.dom_id(support_posts))}|#{support_thread.title}>",
    pretext: "A new discussion was started: <#{support_thread_url(support_thread, anchor: ActionView::RecordIdentifier.dom_id(support_posts))}|#{support_thread.title}>",
    fields: [
      {
        title: "Thread",
        value: support_thread.title,
        short: true
      },
      {
        title: "Posted By",
        value: support_post.user.name,
        short: true,
      },
    ],
    ts: support_post.created_at.to_i
  }

  HelpCenter::Slack.new(slack_webhook_url).post(payload)
end