Module: Federails::DataEntity

Extended by:
ActiveSupport::Concern
Includes:
HandlesDeleteRequests
Defined in:
app/models/concerns/federails/data_entity.rb

Overview

Model concern to include in models for which data is pushed to the Fediverse and comes from the Fediverse.

Once included, an activity will automatically be created upon

- entity creation
- entity updates

Also, when properly configured, a handler is registered to transform incoming objects and create/update entities accordingly.

## Pre-requisites

Model should have the following methods:

- `to_activitypub_object`, returning a valid ActivityPub object
- `self.from_activitypub_object`, returning a hash of valid attributes from a hash of incoming data

Table needs at least:
- `t.string :federated_url, null: true, default: nil`
- `t.references :federails_actor, foreign_key: true, null: true, default: nil

Model must have the following attributes: “‘rb add_column :posts, :federated_url, :string, null: true, default: nil add_reference :posts, :federails_actor, foreign_key: true, null: true, default: nil “`

## Usage

Include the concern in an existing model:

“‘rb class Post < ApplicationRecord

include Federails::DataEntity
acts_as_federails_data options

# This will be called when a Delete activity comes for the entry. As we don't know how you want to handle it,
# you'll have to implement the behavior yourself.
on_federails_delete_requested :do_something

# This will be called when a Undo activity comes for the entry. The easiest way to handle this case is to re-fetch
# the entity
on_federails_undelete_requested :do_something_else

def to_activitypub_object
  Federails::DataTransformer::Note.to_federation self,
                                                 content: content,
                                                 name:    title
end

# Creates a hash of attributes from incoming Note
def self.from_activitypub_object(hash)
  {
    title:   hash['name'] || 'A post',
    content: hash['content'],
  }
end

end “‘

**If your model has a mechanism for soft deletion:**

  • you can specify some methods names to handle it in Federails responses:

  • you will need to send the delete activity yourself

“‘rb acts_as_federails_data handles: ’Note’,

...,
soft_deleted_method: :deleted?
soft_delete_date_method: :deleted_at

on_federails_delete_requested :soft_delete! on_federails_undelete_requested :restore_remote_entity!

# Method you use to soft-delete entities def soft_delete!

update deleted_at: time.current

send_federails_activity 'Delete' unless local_federails_entity?

end

# Method you use to restore soft-deleted entities def restore!

update deleted_at: nil

if local_federails_entity?
  delete_activity =  Activity.find_by action: 'Delete', entity: self
  send_federails_activity 'Undo', entity: delete_activity, actor: federails_actor if delete_activity.present?
end

end

def restore_remote_entity!

self.deleted_at: nil
federails_sync!
save!

end “‘

Defined Under Namespace

Modules: ClassMethods Classes: TombstonedError

Instance Method Summary collapse

Instance Method Details

#federails_data_configurationObject



237
238
239
# File 'app/models/concerns/federails/data_entity.rb', line 237

def federails_data_configuration
  Federails.data_entity_configuration(self)
end

#federails_sync!Object



241
242
243
244
245
246
247
248
249
250
# File 'app/models/concerns/federails/data_entity.rb', line 241

def federails_sync!
  if local_federails_entity?
    Rails.logger.info { "Ignored attempt to sync a local #{self.class.name}" }
    return false
  end

  object = Fediverse::Request.dereference(federated_url)

  update! self.class.from_activitypub_object(object)
end

#federails_tombstoned?Boolean



229
230
231
# File 'app/models/concerns/federails/data_entity.rb', line 229

def federails_tombstoned?
  federails_data_configuration[:soft_deleted_method] ? send(federails_data_configuration[:soft_deleted_method]) : false
end

#federails_tombstoned_atObject



233
234
235
# File 'app/models/concerns/federails/data_entity.rb', line 233

def federails_tombstoned_at
  federails_data_configuration[:soft_delete_date_method] ? send(federails_data_configuration[:soft_delete_date_method]) : nil
end

#federated_urlString

Computed value for the federated URL



213
214
215
216
217
218
219
220
# File 'app/models/concerns/federails/data_entity.rb', line 213

def federated_url
  return nil unless send(federails_data_configuration[:should_federate_method])
  return attributes['federated_url'] if attributes['federated_url'].present?

  path_segment = Federails.data_entity_configuration(self)[:route_path_segment]
  url_param = Federails.data_entity_configuration(self)[:url_param]
  Federails::Engine.routes.url_helpers.server_published_url(publishable_type: path_segment, id: send(url_param))
end

#local_federails_entity?Boolean

Check whether the entity was created locally or comes from the Fediverse



225
226
227
# File 'app/models/concerns/federails/data_entity.rb', line 225

def local_federails_entity?
  attributes['federated_url'].blank?
end