Sha256: 919a0dcd1031384c89c37909030fc85294a0b9fdc7157b348221d318b1e42692

Contents?: true

Size: 1.65 KB

Versions: 9

Compression:

Stored size: 1.65 KB

Contents

require 'thredded/at_notification_extractor'

module Thredded
  class AtNotifier
    def initialize(post)
      @post = post
    end

    def notifications_for_at_users
      members = at_notifiable_members

      if members.present?
        user_emails = members.map(&:email)
        Thredded::PostMailer.at_notification(post.id, user_emails).deliver
        mark_notified(members)
      end
    end

    def at_notifiable_members
      at_names = Thredded::AtNotificationExtractor.new(post.content).extract
      members = post.messageboard.members_from_list(at_names).to_a

      members.delete post.user
      members = exclude_previously_notified(members)
      members = exclude_those_that_are_not_private(members)
      members = exclude_those_opting_out_of_at_notifications(members)

      members
    end

    private

    attr_reader :post

    def exclude_those_opting_out_of_at_notifications(members)
      members.reject do |member|
        !Thredded::MessageboardPreference
          .for(member)
          .in(post.messageboard)
          .first
          .try(:notify_on_mention?)
      end
    end

    def exclude_those_that_are_not_private(members)
      members.reject do |member|
        post.topic.private? && post.topic.users.exclude?(member)
      end
    end

    def exclude_previously_notified(members)
      emails_notified = Thredded::PostNotification
        .where(post_id: post.id)
        .map(&:email)

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

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
thredded-0.0.12 lib/thredded/at_notifier.rb
thredded-0.0.10 lib/thredded/at_notifier.rb
thredded-0.0.9 lib/thredded/at_notifier.rb
thredded-0.0.8 lib/thredded/at_notifier.rb
thredded-0.0.7 lib/thredded/at_notifier.rb
thredded-0.0.6 lib/thredded/at_notifier.rb
thredded-0.0.5 lib/thredded/at_notifier.rb
thredded-0.0.4 lib/thredded/at_notifier.rb
thredded-0.0.3 lib/thredded/at_notifier.rb