2024-02-20 17:44:52 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# = ActivityPub =
|
|
|
|
#
|
|
|
|
# El registro de actividades recibidas y su estado. Cuando recibimos
|
|
|
|
# una actividad, puede estar destinada a varies actores dentro de Sutty,
|
|
|
|
# con lo que generamos una cola para cada une.
|
|
|
|
#
|
2024-03-15 21:05:05 +00:00
|
|
|
#
|
|
|
|
# @todo Ya que une actore puede hacer varias actividades sobre el mismo
|
|
|
|
# objeto, lo correcto sería que la actividad a moderar sea una sola en
|
|
|
|
# lugar de una lista acumulativa. Es decir cada ActivityPub representa
|
|
|
|
# el estado del conjunto (Actor, Object, Activity)
|
|
|
|
#
|
2024-02-20 17:44:52 +00:00
|
|
|
# @see {https://www.w3.org/TR/activitypub/#client-to-server-interactions}
|
|
|
|
class ActivityPub < ApplicationRecord
|
2024-03-15 17:26:12 +00:00
|
|
|
IGNORED_EVENTS = %i[pause remove].freeze
|
|
|
|
IGNORED_STATES = %i[removed].freeze
|
2024-02-20 17:44:52 +00:00
|
|
|
|
2024-03-12 17:24:38 +00:00
|
|
|
include AASM
|
|
|
|
|
2024-02-26 13:39:00 +00:00
|
|
|
belongs_to :instance
|
2024-02-20 17:44:52 +00:00
|
|
|
belongs_to :site
|
|
|
|
belongs_to :object, polymorphic: true
|
2024-03-04 14:59:29 +00:00
|
|
|
belongs_to :actor
|
2024-03-06 18:36:00 +00:00
|
|
|
belongs_to :remote_flag, optional: true, class_name: 'ActivityPub::RemoteFlag'
|
2024-02-20 17:44:52 +00:00
|
|
|
has_many :activities
|
|
|
|
|
|
|
|
validates :site_id, presence: true
|
|
|
|
validates :object_id, presence: true
|
2024-02-21 20:04:39 +00:00
|
|
|
validates :aasm_state, presence: true, inclusion: { in: %w[paused approved rejected reported removed] }
|
2024-02-20 17:44:52 +00:00
|
|
|
|
2024-03-05 17:18:06 +00:00
|
|
|
accepts_nested_attributes_for :remote_flag
|
|
|
|
|
2024-02-23 15:54:31 +00:00
|
|
|
# Encuentra la URI de un objeto
|
|
|
|
#
|
|
|
|
# @return [String, nil]
|
|
|
|
def self.uri_from_object(object)
|
|
|
|
case object
|
2024-03-01 20:29:08 +00:00
|
|
|
when Array then uri_from_object(object.first)
|
2024-02-23 15:54:31 +00:00
|
|
|
when String then object
|
2024-03-01 20:29:08 +00:00
|
|
|
when Hash then (object['id'] || object[:id])
|
2024-02-23 15:54:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-18 14:21:13 +00:00
|
|
|
# Obtiene el campo `url` de diversas formas. Si es una String, asumir
|
|
|
|
# que es una URL, si es un Hash, asumir que es un Link, si es un
|
|
|
|
# Array de Strings, obtener la primera, si es de Hash, obtener el
|
|
|
|
# primer link con rel=canonical y mediaType=text/html
|
|
|
|
#
|
|
|
|
# De lo contrario devolver el ID.
|
|
|
|
#
|
|
|
|
# @todo Refactorizar
|
|
|
|
# @param object [Hash]
|
|
|
|
# @return [String]
|
|
|
|
def self.url_from_object(object)
|
|
|
|
raise unless object.respond_to?(:[])
|
|
|
|
|
|
|
|
url =
|
|
|
|
case object['url']
|
|
|
|
when String then object['url']
|
|
|
|
when Hash then object['href']
|
|
|
|
# Esto es un lío porque queremos saber si es un Array<Hash> o
|
|
|
|
# Array<String> o mezcla y obtener el que más nos convenga o
|
|
|
|
# adivinar uno.
|
|
|
|
when Array
|
|
|
|
links = object['url'].map.with_index do |link, i|
|
|
|
|
case link
|
|
|
|
when Hash then link
|
|
|
|
else { 'href' => link.to_s }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
links.find do |link|
|
|
|
|
link['rel'] == 'canonical' && link['mediaType'] == 'text/html'
|
|
|
|
end&.[]('href') || links.first&.[]('href')
|
|
|
|
end
|
|
|
|
|
|
|
|
url || object['id']
|
|
|
|
end
|
|
|
|
|
2024-02-20 17:44:52 +00:00
|
|
|
aasm do
|
|
|
|
# Todavía no hay una decisión sobre el objeto
|
|
|
|
state :paused, initial: true
|
|
|
|
# Le usuarie aprobó el objeto
|
2024-03-16 22:16:30 +00:00
|
|
|
state :approved
|
2024-02-20 17:44:52 +00:00
|
|
|
# Le usuarie rechazó el objeto
|
2024-03-16 22:16:30 +00:00
|
|
|
state :rejected
|
2024-02-20 17:44:52 +00:00
|
|
|
# Le usuarie reportó el objeto
|
|
|
|
state :reported
|
|
|
|
# Le actore eliminó el objeto
|
2024-02-21 18:44:41 +00:00
|
|
|
state :removed
|
2024-02-20 20:15:57 +00:00
|
|
|
|
2024-03-16 18:12:55 +00:00
|
|
|
# Gestionar todos los errores
|
|
|
|
error_on_all_events do |e|
|
|
|
|
ExceptionNotifier.notify_exception(e, data: { site: site.name, activity_pub: self.id, activity: activities.first.uri })
|
|
|
|
end
|
|
|
|
|
2024-03-14 20:08:38 +00:00
|
|
|
# Se puede volver a pausa en caso de actualización remota, para
|
|
|
|
# revisar los cambios.
|
|
|
|
event :pause do
|
|
|
|
transitions to: :paused
|
|
|
|
end
|
|
|
|
|
2024-02-20 20:15:57 +00:00
|
|
|
# Recibir una acción de eliminación, eliminar el contenido de la
|
|
|
|
# base de datos. Esto elimina el contenido para todos los sitios
|
|
|
|
# porque estamos respetando lo que pidió le actore.
|
2024-02-21 18:44:41 +00:00
|
|
|
event :remove do
|
|
|
|
transitions to: :removed
|
2024-02-20 20:15:57 +00:00
|
|
|
|
2024-03-16 18:12:55 +00:00
|
|
|
after do
|
2024-03-13 18:52:33 +00:00
|
|
|
next if object.blank?
|
|
|
|
|
2024-02-21 20:05:17 +00:00
|
|
|
object.update(content: {}) unless object.content.empty?
|
2024-02-20 20:15:57 +00:00
|
|
|
end
|
|
|
|
end
|
2024-02-20 20:18:15 +00:00
|
|
|
|
2024-03-04 19:46:43 +00:00
|
|
|
# La actividad se aprueba, informándole a la Social Inbox que está
|
|
|
|
# aprobada. También recibimos la aprobación via
|
|
|
|
# webhook a modo de confirmación.
|
2024-02-21 16:04:15 +00:00
|
|
|
event :approve do
|
2024-03-04 19:52:28 +00:00
|
|
|
transitions from: %i[paused], to: :approved
|
2024-03-16 22:16:30 +00:00
|
|
|
|
|
|
|
after do
|
|
|
|
ActivityPub::InboxJob.perform_later(site: site, activity: activities.first.uri, action: :accept)
|
|
|
|
end
|
2024-02-20 20:18:15 +00:00
|
|
|
end
|
2024-02-21 16:06:06 +00:00
|
|
|
|
|
|
|
# La actividad fue rechazada
|
|
|
|
event :reject do
|
2024-03-15 17:31:54 +00:00
|
|
|
transitions from: %i[paused], to: :rejected
|
2024-03-16 22:16:30 +00:00
|
|
|
|
|
|
|
after do
|
|
|
|
ActivityPub::InboxJob.perform_later(site: site, activity: activities.first.uri, action: :reject)
|
|
|
|
end
|
2024-02-21 16:06:06 +00:00
|
|
|
end
|
2024-02-22 21:42:36 +00:00
|
|
|
|
|
|
|
# Solo podemos reportarla luego de rechazarla
|
|
|
|
event :report do
|
|
|
|
transitions from: :rejected, to: :reported
|
2024-03-05 17:18:06 +00:00
|
|
|
|
2024-03-16 18:12:55 +00:00
|
|
|
after do
|
2024-03-05 19:00:47 +00:00
|
|
|
ActivityPub::RemoteFlagJob.perform_later(remote_flag: remote_flag) if remote_flag.waiting?
|
2024-03-05 17:18:06 +00:00
|
|
|
end
|
2024-02-22 21:42:36 +00:00
|
|
|
end
|
2024-02-20 17:44:52 +00:00
|
|
|
end
|
2024-03-08 18:55:01 +00:00
|
|
|
|
2024-03-13 14:15:31 +00:00
|
|
|
# Definir eventos en masa
|
|
|
|
include AasmEventsConcern
|
2024-02-20 17:44:52 +00:00
|
|
|
end
|