Sha256: 5753c32489dbbad24064aba0707c09d80eb12ee1ae812b2c14566612f652f3e7

Contents?: true

Size: 1.92 KB

Versions: 34

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module Decidim
  # 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.
  class 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.
    # extra - a Hash with extra information to be included in the notification.
    def initialize(event, event_class, resource, recipient_ids, extra)
      @event = event
      @event_class = event_class
      @resource = resource
      @recipient_ids = recipient_ids
      @extra = extra
    end

    # 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.
    def generate
      return unless resource
      return unless event_class.types.include?(:email)

      recipient_ids.each do |recipient_id|
        send_email_to(recipient_id)
      end
    end

    private

    attr_reader :event, :event_class, :resource, :recipient_ids, :extra

    # Private: sends the notification email to the user if they have the
    # `email_on_notification` flag active.
    #
    # recipient_id - The ID of the user that will receive the email.
    #
    # Returns nothing.
    def send_email_to(recipient_id)
      recipient = Decidim::User.where(id: recipient_id).first
      return unless recipient
      return unless recipient.email_on_notification?

      NotificationMailer
        .event_received(
          event,
          event_class.name,
          resource,
          recipient,
          extra
        )
        .deliver_later
    end
  end
end

Version data entries

34 entries across 34 versions & 2 rubygems

Version Path
decidim-core-0.10.1 app/services/decidim/email_notification_generator.rb
decidim-core-0.10.0 app/services/decidim/email_notification_generator.rb
decidim-core-0.9.3 app/services/decidim/email_notification_generator.rb
decidim-core-0.9.2 app/services/decidim/email_notification_generator.rb
decidim-core-0.9.1 app/services/decidim/email_notification_generator.rb
decidim-core-0.9.0 app/services/decidim/email_notification_generator.rb
decidim-core-0.8.4 app/services/decidim/email_notification_generator.rb
decidim-core-0.8.3 app/services/decidim/email_notification_generator.rb
decidim-core-0.8.2 app/services/decidim/email_notification_generator.rb
decidim-core-0.8.1 app/services/decidim/email_notification_generator.rb
decidim-core-0.8.0 app/services/decidim/email_notification_generator.rb
decidim-core-0.7.4 app/services/decidim/email_notification_generator.rb
decidim-core-0.7.3 app/services/decidim/email_notification_generator.rb
decidim-core-0.7.2 app/services/decidim/email_notification_generator.rb
decidim-core-0.7.1 app/services/decidim/email_notification_generator.rb
decidim-core-0.7.0 app/services/decidim/email_notification_generator.rb
decidim-core-0.6.8 app/services/decidim/email_notification_generator.rb
decidim-0.6.8 decidim-core/app/services/decidim/email_notification_generator.rb
decidim-core-0.6.7 app/services/decidim/email_notification_generator.rb
decidim-0.6.7 decidim-core/app/services/decidim/email_notification_generator.rb