Sha256: cfea3964ebb35c4aba71300833c52a17a5570b9cd849ac3d0c5fa7c16caea5d1

Contents?: true

Size: 1.24 KB

Versions: 9

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true
module Thredded
  class NotifyPrivateTopicUsers
    def initialize(private_post)
      @post = private_post
      @private_topic = private_post.postable
    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 - [post.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

9 entries across 9 versions & 1 rubygems

Version Path
thredded-0.8.4 app/commands/thredded/notify_private_topic_users.rb
thredded-0.8.2 app/commands/thredded/notify_private_topic_users.rb
thredded-0.7.0 app/commands/thredded/notify_private_topic_users.rb
thredded-0.6.3 app/commands/thredded/notify_private_topic_users.rb
thredded-0.6.2 app/commands/thredded/notify_private_topic_users.rb
thredded-0.6.1 app/commands/thredded/notify_private_topic_users.rb
thredded-0.6.0 app/commands/thredded/notify_private_topic_users.rb
thredded-0.5.1 app/commands/thredded/notify_private_topic_users.rb
thredded-0.5.0 app/commands/thredded/notify_private_topic_users.rb