Commit 9b0efdb7 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Remove code duplication in notification_service.rb

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 616675b4
...@@ -113,7 +113,7 @@ class NotificationService ...@@ -113,7 +113,7 @@ class NotificationService
end end
# Add all users participating in the thread (author, assignee, comment authors) # Add all users participating in the thread (author, assignee, comment authors)
participants = participants =
if target.respond_to?(:participants) if target.respond_to?(:participants)
target.participants(note.author) target.participants(note.author)
else else
...@@ -276,35 +276,25 @@ class NotificationService ...@@ -276,35 +276,25 @@ class NotificationService
# Remove users with disabled notifications from array # Remove users with disabled notifications from array
# Also remove duplications and nil recipients # Also remove duplications and nil recipients
def reject_muted_users(users, project = nil) def reject_muted_users(users, project = nil)
users = users.to_a.compact.uniq reject_users(users, :disabled?, project)
users = users.reject(&:blocked?)
users.reject do |user|
next user.notification.disabled? unless project
member = project.project_members.find_by(user_id: user.id)
if !member && project.group
member = project.group.group_members.find_by(user_id: user.id)
end
# reject users who globally disabled notification and has no membership
next user.notification.disabled? unless member
# reject users who disabled notification in project
next true if member.notification.disabled?
# reject users who have N_GLOBAL in project and disabled in global settings
member.notification.global? && user.notification.disabled?
end
end end
# Remove users with notification level 'Mentioned' # Remove users with notification level 'Mentioned'
def reject_mention_users(users, project = nil) def reject_mention_users(users, project = nil)
reject_users(users, :mention?, project)
end
# Reject users which method_name from notification object returns true.
#
# Example:
# reject_users(users, :watch?, project)
#
def reject_users(users, method_name, project = nil)
users = users.to_a.compact.uniq users = users.to_a.compact.uniq
users = users.reject(&:blocked?)
users.reject do |user| users.reject do |user|
next user.notification.mention? unless project next user.notification.send(method_name) unless project
member = project.project_members.find_by(user_id: user.id) member = project.project_members.find_by(user_id: user.id)
...@@ -313,19 +303,19 @@ class NotificationService ...@@ -313,19 +303,19 @@ class NotificationService
end end
# reject users who globally set mention notification and has no membership # reject users who globally set mention notification and has no membership
next user.notification.mention? unless member next user.notification.send(method_name) unless member
# reject users who set mention notification in project # reject users who set mention notification in project
next true if member.notification.mention? next true if member.notification.send(method_name)
# reject users who have N_MENTION in project and disabled in global settings # reject users who have N_MENTION in project and disabled in global settings
member.notification.global? && user.notification.mention? member.notification.global? && user.notification.send(method_name)
end end
end end
def reject_unsubscribed_users(recipients, target) def reject_unsubscribed_users(recipients, target)
return recipients unless target.respond_to? :subscriptions return recipients unless target.respond_to? :subscriptions
recipients.reject do |user| recipients.reject do |user|
subscription = target.subscriptions.find_by_user_id(user.id) subscription = target.subscriptions.find_by_user_id(user.id)
subscription && !subscription.subscribed subscription && !subscription.subscribed
...@@ -343,7 +333,7 @@ class NotificationService ...@@ -343,7 +333,7 @@ class NotificationService
recipients recipients
end end
end end
def new_resource_email(target, project, method) def new_resource_email(target, project, method)
recipients = build_recipients(target, project, target.author) recipients = build_recipients(target, project, target.author)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment