Sha256: fb61cc6cfea125f65c744cdfb7188d2bd66cd3c529fe4770c5eda2f2a8beb534

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

class SimpleDiscussion::ForumThreadNotificationJob < ApplicationJob
  include SimpleDiscussion::Engine.routes.url_helpers

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

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

  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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_discussion-1.3.0 app/jobs/simple_discussion/forum_thread_notification_job.rb