Class: Federails::Utils::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/federails/utils/object.rb

Overview

Methods to manipulate incoming objects

Class Method Summary collapse

Class Method Details

.find_distant_object_in_all(federated_url) ⇒ Federails::Actor, ...

Search for a distant object in actors and configured data entities.

This is useful to find something when the type is unknown, as an object from a Delete activity.

Parameters:

  • federated_url (String)

    Object identifier

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/federails/utils/object.rb', line 29

def find_distant_object_in_all(federated_url)
  # Search in actors
  object = Federails::Actor.find_by federated_url: federated_url
  return object if object.present?

  # Search in followings
  object = Federails::Following.find_by federated_url: federated_url
  return object if object.present?

  # Search in data entities
  Federails.configuration.data_types.keys.sort.each do |klass|
    object = klass.constantize.find_by federated_url: federated_url

    break if object.present?
  end

  object
end

.find_or_create!(object_or_id) ⇒ ApplicationRecord?

Finds or create an entity from an ActivityPub object or id

Note that the data transformer MUST return timestamps from the ActivityPub object if used on the model, as they won’t be set automatically.

Parameters:

  • object_or_id (String, Hash)

    String identifier or incoming object

Returns:

See Also:



72
73
74
75
76
77
78
# File 'lib/federails/utils/object.rb', line 72

def find_or_create!(object_or_id)
  entity = find_or_initialize! object_or_id
  return entity if entity.persisted?

  entity.save!(touch: false)
  entity
end

.find_or_initialize(object_or_id) ⇒ ApplicationRecord?

Finds data from an object or its ID.

When data exists locally, the entity is returned. For distant data, a new instance is returned unless the target does not exist.

Parameters:

  • object_or_id (String, Hash)

    String identifier or incoming object

Returns:



14
15
16
17
18
19
20
21
# File 'lib/federails/utils/object.rb', line 14

def find_or_initialize(object_or_id)
  federated_url = object_or_id.is_a?(Hash) ? object_or_id['id'] : object_or_id

  route = local_route(federated_url)
  return from_local_route(route) if route

  from_distant_server(object_or_id)
end

.find_or_initialize!(object_or_id) ⇒ ApplicationRecord?

Finds or initializes an entity from an ActivityPub object or id

Parameters:

  • object_or_id (String, Hash)

    String identifier or incoming object

Returns:

Raises:

  • (ActiveRecord::RecordNotFound)

See Also:



55
56
57
58
59
60
# File 'lib/federails/utils/object.rb', line 55

def find_or_initialize!(object_or_id)
  entity = find_or_initialize object_or_id
  raise ActiveRecord::RecordNotFound unless entity

  entity
end

.timestamp_attributes(hash) ⇒ Hash

Returns the timestamps to use from an ActivityPub object

Parameters:

  • hash (Hash)

    ActivityPub object

Returns:

  • (Hash)

    Hash with timestamps



85
86
87
88
89
90
# File 'lib/federails/utils/object.rb', line 85

def timestamp_attributes(hash)
  {
    created_at: hash['published'] ||= Time.current,
    updated_at: hash['updated'].presence || hash['published'],
  }
end