Class: Thredded::NotifyFollowingUsers

Inherits:
Object
  • Object
show all
Defined in:
app/commands/thredded/notify_following_users.rb

Instance Method Summary collapse

Constructor Details

#initialize(post) ⇒ NotifyFollowingUsers

Returns a new instance of NotifyFollowingUsers.


7
8
9
# File 'app/commands/thredded/notify_following_users.rb', line 7

def initialize(post)
  @post = post
end

Instance Method Details

#runObject


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/commands/thredded/notify_following_users.rb', line 11

def run
  subscribed_users.select! do |user|
    # Record idempotently that the notification happened.
    # If a notification was already created (e.g. from another thread/process),
    # this will return false due to the unique constraint on the table
    # and the user will be excluded.
    subscribed_via_any_notifier?(user) && record_as_notified_successful?(user)
  end
  Thredded.notifiers.each do |notifier|
    notifiable_users = targeted_users(notifier)
    next if notifiable_users.empty?
    notifier.new_post(@post, notifiable_users)
  end
end

#subscribed_usersArray<User>

Returns:

  • (Array<User>)

40
41
42
43
44
45
# File 'app/commands/thredded/notify_following_users.rb', line 40

def subscribed_users
  @subscribed_users ||=
    @post.postable.followers.includes(:thredded_notifications_for_followed_topics).select do |user|
      !already_notified?(user) && !originator?(user) && can_read_post?(user)
    end
end

#targeted_users(notifier) ⇒ Object


26
27
28
29
30
# File 'app/commands/thredded/notify_following_users.rb', line 26

def targeted_users(notifier)
  subscribed_users.select do |user|
    user_subscribed_via?(user, notifier)
  end
end

#user_subscribed_via?(user, notifier) ⇒ Boolean

Returns:

  • (Boolean)

32
33
34
35
36
37
# File 'app/commands/thredded/notify_following_users.rb', line 32

def user_subscribed_via?(user, notifier)
  Thredded::NotificationsForFollowedTopics
    .detect_or_default(user.thredded_notifications_for_followed_topics, notifier).enabled? &&
    Thredded::MessageboardNotificationsForFollowedTopics
      .detect_or_default(messageboard_notifier_prefs_by_user_id[user.id], notifier).enabled?
end