# frozen_string_literal: true # Mantiene la relación entre Site y Actor class ActorModeration < ApplicationRecord include AASM include AasmEventsConcern IGNORED_EVENTS = %i[remove] IGNORED_STATES = %i[removed] belongs_to :site belongs_to :remote_flag, optional: true, class_name: 'ActivityPub::RemoteFlag' belongs_to :actor, class_name: 'ActivityPub::Actor' accepts_nested_attributes_for :remote_flag # Bloquea todes les Actores bloqueables def self.block_all! self.update_all(aasm_state: 'blocked', updated_at: Time.now) end def self.pause_all! self.update_all(aasm_state: 'paused', updated_at: Time.now) end def self.remove_all! self.update_all(aasm_state: 'removed', updated_at: Time.now) end aasm do state :paused, initial: true state :allowed state :blocked state :reported state :removed event :pause do transitions from: %i[allowed blocked reported], to: :paused before do pause_remotely! end end # Al permitir una cuenta se permiten todos los comentarios # pendientes de moderación que ya hizo. event :allow do transitions from: %i[paused blocked reported], to: :allowed before do allow_remotely! site.activity_pubs.paused.where(actor_id: self.actor_id).find_each do |activity_pub| activity_pub.allow! if activity_pub.may_allow? rescue Exception => e ExceptionNotifier.notify_exception(e, data: { site: site.name, activity_pub: activity_pub_id }) end end end # Al bloquear una cuenta se bloquean todos los comentarios # pendientes de moderación que hizo. event :block do transitions from: %i[paused allowed], to: :blocked before do block_remotely! site.activity_pubs.paused.where(actor_id: self.actor_id).find_each do |activity_pub| activity_pub.reject! if activity_pub.may_reject? rescue Exception => e ExceptionNotifier.notify_exception(e, data: { site: site.name, activity_pub: activity_pub_id }) end end end # Al reportar, necesitamos asociar una RemoteFlag para poder # enviarla. event :report do transitions from: %i[blocked], to: :reported before do ActivityPub::RemoteFlagJob.perform_later(remote_flag: remote_flag) if remote_flag.waiting? end end # Si un perfil es eliminado remotamente, tenemos que dejar de # mostrarlo y todas sus actividades. event :remove do transitions to: :removed end end def pause_remotely! raise unless actor.mention && site.social_inbox.allowlist.delete(list: [actor.mention]).ok? && site.social_inbox.blocklist.delete(list: [actor.mention]).ok? end def allow_remotely! raise unless actor.mention && site.social_inbox.allowlist.post(list: [actor.mention]).ok? && site.social_inbox.blocklist.delete(list: [actor.mention]).ok? end def block_remotely! raise unless actor.mention && site.social_inbox.allowlist.delete(list: [actor.mention]).ok? && site.social_inbox.blocklist.post(list: [actor.mention]).ok? end end