diff --git a/app/controllers/activity_pubs_controller.rb b/app/controllers/activity_pubs_controller.rb index dfe388a4..057e65f7 100644 --- a/app/controllers/activity_pubs_controller.rb +++ b/app/controllers/activity_pubs_controller.rb @@ -2,16 +2,20 @@ # Gestiona acciones de moderación class ActivityPubsController < ApplicationController + include ModerationConcern + ActivityPub.events.each do |event| define_method(event) do + authorize activity_pub + activity_pub.public_send(:"#{event}!") if activity_pub.public_send(:"may_#{event}?") - redirect_back fallback_location: site_moderation_queue_path(**(session[:moderation_queue_filters] || {})) + redirect_to_moderation_queue! end end def action_on_several - + redirect_to_moderation_queue! end private diff --git a/app/controllers/actor_moderations_controller.rb b/app/controllers/actor_moderations_controller.rb index 907f21c6..eadc2165 100644 --- a/app/controllers/actor_moderations_controller.rb +++ b/app/controllers/actor_moderations_controller.rb @@ -2,6 +2,8 @@ # Gestiona la cola de moderación de actores class ActorModerationsController < ApplicationController + include ModerationConcern + ActorModeration.events.each do |actor_event| define_method(actor_event) do authorize actor_moderation @@ -11,7 +13,7 @@ class ActorModerationsController < ApplicationController actor_moderation.public_send(:"#{actor_event}!") if actor_moderation.public_send(:"may_#{actor_event}?") - redirect_back fallback_location: site_moderation_queue_path(**(session[:moderation_queue_filters] || {})) + redirect_to_moderation_queue! end end @@ -29,6 +31,8 @@ class ActorModerationsController < ApplicationController method = :"#{action}!" may = :"may_#{action}?" + redirect_to_moderation_queue! + return unless ActorModeration.events.include? action ActorModeration.transaction do diff --git a/app/controllers/concerns/moderation_concern.rb b/app/controllers/concerns/moderation_concern.rb new file mode 100644 index 00000000..9a4f1c16 --- /dev/null +++ b/app/controllers/concerns/moderation_concern.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module ModerationConcern + extend ActiveSupport::Concern + + included do + private + + def redirect_to_moderation_queue! + redirect_back fallback_location: site_moderation_queue_path(**(session[:moderation_queue_filters] || {})) + end + end +end diff --git a/app/controllers/instance_moderations_controller.rb b/app/controllers/instance_moderations_controller.rb index dc9e1dfa..270f0588 100644 --- a/app/controllers/instance_moderations_controller.rb +++ b/app/controllers/instance_moderations_controller.rb @@ -2,19 +2,16 @@ # Actualiza la relación entre un sitio y una instancia class InstanceModerationsController < ApplicationController - before_action :authorize_policy, except: %i[action_on_several] - around_action :redirect_to_moderation_queue! + include ModerationConcern - def pause - instance_moderation.pause! if instance_moderation.may_pause? - end + InstanceModeration.events.each do |event| + define_method(event) do + authorize instance_moderation - def allow - instance_moderation.allow! if instance_moderation.may_allow? - end + instance_moderation.public_send(:"#{event}!") if instance_moderation.public_send(:"may_#{event}?") - def block - instance_moderation.block! if instance_moderation.may_block? + redirect_to_moderation_queue! + end end def action_on_several @@ -25,9 +22,10 @@ class InstanceModerationsController < ApplicationController action = params[:instance_moderation_action].to_sym method = :"#{action}!" may = :"may_#{action}?" - events = instance_moderation.aasm.events.map(&:name) - return unless events.include? action + redirect_to_moderation_queue! + + return unless InstanceModeration.events.include? action InstanceModeration.transaction do instance_moderations.find_each do |instance_moderation| @@ -38,18 +36,8 @@ class InstanceModerationsController < ApplicationController private - def redirect_to_moderation_queue!(&action) - redirect_back fallback_location: site_moderation_queue_path, state: session[:moderation_queue_filtered_by_state] - - yield - end - # @return [InstanceModeration] def instance_moderation @instance_moderation ||= site.instance_moderations.find(params[:instance_moderation_id]) end - - def authorize_policy - authorize instance_moderation - end end diff --git a/app/controllers/moderation_queue_controller.rb b/app/controllers/moderation_queue_controller.rb index b803ccc9..6a628aaa 100644 --- a/app/controllers/moderation_queue_controller.rb +++ b/app/controllers/moderation_queue_controller.rb @@ -6,7 +6,7 @@ class ModerationQueueController < ApplicationController def index dummy_data - session[:moderation_queue_filters] = params.permit(:state, :actor_state) + session[:moderation_queue_filters] = params.permit(:state, :actor_state, :activity_pub_state) # @todo cambiar el estado por query @activity_pubs = site.activity_pubs diff --git a/app/policies/activity_pub_policy.rb b/app/policies/activity_pub_policy.rb new file mode 100644 index 00000000..f5755840 --- /dev/null +++ b/app/policies/activity_pub_policy.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# Solo les usuaries pueden moderar comentarios +ActivityPubPolicy = Struct.new(:usuarie, :activity_pub) do + ActivityPub.events.each do |event| + define_method(:"#{event}?") do + activity_pub.site.usuarie? usuarie + end + end + + # En este paso tenemos varias instancias por moderar pero todas son + # del mismo sitio. + def action_on_several? + activity_pub.first.site.usuarie? usuarie + end +end diff --git a/app/policies/instance_moderation_policy.rb b/app/policies/instance_moderation_policy.rb index c07455b3..13ebfeca 100644 --- a/app/policies/instance_moderation_policy.rb +++ b/app/policies/instance_moderation_policy.rb @@ -2,16 +2,10 @@ # Solo les usuaries pueden moderar instancias InstanceModerationPolicy = Struct.new(:usuarie, :instance_moderation) do - def pause? - instance_moderation.site.usuarie? usuarie - end - - def allow? - pause? - end - - def block? - pause? + InstanceModeration.events.each do |event| + define_method(:"#{event}?") do + instance_moderation.site.usuarie? usuarie + end end # En este paso tenemos varias instancias por moderar pero todas son