Class: ActivityPub::ActorActivityHandler

Inherits:
Object
  • Object
show all
Defined in:
app/services/activity_pub/actor_activity_handler.rb

Class Method Summary collapse

Class Method Details

.actor_object_attributes(activity) ⇒ Object

This is copied from Fediverse::Webfinger - that needs refactoring so we can use it directly



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/services/activity_pub/actor_activity_handler.rb', line 37

def self.actor_object_attributes(activity)
  object = Fediverse::Request.dereference(activity["object"])

  # We only want to process actors
  return unless object["inbox"].present? && object["outbox"].present?

  id = object.delete("id")
  {
    federated_url: id,
    username: object.delete("preferredUsername"),
    name: object.delete("name"),
    server: server_and_port(id),
    inbox_url: object.delete("inbox"),
    outbox_url: object.delete("outbox"),
    followers_url: object.delete("followers"),
    followings_url: object.delete("following"),
    profile_url: object.delete("url"),
    public_key: object.delete("publicKey")&.dig("publicKeyPem"),
    extensions: object.except("@context")
  }
end

.get_actor(activity) ⇒ Object



27
28
29
30
31
32
33
# File 'app/services/activity_pub/actor_activity_handler.rb', line 27

def self.get_actor(activity)
  Federails::Actor.find_or_create_by_federation_url(
    activity["actor"].is_a?(Hash) ?
      activity.dig("actor", "id") :
      activity["actor"]
  )
end

.handle_activity(activity_hash_or_id, action) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/activity_pub/actor_activity_handler.rb', line 10

def self.handle_activity(activity_hash_or_id, action)
  activity = Fediverse::Request.dereference(activity_hash_or_id)

  # Get object attributes and update object
  attributes = actor_object_attributes(activity)
  return unless attributes
  object = Federails::Actor.find_or_create_by_federation_url(attributes[:federated_url]) # rubocop:disable Rails/DynamicFindBy
  return if object.local? # Don't update local objects, they must have already been done
  object&.update!(attributes)

  if object.entity
    ActivityPub::ApplicationDeserializer.deserializer_for(object)&.update!
  else
    ActivityPub::ApplicationDeserializer.deserializer_for(object)&.create!
  end
end

.handle_create_activity(activity_hash_or_id) ⇒ Object



2
3
4
# File 'app/services/activity_pub/actor_activity_handler.rb', line 2

def self.handle_create_activity(activity_hash_or_id)
  handle_activity(activity_hash_or_id, "Create")
end

.handle_update_activity(activity_hash_or_id) ⇒ Object



6
7
8
# File 'app/services/activity_pub/actor_activity_handler.rb', line 6

def self.handle_update_activity(activity_hash_or_id)
  handle_activity(activity_hash_or_id, "Update")
end

.server_and_port(string) ⇒ Object



59
60
61
62
63
64
65
66
# File 'app/services/activity_pub/actor_activity_handler.rb', line 59

def self.server_and_port(string)
  uri = URI.parse string
  if uri.port && [80, 443].exclude?(uri.port)
    "#{uri.host}:#{uri.port}"
  else
    uri.host
  end
end