From 4bc11c700755d211967ef1b71e5e6828fc0f9e94 Mon Sep 17 00:00:00 2001 From: f Date: Tue, 27 Feb 2024 16:39:02 -0300 Subject: [PATCH] feat: gestionar listas de bloqueo desde el panel --- .../fediblock_states_controller.rb | 40 +++++++++++++++++++ .../activity_pub/instance_moderation_job.rb | 29 ++++++++++++++ app/models/site/social_distributed_press.rb | 1 + app/views/components/_block_list.haml | 14 ++++--- .../_block_instances_textarea.haml | 6 +-- app/views/moderation_queue/_instances.haml | 9 ++++- config/locales/en.yml | 2 + config/locales/es.yml | 2 + config/routes.rb | 1 + 9 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 app/controllers/fediblock_states_controller.rb create mode 100644 app/jobs/activity_pub/instance_moderation_job.rb diff --git a/app/controllers/fediblock_states_controller.rb b/app/controllers/fediblock_states_controller.rb new file mode 100644 index 00000000..c116b75a --- /dev/null +++ b/app/controllers/fediblock_states_controller.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +# Estado de las listas de bloqueo en cada sitio +class FediblockStatesController < ApplicationController + # Realiza cambios en las listas de bloqueo + def action_on_several + if fediblock_states_ids.present? + # Encontrar todas y deshabilitar las que no se enviaron + site.fediblock_states.all.find_each do |fediblock_state| + if fediblock_states_ids.include? fediblock_state.id + fediblock_state.enable! if fediblock_state.may_enable? + elsif fediblock_state.may_disable? + fediblock_state.disable! + end + end + end + + # Bloquear otras instancias + if custom_blocklist.present? + ActivityPub::InstanceModerationJob.perform_later(site: site, hostnames: custom_blocklist) + end + + redirect_to site_moderation_queue_path + end + + private + + def fediblock_states_ids + params[:fediblock_states_ids] || [] + end + + # La lista de hostnames + def custom_blocklist + @custom_blocklist ||= fediblocks_states_params[:custom_blocklist].split("\n").map(&:strip).select(&:present?) + end + + def fediblocks_states_params + @fediblocks_states_params ||= params.permit(:custom_blocklist, fediblock_states_ids: []) + end +end diff --git a/app/jobs/activity_pub/instance_moderation_job.rb b/app/jobs/activity_pub/instance_moderation_job.rb new file mode 100644 index 00000000..a9b0bea0 --- /dev/null +++ b/app/jobs/activity_pub/instance_moderation_job.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +class ActivityPub + # Bloquea varias instancias de una sola vez + class InstanceModerationJob < ApplicationJob + # @param :site [Site] + # @param :hostnames [Array] + def perform(site:, hostnames:) + # Crear las instancias que no existan todavía + hostnames.each do |hostname| + ActivityPub::Instance.find_or_create_by(hostname: hostname) + end + + instances = ActivityPub::Instance.where(hostname: hostnames) + + Site.transaction do + # Crea todas las moderaciones de instancia con un estado por + # defecto si no existen + instances.find_each do |instance| + # Esto bloquea cada una individualmente en la Social Inbox, + # idealmente son pocas instancias las que aparecen. + site.instance_moderations.find_or_create_by(instance: instance).tap do |instance_moderation| + instance_moderation.block! if instance_moderation.may_block? + end + end + end + end + end +end diff --git a/app/models/site/social_distributed_press.rb b/app/models/site/social_distributed_press.rb index e0d2d4f4..fdb37bca 100644 --- a/app/models/site/social_distributed_press.rb +++ b/app/models/site/social_distributed_press.rb @@ -13,6 +13,7 @@ class Site has_many :activity_pubs has_many :instance_moderations has_many :fediblock_states + has_many :instances, through: :instance_moderations before_save :generate_private_key_pem!, unless: :private_key_pem? diff --git a/app/views/components/_block_list.haml b/app/views/components/_block_list.haml index f972642a..79481363 100644 --- a/app/views/components/_block_list.haml +++ b/app/views/components/_block_list.haml @@ -1,8 +1,12 @@ -# Componente Listas de bloqueo de Instancias +- know_more = t('.know_more') .card.mt-3.mb-3 .card-body - .d-flex.flex-row - = render 'components/checkbox', id: state.id, checked: state.enabled? do - %span.h4= blocklist.title - %p.mb-0 - %a{ href: blocklist.url }= t('.more') + = render 'components/checkbox', id: state.id, name: 'fediblock_states_ids[]', value: state.id, checked: state.enabled? do + %span.h4.mb-0= blocklist.title + + %dl.mb-0 + %dt.d-inline= t('.instances_blocked') + %dd.d-inline.font-weight-normal= blocklist.instances.count + %p.mb-0.font-weight-normal + %a{ href: blocklist.url }= know_more diff --git a/app/views/moderation_queue/_block_instances_textarea.haml b/app/views/moderation_queue/_block_instances_textarea.haml index 9b388a0d..9729d4de 100644 --- a/app/views/moderation_queue/_block_instances_textarea.haml +++ b/app/views/moderation_queue/_block_instances_textarea.haml @@ -1,5 +1,3 @@ .form-group - = label_tag "custom_blocklist", t('moderation_queue.instances.custom_block') - = text_area_tag "custom_blocklist", nil, class: 'form-control' - %button.btn.btn-secondary.mt-3{ type: 'submit' }= t('moderation_queue.instances.submit') - + = label_tag 'custom_blocklist', t('moderation_queue.instances.custom_block') + = text_area_tag 'custom_blocklist', nil, class: 'form-control' diff --git a/app/views/moderation_queue/_instances.haml b/app/views/moderation_queue/_instances.haml index a08c8b10..d2ebb5a7 100644 --- a/app/views/moderation_queue/_instances.haml +++ b/app/views/moderation_queue/_instances.haml @@ -19,5 +19,10 @@ %div %h3.mt-5= t('moderation_queue.instances.title') %lead= t('moderation_queue.instances.description') - = render 'components/block_lists', blocklists: fediblock_states - = render 'moderation_queue/block_instances_textarea' + + = form_tag site_fediblock_states_action_on_several_path, method: :patch do + = render 'components/block_lists', blocklists: fediblock_states + = render 'moderation_queue/block_instances_textarea' + + .form-group + %button.btn.btn-secondary.mt-3{ type: 'submit' }= t('moderation_queue.instances.submit') diff --git a/config/locales/en.yml b/config/locales/en.yml index 9581285f..717c9ece 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -50,6 +50,8 @@ en: pm: pm format: '%-I:%M %p' components: + block_list: + know_more: Know more instances_filters: text_show: Show text_checked: With selected diff --git a/config/locales/es.yml b/config/locales/es.yml index 9b1f1937..87111523 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -50,6 +50,8 @@ es: pm: pm format: '%-H:%M' components: + block_list: + know_more: Saber más (en inglés) instances_filters: text_show: Ver text_checked: Con los marcados diff --git a/config/routes.rb b/config/routes.rb index d9e2aca7..d70f8339 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -69,6 +69,7 @@ Rails.application.routes.draw do end patch :instance_moderations_action_on_several, to: 'instance_moderations#action_on_several' + patch :fediblock_states_action_on_several, to: 'fediblock_states#action_on_several' # Gestionar artículos según idioma nested do