Class: Decidim::EmailNotificationGenerator

Inherits:
Object
  • Object
show all
Defined in:
decidim-core/app/services/decidim/email_notification_generator.rb

Overview

This class handles system events affecting resources and generates a notification for each recipient by scheduling a new `Decidim::NotificationMailer` job for each of them. This way we can easily control which jobs fail and retry them, so that we don't have duplicated notifications.

Instance Method Summary collapse

Constructor Details

#initialize(event, event_class, resource, followers, affected_users, extra) ⇒ EmailNotificationGenerator

Initializes the class.

event - A String with the name of the event. event_class - A class that wraps the event. resource - an instance of a class implementing the `Decidim::Resource` concern. followers - a collection of Users that receive the notification because

they're following it

affected_users - a collection of Users that receive the notification because

they're affected by it

extra - a Hash with extra information to be included in the notification. rubocop:disable Metrics/ParameterLists


21
22
23
24
25
26
27
28
# File 'decidim-core/app/services/decidim/email_notification_generator.rb', line 21

def initialize(event, event_class, resource, followers, affected_users, extra)
  @event = event
  @event_class = event_class
  @resource = resource
  @followers = followers
  @affected_users = affected_users
  @extra = extra
end

Instance Method Details

#generateObject

Schedules a job for each recipient to send the email. Returns `nil` if the resource is not resource or if it is not present.

Returns nothing.


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'decidim-core/app/services/decidim/email_notification_generator.rb', line 35

def generate
  return unless resource
  return unless event_class.types.include?(:email)

  followers.each do |recipient|
    next unless ["all", "followed-only"].include?(recipient.notification_types)

    send_email_to(recipient, user_role: :follower)
  end

  affected_users.each do |recipient|
    next unless ["all", "own-only"].include?(recipient.notification_types)

    send_email_to(recipient, user_role: :affected_user)
  end
end