Skip to content

Commit

Permalink
Create notify? report policy
Browse files Browse the repository at this point in the history
Right now the authorization logic is spread over several places.
Lets move it to a policy to avoid duplications and confusion.
The policy is meant to decide if a user should be notified about
a created report or not.
  • Loading branch information
krauselukas authored and danidoni committed Oct 2, 2023
1 parent bdf2dec commit 9a58e67
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def update
notifications: paginated_notifications,
selected_filter: selected_filter,
show_read_all_button: show_read_all_button?,
show_reports: Flipper.enabled?(:content_moderation, User.session) && User.session.is_moderator?
show_reports: ReportPolicy.new(User.session, Report).notify?
}
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/api/app/jobs/send_event_emails_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def send_email(subscribers, event)

def event_subscribers(event:)
if event.is_a?(Event::CreateReport)
event.subscribers.filter_map { |subscriber| subscriber if Flipper.enabled?(:content_moderation, subscriber) && subscriber.is_moderator? }
event.subscribers.filter_map { |subscriber| subscriber if ReportPolicy.new(subscriber, Report).notify? }
else
event.subscribers
end
Expand Down
5 changes: 3 additions & 2 deletions src/api/app/models/event_subscription/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ def find_or_initialize_subscription(eventtype, receiver_role, channel)

def show_form_for_create_report_event?(event_class:, subscriber:)
if event_class.name == 'Event::CreateReport'
return false unless subscriber.try(:is_moderator?)
return false unless Flipper.enabled?(:content_moderation, subscriber)
# There is no subscriber for the global subscription configuration
return false if subscriber.blank?
return false unless ReportPolicy.new(subscriber, Report).notify?
end

true
Expand Down
8 changes: 8 additions & 0 deletions src/api/app/policies/report_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ def create?
!UserPolicy.new(user, record.reportable).update?
end
end

def notify?
return false unless Flipper.enabled?(:content_moderation, user)
return true if User.moderators.blank? && (user.is_admin? || user.is_staff?)
return true if user.is_moderator?

false
end
end
15 changes: 6 additions & 9 deletions src/api/app/services/notification_service/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def create_notification?(subscriber, channel)
true
end

def create_report_notification?(event:, subscriber:)
return false if event.is_a?(Event::CreateReport) && !ReportPolicy.new(subscriber, Report).notify?

true
end

def notifiable_exists?
# We need this check because the notification is created in a delayed job.
# So the notifiable object could have been removed in the meantime.
Expand All @@ -70,14 +76,5 @@ def notifiable_exists?
notifiable_id = @event.parameters_for_notification[:notifiable_id]
notifiable_type.exists?(notifiable_id)
end

def create_report_notification?(event:, subscriber:)
if event.is_a?(Event::CreateReport)
return false unless Flipper.enabled?(:content_moderation, subscriber)
return false unless subscriber.is_moderator?
end

true
end
end
end

0 comments on commit 9a58e67

Please sign in to comment.