Skip to content

Commit

Permalink
[api][webui] Move EventFindSubscriptions into a sub module.
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/api/app/models/event/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def custom_headers
end

def subscriptions
EventFindSubscriptions.new(self).subscriptions
EventSubscription::FindForEvent.new(self).subscriptions
end

def subscribers
Expand Down
64 changes: 0 additions & 64 deletions src/api/app/models/event_find_subscriptions.rb

This file was deleted.

72 changes: 72 additions & 0 deletions src/api/app/models/event_subscription/find_for_event.rb
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,42 @@
end
end
end

context 'with a maintainer user/group who has a maintainer subscription and default subscription' do
let!(:project) { create(:project, maintainer: [maintainer]) }
let!(:comment) { create(:comment_project, commentable: project) }

let!(:default_subscription) do
create(:event_subscription_comment_for_project, receiver_role: 'maintainer', user: nil, group: nil)
end

context 'and the maintainer subscription is enabled' do
let!(:subscription) { create(:event_subscription_comment_for_project_without_subscriber, receiver_role: 'maintainer', subscriber: maintainer) }

it 'returns the subscription for that user/group' do
subscriber_result = subject.find { |subscription| subscription.subscriber == maintainer }

expect(subscriber_result).to eq(subscription)
end
end

context 'and the maintainer subscription is disabled' do
let!(:subscription) do
create(:event_subscription_comment_for_project_without_subscriber, receiver_role: 'maintainer', subscriber: maintainer, channel: 'disabled')
end

it 'does not include that user/group' do
expect(subject.map(&:subscriber)).not_to include(maintainer)
end
end
end
end

RSpec.describe EventFindSubscriptions do
RSpec.describe EventSubscription::FindForEvent do
describe '#subscribers' do
subject do
event = Event::CommentForProject.first
EventFindSubscriptions.new(event).subscriptions
EventSubscription::FindForEvent.new(event).subscriptions
end

context 'with a comment for a project' do
Expand Down
7 changes: 4 additions & 3 deletions src/api/test/models/event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ def setup
end

def users_for_event(e)
users = EventFindSubscriptions.new(e).subscriptions.map(&:subscriber)
users = EventSubscription::FindForEvent.new(e).subscriptions.map(&:subscriber)
User.where(id: users).pluck(:login).sort
end

def groups_for_event(e)
groups = EventFindSubscriptions.new(e).subscriptions.map(&:subscriber)
groups = EventSubscription::FindForEvent.new(e).subscriptions.map(&:subscriber)
Group.where(id: groups).pluck(:title).sort
end

Expand Down Expand Up @@ -162,7 +162,8 @@ def groups_for_event(e)

assert_equal "Request #{myid} requires review (submit Apache/BranchPack)", email.subject
# fred is maintainer of the package, hidden_homer of the project, Iggy triggers the event, so doesn't get email
assert_equal %w(fred@feuerstein.de homer@nospam.net), email.to.sort
# adrian is a member of test_group_b which is a maintainer of the package
assert_equal %w(adrian@example.com fred@feuerstein.de homer@nospam.net), email.to.sort

# now verify another review sends other emails
ActionMailer::Base.deliveries.clear
Expand Down

0 comments on commit ff2a36b

Please sign in to comment.