module Federails module Entity extend ActiveSupport::Concern included do # rubocop:todo Metrics/BlockLength include ActiveSupport::Callbacks define_callbacks :followed # Define a method that will be called after the entity receives a follow request # @param method [Symbol] The name of the method to call, or a block that will be called directly # @example # after_followed :accept_follow def self.after_followed(method) set_callback :followed, :after, method end # Define a method that will be called after an activity has been received # @param activity_type [String] The activity action to handle, e.g. 'Create'. If you specify '*', the handler will be called for any activity type. # @param object_type [String] The object type to handle, e.g. 'Note'. If you specify '*', the handler will be called for any object type. # @param method [Symbol] The name of the class method to call. The method will receive the complete activity payload as a parameter. # @example # after_activity_received 'Create', 'Note', :create_note def self.after_activity_received(activity_type, object_type, method) Fediverse::Inbox.register_handler(activity_type, object_type, self, method) end has_one :actor, class_name: 'Federails::Actor', as: :entity, dependent: :destroy after_create :create_actor, if: lambda { raise("Entity not configured for #{self.class.name}. Did you use \"acts_as_federails_actor\"?") unless Federails::Configuration.entity_types.key? self.class.name Federails::Configuration.entity_types[self.class.name][:auto_create_actors] } # Configures the mapping between entity and actor # @param username_field [Symbol] The method or attribute name that returns the preferred username for ActivityPub # @param name_field [Symbol] The method or attribute name that returns the preferred name for ActivityPub # @param profile_url_method [Symbol] The route method name that will generate the profile URL for ActivityPub # @param actor_type [String] The ActivityStreams Actor type for this entity; defaults to 'Person' # @param user_count_method [Symbol] A class method to call to count active users. Leave unspecified to leave this # entity out of user counts. Method signature should accept a single parameter which will specify a date range # If parameter is nil, the total user count should be returned. If the parameter is specified, the number of users # active during the time period should be returned. # @param auto_create_actors [Boolean] Whether to automatically create an actor when the entity is created # @example # acts_as_federails_actor username_field: :username, name_field: :display_name, profile_url_method: :url_for, actor_type: 'Person' # rubocop:disable Metrics/ParameterLists def self.acts_as_federails_actor( name_field:, username_field:, profile_url_method: nil, actor_type: 'Person', user_count_method: nil, auto_create_actors: true ) Federails::Configuration.register_entity( self, username_field: username_field, name_field: name_field, profile_url_method: profile_url_method, actor_type: actor_type, user_count_method: user_count_method, auto_create_actors: auto_create_actors ) end # rubocop:enable Metrics/ParameterLists # Add custom data to actor responses. # Override in your own model to add extra data, which will be merged into the actor response # generated by Federails. You can include extra `@context` for activitypub extensions and it will # be merged with the main response context. # @example # def to_activitypub_object # { # "@context": { # toot: "http://joinmastodon.org/ns#", # attributionDomains: { # "@id": "toot:attributionDomains", # "@type": "@id" # } # }, # attributionDomains: [ # "example.com" # ] # } # end def to_activitypub_object {} end private def create_actor Federails::Actor.create! entity: self end end end end