-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api][webui] Move EventFindSubscriptions into a sub module.
Move the class so its kept the other logic classes that relate to EventSubscription.
- Loading branch information
Evan Rolfe
committed
Oct 10, 2017
1 parent
4442f80
commit ff2a36b
Showing
5 changed files
with
108 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
class EventSubscription | ||
class FindForEvent | ||
attr_reader :event | ||
|
||
def initialize(event) | ||
@event = event | ||
end | ||
|
||
def subscriptions | ||
receivers_and_subscriptions = {} | ||
|
||
event.class.receiver_roles.flat_map do |receiver_role| | ||
# Find the users/groups who are receivers for this event | ||
receivers = event.send("#{receiver_role}s") | ||
receivers = filter_and_convert_groups_without_emails_to_users(receivers) | ||
|
||
# Find the default subscription for this eventtype and receiver_role | ||
default_subscription = EventSubscription.defaults.find_by(eventtype: event.eventtype, receiver_role: receiver_role) | ||
|
||
receivers.each do |receiver| | ||
# Prevent multiple enabled subscriptions for the same subscriber & eventtype | ||
# Also skip if the receiver is the originator of this event | ||
next if receivers_and_subscriptions[receiver].present? || receiver == event.originator | ||
|
||
# Try to find the subscription for this receiver | ||
receiver_subscription = EventSubscription.for_subscriber(receiver).find_by(eventtype: event.eventtype, receiver_role: receiver_role) | ||
|
||
if receiver_subscription.present? | ||
# Use the receiver's subscription if it exists | ||
if receiver_subscription.enabled? | ||
receivers_and_subscriptions[receiver] = receiver_subscription | ||
end | ||
|
||
# Only check the default_subscription if there is no receiver's subscription | ||
elsif default_subscription.present? && default_subscription.enabled? | ||
# Add a new subscription for the receiver based on the default subscription | ||
receivers_and_subscriptions[receiver] = EventSubscription.new( | ||
eventtype: default_subscription.eventtype, | ||
receiver_role: default_subscription.receiver_role, | ||
channel: default_subscription.channel, | ||
subscriber: receiver | ||
) | ||
end | ||
end | ||
end | ||
|
||
receivers_and_subscriptions.values.flatten | ||
end | ||
|
||
private | ||
|
||
def filter_and_convert_groups_without_emails_to_users(receivers) | ||
new_receivers = [] | ||
|
||
receivers.each do |receiver| | ||
if receiver.is_a? User | ||
new_receivers << receiver | ||
|
||
elsif receiver.is_a? Group | ||
|
||
if receiver.email.present? | ||
new_receivers << receiver | ||
else | ||
new_receivers += receiver.users | ||
end | ||
end | ||
end | ||
|
||
new_receivers | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters