Sha256: 097f84fa0de75e03284c0f381abc0f584f11b50c435ab3657475a3f8568a8ea9

Contents?: true

Size: 1.66 KB

Versions: 12

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module Decidim
  # A command with all the business logic when a user creates a report.
  class CreateUserReport < Decidim::Command
    # Public: Initializes the command.
    #
    # form         - A form object with the params.
    # reportable   - The resource being reported
    # current_user - The current user.
    def initialize(form, reportable, current_user)
      @form = form
      @reportable = reportable
      @current_user = current_user
    end

    # Executes the command. Broadcasts these events:
    #
    # - :ok when everything is valid, together with the report.
    # - :invalid if the form wasn't valid and we couldn't proceed.
    #
    # Returns nothing.
    def call
      return broadcast(:invalid) if form.invalid?

      transaction do
        find_or_create_moderation!
        create_report!
        update_report_count!
        send_notification_to_admins!
      end

      broadcast(:ok, report)
    end

    private

    attr_reader :form, :report

    def find_or_create_moderation!
      @moderation = UserModeration.find_or_create_by!(user: @reportable)
    end

    def create_report!
      @report = UserReport.create!(
        moderation: @moderation,
        user: @current_user,
        reason: form.reason,
        details: form.details
      )
    end

    def update_report_count!
      @moderation.update!(report_count: @moderation.report_count + 1)
    end

    def send_notification_to_admins!
      @current_user.organization.admins.each do |admin|
        next unless admin.email_on_moderations

        Decidim::UserReportJob.perform_later(admin, report)
      end
    end

    def hideable?
      false
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-core-0.27.9 app/commands/decidim/create_user_report.rb
decidim-core-0.27.8 app/commands/decidim/create_user_report.rb
decidim-core-0.27.7 app/commands/decidim/create_user_report.rb
decidim-core-0.27.6 app/commands/decidim/create_user_report.rb
decidim-core-0.27.5 app/commands/decidim/create_user_report.rb
decidim-core-0.27.4 app/commands/decidim/create_user_report.rb
decidim-core-0.27.3 app/commands/decidim/create_user_report.rb
decidim-core-0.27.2 app/commands/decidim/create_user_report.rb
decidim-core-0.27.1 app/commands/decidim/create_user_report.rb
decidim-core-0.27.0 app/commands/decidim/create_user_report.rb
decidim-core-0.27.0.rc2 app/commands/decidim/create_user_report.rb
decidim-core-0.27.0.rc1 app/commands/decidim/create_user_report.rb