Module: Federails::DataEntity::ClassMethods

Defined in:
app/models/concerns/federails/data_entity.rb

Overview

Class methods automatically included in the concern.

Instance Method Summary collapse

Instance Method Details

#acts_as_federails_data(handles:, with: :handle_incoming_fediverse_data, route_path_segment: nil, actor_entity_method: nil, url_param: :id, filter_method: nil, should_federate_method: :default_should_federate?, soft_deleted_method: nil, soft_delete_date_method: nil) ⇒ Object

Configures the mapping between entity and Fediverse

rubocop:disable Metrics/ParameterLists, Metrics/MethodLength

Examples:

acts_as_federails_data handles: 'Note', with: :note_handler, route_path_segment: :articles, actor_entity_method: :user

Parameters:

  • actor_entity_method (Symbol) (defaults to: nil)

    Method returning an object responding to ‘federails_actor’, for local content

  • url_param (Symbol) (defaults to: :id)

    Column name of the object ID that should be used in URLs. Defaults to :id

  • route_path_segment (Symbol) (defaults to: nil)

    Segment used in Federails routes to display the ActivityPub representation. Defaults to the pluralized, underscored class name

  • handles (String)

    Type of ActivityPub object handled by this entity type

  • with (Symbol) (defaults to: :handle_incoming_fediverse_data)

    Self class method that will handle incoming objects. Defaults to :handle_incoming_fediverse_data

  • filter_method (Symbol) (defaults to: nil)

    Self class method that determines if an incoming object should be handled. Note that the first model for which this method returns true will be used. If left empty, the model CAN be selected, so define them if many models handle the same data type.

  • should_federate_method (Symbol) (defaults to: :default_should_federate?)

    method to determine if an object should be federated. If the method returns false, no create/update activities will happen, and object will not be accessible at federated_url. Defaults to a method that always returns true.

  • soft_deleted_method (Symbol, nil) (defaults to: nil)

    If the model uses a soft-delete mechanism, this is the method to check if entity is soft-deleted. This is not required by the spec but greatly encouraged as the app will return a 410 response with a Tombstone object instead of an 404 error.

  • soft_delete_date_method (Symbol, nil) (defaults to: nil)

    Method to get the date of the soft-deletion



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/models/concerns/federails/data_entity.rb', line 128

def acts_as_federails_data(
  handles:,
  with: :handle_incoming_fediverse_data,
  route_path_segment: nil,
  actor_entity_method: nil,
  url_param: :id,
  filter_method: nil,
  should_federate_method: :default_should_federate?,
  soft_deleted_method: nil,
  soft_delete_date_method: nil
)
  route_path_segment ||= name.pluralize.underscore

  Federails::Configuration.register_data_type self,
                                              route_path_segment:      route_path_segment,
                                              actor_entity_method:     actor_entity_method,
                                              url_param:               url_param,
                                              handles:                 handles,
                                              with:                    with,
                                              filter_method:           filter_method,
                                              should_federate_method:  should_federate_method,
                                              soft_deleted_method:     soft_deleted_method,
                                              soft_delete_date_method: soft_delete_date_method

  # NOTE: Delete activities cannot be handled like this as we can't be sure to have the object's type
  Fediverse::Inbox.register_handler 'Create', handles, self, with
  Fediverse::Inbox.register_handler 'Update', handles, self, with
end

#find_untombstoned_by!(**params) ⇒ Object



188
189
190
191
192
193
194
195
# File 'app/models/concerns/federails/data_entity.rb', line 188

def find_untombstoned_by!(**params)
  configuration = Federails.data_entity_configuration(self)
  entity = find_by!(**params)

  raise Federails::DataEntity::TombstonedError if configuration[:soft_deleted_method] && entity.send(configuration[:soft_deleted_method])

  entity
end

#handle_incoming_fediverse_data(activity_hash_or_id) ⇒ self

Creates or updates entity based on the ActivityPub activity

Parameters:

  • activity_hash_or_id (Hash, String)

    Dereferenced activity hash or ID

Returns:

  • (self)


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/models/concerns/federails/data_entity.rb', line 172

def handle_incoming_fediverse_data(activity_hash_or_id)
  activity = Fediverse::Request.dereference(activity_hash_or_id)
  object = Fediverse::Request.dereference(activity['object'])

  entity = Federails::Utils::Object.find_or_create!(object)

  if activity['type'] == 'Update'
    entity.assign_attributes from_activitypub_object(object)

    # Use timestamps from attributes
    entity.save! touch: false
  end

  entity
end

#new_from_activitypub_object(activitypub_object) ⇒ self

Instantiates a new instance from an ActivityPub object

Parameters:

  • activitypub_object (Hash)

Returns:

  • (self)


163
164
165
# File 'app/models/concerns/federails/data_entity.rb', line 163

def new_from_activitypub_object(activitypub_object)
  new from_activitypub_object(activitypub_object)
end