Sha256: 8efac4bbaf7bc5c1d56e25101eb20d19968515750917696b4071efbdcd80d7cd

Contents?: true

Size: 1.64 KB

Versions: 25

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module Decidim
  # A command with all the business logic when a user creates a report.
  class CreateUserReport < Rectify::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_organization.admins.each do |admin|
        Decidim::UserReportJob.perform_later(admin, current_user, form.reason, reportable)
      end
    end

    def hideable?
      false
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-core-0.26.10 app/commands/decidim/create_user_report.rb
decidim-core-0.26.9 app/commands/decidim/create_user_report.rb
decidim-core-0.26.8 app/commands/decidim/create_user_report.rb
decidim-core-0.26.7 app/commands/decidim/create_user_report.rb
decidim-core-0.26.5 app/commands/decidim/create_user_report.rb
decidim-core-0.26.4 app/commands/decidim/create_user_report.rb
decidim-core-0.26.3 app/commands/decidim/create_user_report.rb
decidim-core-0.26.2 app/commands/decidim/create_user_report.rb
decidim-core-0.26.1 app/commands/decidim/create_user_report.rb
decidim-core-0.26.0 app/commands/decidim/create_user_report.rb
decidim-core-0.26.0.rc2 app/commands/decidim/create_user_report.rb
decidim-core-0.26.0.rc1 app/commands/decidim/create_user_report.rb
decidim-core-0.25.2 app/commands/decidim/create_user_report.rb
decidim-core-0.25.1 app/commands/decidim/create_user_report.rb
decidim-core-0.25.0 app/commands/decidim/create_user_report.rb
decidim-core-0.25.0.rc4 app/commands/decidim/create_user_report.rb
decidim-core-0.25.0.rc3 app/commands/decidim/create_user_report.rb
decidim-core-0.25.0.rc2 app/commands/decidim/create_user_report.rb
decidim-core-0.25.0.rc1 app/commands/decidim/create_user_report.rb
decidim-core-0.24.3 app/commands/decidim/create_user_report.rb