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


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



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



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