Sha256: 14fc7147699fc1f0009f7bcefa126b3eba23867bf07df51cf0448f0e4243d6b0

Contents?: true

Size: 1.81 KB

Versions: 12

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # A command with all the business logic when a user hides a resource.
    class HideResource < Decidim::Command
      # Public: Initializes the command.
      #
      # reportable - A Decidim::Reportable
      # current_user - the user that performs the action
      def initialize(reportable, current_user)
        @reportable = reportable
        @current_user = current_user
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid, together with the resource.
      # - :invalid if the resource is already hidden
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) unless hideable?

        hide!

        send_hide_notification_to_author

        broadcast(:ok, @reportable)
      end

      private

      def hideable?
        !@reportable.hidden? && @reportable.reported?
      end

      def hide!
        Decidim.traceability.perform_action!(
          "hide",
          @reportable.moderation,
          @current_user,
          extra: {
            reportable_type: @reportable.class.name
          }
        ) do
          @reportable.moderation.update!(hidden_at: Time.current)
          @reportable.try(:touch)
        end
      end

      def send_hide_notification_to_author
        data = {
          event: "decidim.events.reports.resource_hidden",
          event_class: Decidim::ResourceHiddenEvent,
          resource: @reportable,
          extra: {
            report_reasons: report_reasons
          },
          affected_users: @reportable.try(:authors) || [@reportable.try(:normalized_author)]
        }

        Decidim::EventsManager.publish(**data)
      end

      def report_reasons
        @reportable.moderation.reports.pluck(:reason).uniq
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

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