From ff428c652713c0d412b0660cb5788d274fec71fa Mon Sep 17 00:00:00 2001 From: f Date: Tue, 5 Mar 2024 14:18:06 -0300 Subject: [PATCH] feat: poder reportar comentarios --- app/controllers/activity_pubs_controller.rb | 5 +++- .../actor_moderations_controller.rb | 24 +------------------ .../concerns/moderation_concern.rb | 21 ++++++++++++++++ app/jobs/activity_pub/remote_flag_job.rb | 2 +- app/models/activity_pub.rb | 7 ++++++ app/models/activity_pub/remote_flag.rb | 7 +++++- config/locales/en.yml | 1 + config/locales/es.yml | 1 + .../20240305164653_change_remote_flags.rb | 12 ++++++++++ db/structure.sql | 6 +++-- 10 files changed, 58 insertions(+), 28 deletions(-) create mode 100644 db/migrate/20240305164653_change_remote_flags.rb diff --git a/app/controllers/activity_pubs_controller.rb b/app/controllers/activity_pubs_controller.rb index 9bbbb9cc..37702d96 100644 --- a/app/controllers/activity_pubs_controller.rb +++ b/app/controllers/activity_pubs_controller.rb @@ -8,6 +8,7 @@ class ActivityPubsController < ApplicationController define_method(event) do authorize activity_pub + activity_pub.update(remote_flag_params(activity_pub)) if event == :report activity_pub.public_send(:"#{event}!") if activity_pub.public_send(:"may_#{event}?") redirect_to_moderation_queue! @@ -29,7 +30,9 @@ class ActivityPubsController < ApplicationController ActivityPub.transaction do activity_pubs.find_each do |activity_pub| - activity_pub.public_send(method) if activity_pub.public_send(may) + next unless activity_pub.public_send(may) + + activity_pub.public_send(method) end end end diff --git a/app/controllers/actor_moderations_controller.rb b/app/controllers/actor_moderations_controller.rb index 2d834015..56adda4a 100644 --- a/app/controllers/actor_moderations_controller.rb +++ b/app/controllers/actor_moderations_controller.rb @@ -9,7 +9,7 @@ class ActorModerationsController < ApplicationController authorize actor_moderation # Crea una RemoteFlag si se envían los parámetros adecuados - actor_moderation.update(actor_moderation_params(actor_moderation)) if actor_event == :report + actor_moderation.update(remote_flag_params(actor_moderation)) if actor_event == :report actor_moderation.public_send(:"#{actor_event}!") if actor_moderation.public_send(:"may_#{actor_event}?") @@ -51,26 +51,4 @@ class ActorModerationsController < ApplicationController def actor_moderation @actor_moderation ||= site.actor_moderations.find(params[:actor_moderation_id] || params[:id]) end - - # @return [String] - def panel_actor_mention - @panel_actor_mention ||= ENV.fetch('PANEL_ACTOR_MENTION', '@sutty@sutty.nl') - end - - # @return [Hash] - def actor_moderation_params(actor_moderation) - { remote_flag_attributes: { id: actor_moderation.remote_flag_id, message: '' } }.tap do |p| - p[:remote_flag_attributes][:site_id] = actor_moderation.site_id - p[:remote_flag_attributes][:actor_id] = actor_moderation.actor_id - - I18n.available_locales.each do |locale| - p[:remote_flag_attributes][:message].tap do |m| - m += I18n.t(locale) - m += ': ' - m += I18n.t('actor_moderations.report_message', locale: locale, panel_actor_mention: panel_actor_mention) - m += '\n\n' - end - end - end - end end diff --git a/app/controllers/concerns/moderation_concern.rb b/app/controllers/concerns/moderation_concern.rb index 9a4f1c16..5b4c276d 100644 --- a/app/controllers/concerns/moderation_concern.rb +++ b/app/controllers/concerns/moderation_concern.rb @@ -9,5 +9,26 @@ module ModerationConcern def redirect_to_moderation_queue! redirect_back fallback_location: site_moderation_queue_path(**(session[:moderation_queue_filters] || {})) end + + # @return [String] + def panel_actor_mention + @panel_actor_mention ||= ENV.fetch('PANEL_ACTOR_MENTION', '@sutty@sutty.nl') + end + + def remote_flag_params(model) + { remote_flag_attributes: { id: model.remote_flag_id, message: '' } }.tap do |p| + p[:remote_flag_attributes][:site_id] = model.site_id + p[:remote_flag_attributes][:actor_id] = model.actor_id + + I18n.available_locales.each do |locale| + p[:remote_flag_attributes][:message].tap do |m| + m << I18n.t(locale) + m << ': ' + m << I18n.t('remote_flags.report_message', locale: locale, panel_actor_mention: panel_actor_mention) + m << '\n\n' + end + end + end + end end end diff --git a/app/jobs/activity_pub/remote_flag_job.rb b/app/jobs/activity_pub/remote_flag_job.rb index 332d31ac..30796923 100644 --- a/app/jobs/activity_pub/remote_flag_job.rb +++ b/app/jobs/activity_pub/remote_flag_job.rb @@ -13,7 +13,7 @@ class ActivityPub self.priority = 30 def perform(remote_flag:) - client = remote_flag.site.social_inbox.client_for(remote_flag.actor.content['inbox']) + client = remote_flag.site.social_inbox.client_for(remote_flag.actor&.content['inbox']) response = client.post(endpoint: '', body: remote_flag.content) raise 'No se pudo enviar el reporte' unless response.ok? diff --git a/app/models/activity_pub.rb b/app/models/activity_pub.rb index 6f0d884c..23f4324f 100644 --- a/app/models/activity_pub.rb +++ b/app/models/activity_pub.rb @@ -18,12 +18,15 @@ class ActivityPub < ApplicationRecord belongs_to :site belongs_to :object, polymorphic: true belongs_to :actor + belongs_to :remote_flag, class_name: 'ActivityPub::RemoteFlag' has_many :activities validates :site_id, presence: true validates :object_id, presence: true validates :aasm_state, presence: true, inclusion: { in: %w[paused approved rejected reported removed] } + accepts_nested_attributes_for :remote_flag + # Encuentra la URI de un objeto # # @return [String, nil] @@ -83,6 +86,10 @@ class ActivityPub < ApplicationRecord # Solo podemos reportarla luego de rechazarla event :report do transitions from: :rejected, to: :reported + + before do + ActivityPub::RemoteFlagJob.perform_later(remote_flag: remote_flag) + end end end end diff --git a/app/models/activity_pub/remote_flag.rb b/app/models/activity_pub/remote_flag.rb index b790c4b1..a302503b 100644 --- a/app/models/activity_pub/remote_flag.rb +++ b/app/models/activity_pub/remote_flag.rb @@ -5,6 +5,11 @@ class ActivityPub belongs_to :actor belongs_to :site + has_one :actor_moderation + has_many :activity_pubs + # XXX: source_type es obligatorio para el `through` + has_many :objects, through: :activity_pubs, source_type: 'ActivityPub::Object::Note' + # Genera la actividad a enviar def content { @@ -13,7 +18,7 @@ class ActivityPub 'type' => 'Flag', 'actor' => ENV.fetch('PANEL_ACTOR_ID') { "https://#{ENV['SUTTY']}/about.jsonld" }, 'content' => message.to_s, - 'object' => [ actor.uri ] + 'object' => [ actor.uri ] + objects.pluck(:uri) } end end diff --git a/config/locales/en.yml b/config/locales/en.yml index fe8798f4..0ca90aa7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -116,6 +116,7 @@ en: profile_id: ID profile_published: Published profile_summary: Summary + remote_flags: report_message: "Hi! Someone using Sutty CMS reported this account on your instance. We don't have support for customized report messages yet, but we will soon. You can reach us at %{panel_actor_mention}." moderation_queue: everything: 'Select all' diff --git a/config/locales/es.yml b/config/locales/es.yml index e22fdf57..40a1a18c 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -115,6 +115,7 @@ es: profile_id: ID profile_published: Publicada profile_summary: Presentación + remote_flags: report_message: "¡Hola! Une usuarie de Sutty CMS reportó esta cuenta en tu instancia. Todavía no tenemos soporte para mensajes personalizados. Podés contactarnos en %{panel_actor_mention}." moderation_queue: everything: 'Seleccionar todo' diff --git a/db/migrate/20240305164653_change_remote_flags.rb b/db/migrate/20240305164653_change_remote_flags.rb new file mode 100644 index 00000000..258f3335 --- /dev/null +++ b/db/migrate/20240305164653_change_remote_flags.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# Agrega relaciones en las remote flags +class ChangeRemoteFlags < ActiveRecord::Migration[6.1] + def up + add_column :activity_pubs, :remote_flag_id, :uuid, index: true, null: true + end + + def down + remove_column :activity_pubs, :remote_flag_id + end +end diff --git a/db/structure.sql b/db/structure.sql index ff6cf895..55a5ecb0 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -573,7 +573,8 @@ CREATE TABLE public.activity_pubs ( object_type character varying NOT NULL, aasm_state character varying NOT NULL, instance_id uuid, - actor_id uuid + actor_id uuid, + remote_flag_id uuid ); @@ -2695,6 +2696,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20240229201155'), ('20240301181224'), ('20240301194154'), -('20240301202955'); +('20240301202955'), +('20240305164653');