summaryrefslogtreecommitdiff
path: root/app/services/notification_service.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/services/notification_service.rb')
-rw-r--r--app/services/notification_service.rb75
1 files changed, 42 insertions, 33 deletions
diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb
index 274161df946..55a1735e54b 100644
--- a/app/services/notification_service.rb
+++ b/app/services/notification_service.rb
@@ -7,7 +7,32 @@
# Ex.
# NotificationService.new.new_issue(issue, current_user)
#
+# When calculating the recipients of a notification is expensive (for instance,
+# in the new issue case), `#async` will make that calculation happen in Sidekiq
+# instead:
+#
+# NotificationService.new.async.new_issue(issue, current_user)
+#
class NotificationService
+ class Async
+ attr_reader :parent
+ delegate :respond_to_missing, to: :parent
+
+ def initialize(parent)
+ @parent = parent
+ end
+
+ def method_missing(meth, *args)
+ return super unless parent.respond_to?(meth)
+
+ MailScheduler::NotificationServiceWorker.perform_async(meth.to_s, *args)
+ end
+ end
+
+ def async
+ @async ||= Async.new(self)
+ end
+
# Always notify user about ssh key added
# only if ssh key is not deploy key
#
@@ -142,8 +167,23 @@ class NotificationService
# * merge_request assignee if their notification level is not Disabled
# * users with custom level checked with "reassign merge request"
#
- def reassigned_merge_request(merge_request, current_user)
- reassign_resource_email(merge_request, current_user, :reassigned_merge_request_email)
+ def reassigned_merge_request(merge_request, current_user, previous_assignee)
+ recipients = NotificationRecipientService.build_recipients(
+ merge_request,
+ current_user,
+ action: "reassign",
+ previous_assignee: previous_assignee
+ )
+
+ recipients.each do |recipient|
+ mailer.reassigned_merge_request_email(
+ recipient.user.id,
+ merge_request.id,
+ previous_assignee&.id,
+ current_user.id,
+ recipient.reason
+ ).deliver_later
+ end
end
# When we add labels to a merge request we should send an email to:
@@ -421,29 +461,6 @@ class NotificationService
end
end
- def reassign_resource_email(target, current_user, method)
- previous_assignee_id = previous_record(target, 'assignee_id')
- previous_assignee = User.find_by(id: previous_assignee_id) if previous_assignee_id
-
- recipients = NotificationRecipientService.build_recipients(
- target,
- current_user,
- action: "reassign",
- previous_assignee: previous_assignee
- )
-
- recipients.each do |recipient|
- mailer.send(
- method,
- recipient.user.id,
- target.id,
- previous_assignee_id,
- current_user.id,
- recipient.reason
- ).deliver_later
- end
- end
-
def relabeled_resource_email(target, labels, current_user, method)
recipients = labels.flat_map { |l| l.subscribers(target.project) }.uniq
recipients = notifiable_users(
@@ -471,14 +488,6 @@ class NotificationService
Notify
end
- def previous_record(object, attribute)
- return unless object && attribute
-
- if object.previous_changes.include?(attribute)
- object.previous_changes[attribute].first
- end
- end
-
private
def recipients_for_pages_domain(domain)