From 5b53a9813f163ecffe592be15f5c146eca1fca61 Mon Sep 17 00:00:00 2001 From: f Date: Tue, 5 Mar 2024 16:00:47 -0300 Subject: [PATCH] feat: enviar una sola vez el reporte remoto y volver a enviarlo si le agregamos reportes --- app/controllers/activity_pubs_controller.rb | 2 ++ app/jobs/activity_pub/remote_flag_job.rb | 6 ++++++ app/models/activity_pub.rb | 2 +- app/models/activity_pub/remote_flag.rb | 21 +++++++++++++++++++ app/models/actor_moderation.rb | 2 +- ...0240305184854_add_state_to_remote_flags.rb | 8 +++++++ db/structure.sql | 6 ++++-- 7 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20240305184854_add_state_to_remote_flags.rb diff --git a/app/controllers/activity_pubs_controller.rb b/app/controllers/activity_pubs_controller.rb index 2b54cacd..c8f86ef0 100644 --- a/app/controllers/activity_pubs_controller.rb +++ b/app/controllers/activity_pubs_controller.rb @@ -35,6 +35,8 @@ class ActivityPubsController < ApplicationController activity_pubs.distinct.pluck(:actor_id).each do |actor_id| remote_flag = ActivityPub::RemoteFlag.find_or_initialize_by(actor_id: actor_id, site_id: site.id) remote_flag.message = message + # Lo estamos actualizando, con lo que lo vamos a volver a enviar + remote_flag.requeue if remote_flag.persisted? remote_flag.save # XXX: Idealmente todas las ActivityPub que enviamos pueden # cambiar de estado, pero chequeamos de todas formas. diff --git a/app/jobs/activity_pub/remote_flag_job.rb b/app/jobs/activity_pub/remote_flag_job.rb index 30796923..7d8131db 100644 --- a/app/jobs/activity_pub/remote_flag_job.rb +++ b/app/jobs/activity_pub/remote_flag_job.rb @@ -13,10 +13,16 @@ class ActivityPub self.priority = 30 def perform(remote_flag:) + return if remote_flag.can_queue? + + remote_flag.queue! + 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? + + remote_flag.send! rescue Exception => e ExceptionNotifier.notify_exception(e, data: { remote_flag: remote_flag.id, response: response.parsed_response }) raise diff --git a/app/models/activity_pub.rb b/app/models/activity_pub.rb index 23f4324f..b07fe790 100644 --- a/app/models/activity_pub.rb +++ b/app/models/activity_pub.rb @@ -88,7 +88,7 @@ class ActivityPub < ApplicationRecord transitions from: :rejected, to: :reported before do - ActivityPub::RemoteFlagJob.perform_later(remote_flag: remote_flag) + ActivityPub::RemoteFlagJob.perform_later(remote_flag: remote_flag) if remote_flag.waiting? end end end diff --git a/app/models/activity_pub/remote_flag.rb b/app/models/activity_pub/remote_flag.rb index a302503b..25f1b743 100644 --- a/app/models/activity_pub/remote_flag.rb +++ b/app/models/activity_pub/remote_flag.rb @@ -2,6 +2,27 @@ class ActivityPub class RemoteFlag < ApplicationRecord + include AASM + include AasmEventsConcern + + aasm do + state :waiting, initial: true + state :queued + state :sent + + event :queue do + transitions from: :waiting, to: :queued + end + + event :send do + transitions from: :queued, to: :sent + end + + event :resend do + transitions from: :sent, to: :waiting + end + end + belongs_to :actor belongs_to :site diff --git a/app/models/actor_moderation.rb b/app/models/actor_moderation.rb index 421d4c6e..d7eea709 100644 --- a/app/models/actor_moderation.rb +++ b/app/models/actor_moderation.rb @@ -59,7 +59,7 @@ class ActorModeration < ApplicationRecord transitions from: %i[blocked], to: :reported before do - ActivityPub::RemoteFlagJob.perform_later(remote_flag: remote_flag) + ActivityPub::RemoteFlagJob.perform_later(remote_flag: remote_flag) if remote_flag.waiting? end end end diff --git a/db/migrate/20240305184854_add_state_to_remote_flags.rb b/db/migrate/20240305184854_add_state_to_remote_flags.rb new file mode 100644 index 00000000..7ff78dfb --- /dev/null +++ b/db/migrate/20240305184854_add_state_to_remote_flags.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# Estado de los reportes remotos +class AddStateToRemoteFlags < ActiveRecord::Migration[6.1] + def change + add_column :activity_pub_remote_flags, :aasm_state, :string, null: false, default: 'waiting' + end +end diff --git a/db/structure.sql b/db/structure.sql index 55a5ecb0..97bd372e 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -556,7 +556,8 @@ CREATE TABLE public.activity_pub_remote_flags ( updated_at timestamp(6) without time zone NOT NULL, site_id bigint, actor_id uuid, - message text + message text, + aasm_state character varying DEFAULT 'waiting'::character varying NOT NULL ); @@ -2697,6 +2698,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20240301181224'), ('20240301194154'), ('20240301202955'), -('20240305164653'); +('20240305164653'), +('20240305184854');