Class: SimpleDiscussion::ForumThreadNotificationJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/simple_discussion/forum_thread_notification_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(forum_thread) ⇒ Object



4
5
6
7
# File 'app/jobs/simple_discussion/forum_thread_notification_job.rb', line 4

def perform(forum_thread)
  send_emails(forum_thread) if SimpleDiscussion.send_email_notifications
  send_webhook(forum_thread) if SimpleDiscussion.send_slack_notifications
end

#send_emails(forum_thread) ⇒ Object



9
10
11
12
13
# File 'app/jobs/simple_discussion/forum_thread_notification_job.rb', line 9

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

#send_webhook(forum_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/simple_discussion/forum_thread_notification_job.rb', line 15

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

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

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