From b972e53e4821c563eaebabb5e1cbcb899ea063f8 Mon Sep 17 00:00:00 2001 From: f Date: Sat, 16 Mar 2024 17:53:29 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20guardar=20la=20menci=C3=B3n=20de=20le?= =?UTF-8?q?=20autore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/activity_pub/fetch_job.rb | 7 +++++- app/models/activity_pub/actor.rb | 9 ++++--- .../20240316203721_add_mention_to_actors.rb | 24 +++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20240316203721_add_mention_to_actors.rb diff --git a/app/jobs/activity_pub/fetch_job.rb b/app/jobs/activity_pub/fetch_job.rb index 129908d3..e9220bfc 100644 --- a/app/jobs/activity_pub/fetch_job.rb +++ b/app/jobs/activity_pub/fetch_job.rb @@ -29,8 +29,13 @@ class ActivityPub object.update(content: content, type: ActivityPub::Object.type_from(content).name) + return if current_type == object.type + + object = ::ActivityPub::Object.find(object_id) + object.actor.save if object.actor_type? + # Arreglar las relaciones con actividades también - ActivityPub.where(object_id: object.id).update_all(object_type: object.type) unless current_type == object.type + ActivityPub.where(object_id: object.id).update_all(object_type: object.type) rescue FastJsonparser::ParseError => e ExceptionNotifier.notify_exception(e, data: { site: site.name, body: response.body }) end diff --git a/app/models/activity_pub/actor.rb b/app/models/activity_pub/actor.rb index b03145e7..a8cc0d5d 100644 --- a/app/models/activity_pub/actor.rb +++ b/app/models/activity_pub/actor.rb @@ -18,19 +18,22 @@ class ActivityPub # Les actores son únicxs a toda la base de datos validates :uri, presence: true, url: true, uniqueness: true + before_save :mentionize! + # Obtiene el nombre de la Actor como mención, solo si obtuvimos el # contenido de antemano. # # @return [String, nil] - def mention + def mentionize! + return if mention.present? return if content['preferredUsername'].blank? return if instance.blank? - @mention ||= "@#{content['preferredUsername']}@#{instance.hostname}" + self.mention ||= "@#{content['preferredUsername']}@#{instance.hostname}" end def object - @object ||= ActivityPub::Object::Person.find_or_initialize_by(uri: uri) + @object ||= ActivityPub::Object.find_or_initialize_by(uri: uri) end def content diff --git a/db/migrate/20240316203721_add_mention_to_actors.rb b/db/migrate/20240316203721_add_mention_to_actors.rb new file mode 100644 index 00000000..caa4f526 --- /dev/null +++ b/db/migrate/20240316203721_add_mention_to_actors.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +# Guarda la mención en la tabla de actores +class AddMentionToActors < ActiveRecord::Migration[6.1] + def up + add_column :activity_pub_actors, :mention, :string, null: true + + actor_types = %w[ + ActivityPub::Object::Application + ActivityPub::Object::Group + ActivityPub::Object::Organization + ActivityPub::Object::Person + ActivityPub::Object::Service + ] + + ActivityPub::Object.where(type: actor_types).where.not(content: {}).find_each do |object| + ActivityPub::Actor.find_by_uri(object.uri)&.save + end + end + + def down + remove_column :activity_pub_actors, :mention + end +end