Sha256: 5a66c808d51f4fc1b9ef72b6a5d47ec9eb773cddc056241643e0668048a24fe8

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true
module Thredded
  class NotifyPrivateTopicUsers
    def initialize(private_topic)
      @post = private_topic.posts.first || Post.new
      @private_topic = private_topic
    end

    def run
      members = private_topic_recipients

      return unless members.present?
      user_emails = members.map(&:email)
      PrivateTopicMailer
        .message_notification(private_topic.id, user_emails)
        .deliver_later
      mark_notified(members)
    end

    def private_topic_recipients
      members = private_topic.users - [private_topic.user]
      members = exclude_those_opting_out_of_message_notifications(members)
      members = exclude_previously_notified(members)
      members
    end

    private

    attr_reader :post, :private_topic

    def mark_notified(members)
      members.each do |member|
        post.post_notifications.create(email: member.email)
      end
    end

    def exclude_those_opting_out_of_message_notifications(members)
      members.select { |member| member.thredded_user_preference.notify_on_message? }
    end

    def exclude_previously_notified(members)
      emails_notified = post.post_notifications.map(&:email)

      members.reject do |member|
        emails_notified.include? member.email
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
thredded-0.3.1 app/commands/thredded/notify_private_topic_users.rb
thredded-0.3.0 app/commands/thredded/notify_private_topic_users.rb