fedipub 0.9.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -1
  3. data/app/controllers/concerns/fedipub/rails/server/render_collections.rb +21 -0
  4. data/app/controllers/fedipub/rails/client/activities_controller.rb +29 -0
  5. data/app/controllers/fedipub/rails/client/actors_controller.rb +56 -0
  6. data/app/controllers/fedipub/rails/client/followings_controller.rb +105 -0
  7. data/app/controllers/fedipub/rails/client_controller.rb +11 -0
  8. data/app/controllers/fedipub/rails/server/activities_controller.rb +80 -0
  9. data/app/controllers/fedipub/rails/server/actors_controller.rb +52 -0
  10. data/app/controllers/fedipub/rails/server/approval_stamps_controller.rb +15 -0
  11. data/app/controllers/fedipub/rails/server/followings_controller.rb +21 -0
  12. data/app/controllers/fedipub/rails/server/nodeinfo_controller.rb +30 -0
  13. data/app/controllers/fedipub/rails/server/published_controller.rb +32 -0
  14. data/app/controllers/fedipub/rails/server/web_finger_controller.rb +46 -0
  15. data/app/controllers/fedipub/rails/server_controller.rb +48 -0
  16. data/app/helpers/fedipub/rails/server_helper.rb +16 -0
  17. data/app/jobs/fedipub/application_job.rb +1 -1
  18. data/app/models/concerns/fedipub/actor_entity.rb +1 -125
  19. data/app/models/concerns/fedipub/data_entity.rb +5 -105
  20. data/app/models/fedipub/activity.rb +1 -0
  21. data/app/models/fedipub/actor.rb +28 -22
  22. data/app/models/fedipub/approval_stamp.rb +38 -0
  23. data/app/models/fedipub/host.rb +2 -2
  24. data/app/policies/fedipub/server/approval_stamp_policy.rb +9 -0
  25. data/app/views/fedipub/rails/client/activities/_index.json.jbuilder +1 -0
  26. data/app/views/fedipub/rails/client/activities/feed.json.jbuilder +1 -0
  27. data/app/views/fedipub/rails/client/activities/index.json.jbuilder +1 -0
  28. data/app/views/fedipub/{client → rails/client}/actors/_actor.json.jbuilder +1 -0
  29. data/app/views/fedipub/rails/client/actors/index.json.jbuilder +1 -0
  30. data/app/views/fedipub/{client → rails/client}/actors/show.html.erb +10 -5
  31. data/app/views/fedipub/rails/client/actors/show.json.jbuilder +1 -0
  32. data/app/views/fedipub/rails/client/followings/index.json.jbuilder +1 -0
  33. data/app/views/fedipub/rails/client/followings/show.json.jbuilder +1 -0
  34. data/app/views/fedipub/{server → rails/server}/activities/_activity.activitypub.jbuilder +2 -2
  35. data/app/views/fedipub/rails/server/activities/show.activitypub.jbuilder +1 -0
  36. data/app/views/fedipub/{server → rails/server}/actors/_actor.activitypub.jbuilder +14 -7
  37. data/app/views/fedipub/{server → rails/server}/actors/_tombstone.activitypub.jbuilder +1 -1
  38. data/app/views/fedipub/rails/server/actors/show.activitypub.jbuilder +5 -0
  39. data/app/views/fedipub/rails/server/approval_stamps/show.activitypub.jbuilder +8 -0
  40. data/app/views/fedipub/{server → rails/server}/followings/_following.activitypub.jbuilder +1 -1
  41. data/app/views/fedipub/rails/server/followings/show.activitypub.jbuilder +1 -0
  42. data/app/views/fedipub/rails/server/nodeinfo/index.nodeinfo.jbuilder +10 -0
  43. data/app/views/fedipub/rails/server/published/_publishable.activitypub.jbuilder +20 -0
  44. data/app/views/fedipub/{server → rails/server}/published/_tombstone.activitypub.jbuilder +1 -1
  45. data/app/views/fedipub/rails/server/published/show.activitypub.jbuilder +5 -0
  46. data/app/views/fedipub/{server → rails/server}/shared/ordered_collection.activitypub.jbuilder +1 -1
  47. data/app/views/fedipub/{server → rails/server}/shared/ordered_collection_page.activitypub.jbuilder +1 -1
  48. data/app/views/fedipub/{server → rails/server}/web_finger/find.jrd.jbuilder +11 -3
  49. data/config/initializers/logger.rb +2 -0
  50. data/config/initializers/miscellany.rb +21 -0
  51. data/config/initializers/security_vocab.rb +1 -0
  52. data/config/routes.rb +8 -7
  53. data/db/migrate/20260922155524_create_fedipub_approval_stamps.rb +12 -0
  54. data/db/migrate/20260924090802_add_shared_inbox_url_to_fedipub_actors.rb +5 -0
  55. data/lib/fedipub/activity_handlers/accept_follow_handler.rb +17 -0
  56. data/lib/fedipub/activity_handlers/delete_handler.rb +13 -0
  57. data/lib/fedipub/activity_handlers/follow_handler.rb +13 -0
  58. data/lib/fedipub/activity_handlers/quote_request_handler.rb +33 -0
  59. data/lib/fedipub/activity_handlers/undo_delete_handler.rb +15 -0
  60. data/lib/fedipub/activity_handlers/undo_follow_handler.rb +16 -0
  61. data/lib/fedipub/concerns/announceable.rb +23 -0
  62. data/lib/fedipub/concerns/application_actor.rb +30 -0
  63. data/lib/fedipub/concerns/generic_actor_entity.rb +135 -0
  64. data/lib/fedipub/concerns/generic_data_entity.rb +112 -0
  65. data/lib/fedipub/concerns/handles_delete_requests.rb +42 -0
  66. data/lib/fedipub/concerns/has_interaction_policy.rb +85 -0
  67. data/lib/fedipub/concerns/likeable.rb +34 -0
  68. data/lib/fedipub/concerns/quote_request.rb +42 -0
  69. data/lib/fedipub/data_transformer/note.rb +3 -2
  70. data/lib/fedipub/errors.rb +10 -0
  71. data/lib/fedipub/features.rb +13 -0
  72. data/lib/fedipub/logger.rb +42 -0
  73. data/lib/fedipub/utils/context.rb +30 -5
  74. data/lib/fedipub/utils/host.rb +1 -1
  75. data/lib/fedipub/utils/json_request.rb +63 -17
  76. data/lib/fedipub/utils/object.rb +1 -1
  77. data/lib/fedipub/version.rb +1 -1
  78. data/lib/fedipub.rb +14 -1
  79. data/lib/fediverse/collection.rb +1 -5
  80. data/lib/fediverse/inbox.rb +16 -51
  81. data/lib/fediverse/node_info.rb +2 -5
  82. data/lib/fediverse/notifier.rb +12 -39
  83. data/lib/fediverse/signature/draft_cavage12.rb +109 -0
  84. data/lib/fediverse/signature/rfc9421.rb +69 -0
  85. data/lib/fediverse/signature.rb +14 -39
  86. data/lib/fediverse/webfinger.rb +19 -18
  87. data/lib/generators/fedipub/copy_client_policies/USAGE +1 -1
  88. data/lib/generators/fedipub/copy_client_policies/copy_client_policies_generator.rb +2 -2
  89. data/lib/generators/fedipub/copy_client_views/USAGE +1 -1
  90. data/lib/generators/fedipub/copy_client_views/copy_client_views_generator.rb +2 -2
  91. data/lib/generators/fedipub/copy_factories/copy_factories_generator.rb +2 -2
  92. data/lib/generators/fedipub/install/install_generator.rb +3 -3
  93. metadata +109 -61
  94. data/app/assets/config/fedipub_manifest.js +0 -1
  95. data/app/assets/stylesheets/fedipub/application.css +0 -15
  96. data/app/controllers/concerns/fedipub/server/render_collections.rb +0 -19
  97. data/app/controllers/fedipub/client/activities_controller.rb +0 -27
  98. data/app/controllers/fedipub/client/actors_controller.rb +0 -54
  99. data/app/controllers/fedipub/client/followings_controller.rb +0 -103
  100. data/app/controllers/fedipub/client_controller.rb +0 -9
  101. data/app/controllers/fedipub/server/activities_controller.rb +0 -78
  102. data/app/controllers/fedipub/server/actors_controller.rb +0 -50
  103. data/app/controllers/fedipub/server/followings_controller.rb +0 -19
  104. data/app/controllers/fedipub/server/nodeinfo_controller.rb +0 -28
  105. data/app/controllers/fedipub/server/published_controller.rb +0 -30
  106. data/app/controllers/fedipub/server/web_finger_controller.rb +0 -45
  107. data/app/controllers/fedipub/server_controller.rb +0 -37
  108. data/app/helpers/fedipub/server_helper.rb +0 -18
  109. data/app/mailers/fedipub/application_mailer.rb +0 -6
  110. data/app/models/concerns/fedipub/announceable.rb +0 -18
  111. data/app/models/concerns/fedipub/handles_delete_requests.rb +0 -36
  112. data/app/models/concerns/fedipub/likeable.rb +0 -29
  113. data/app/views/fedipub/client/activities/_index.json.jbuilder +0 -1
  114. data/app/views/fedipub/client/activities/feed.json.jbuilder +0 -1
  115. data/app/views/fedipub/client/activities/index.json.jbuilder +0 -1
  116. data/app/views/fedipub/client/actors/index.json.jbuilder +0 -1
  117. data/app/views/fedipub/client/actors/show.json.jbuilder +0 -1
  118. data/app/views/fedipub/client/followings/index.json.jbuilder +0 -1
  119. data/app/views/fedipub/client/followings/show.json.jbuilder +0 -1
  120. data/app/views/fedipub/server/activities/show.activitypub.jbuilder +0 -1
  121. data/app/views/fedipub/server/actors/show.activitypub.jbuilder +0 -5
  122. data/app/views/fedipub/server/followings/show.activitypub.jbuilder +0 -1
  123. data/app/views/fedipub/server/nodeinfo/index.nodeinfo.jbuilder +0 -6
  124. data/app/views/fedipub/server/published/_publishable.activitypub.jbuilder +0 -11
  125. data/app/views/fedipub/server/published/show.activitypub.jbuilder +0 -5
  126. data/app/views/users/show.html.erb +0 -4
  127. /data/app/views/fedipub/{client → rails/client}/activities/_activity.html.erb +0 -0
  128. /data/app/views/fedipub/{client → rails/client}/activities/_activity.json.jbuilder +0 -0
  129. /data/app/views/fedipub/{client → rails/client}/activities/feed.html.erb +0 -0
  130. /data/app/views/fedipub/{client → rails/client}/activities/index.html.erb +0 -0
  131. /data/app/views/fedipub/{client → rails/client}/actors/_lookup_form.html.erb +0 -0
  132. /data/app/views/fedipub/{client → rails/client}/actors/gone.html.erb +0 -0
  133. /data/app/views/fedipub/{client → rails/client}/actors/index.html.erb +0 -0
  134. /data/app/views/fedipub/{client → rails/client}/common/_client_links.html.erb +0 -0
  135. /data/app/views/fedipub/{client → rails/client}/followings/_follow.html.erb +0 -0
  136. /data/app/views/fedipub/{client → rails/client}/followings/_follow_actions.html.erb +0 -0
  137. /data/app/views/fedipub/{client → rails/client}/followings/_follower.html.erb +0 -0
  138. /data/app/views/fedipub/{client → rails/client}/followings/_following.json.jbuilder +0 -0
  139. /data/app/views/fedipub/{client → rails/client}/followings/_form.html.erb +0 -0
  140. /data/app/views/fedipub/{client → rails/client}/followings/index.html.erb +0 -0
  141. /data/app/views/fedipub/{client → rails/client}/followings/show.html.erb +0 -0
  142. /data/app/views/fedipub/{server → rails/server}/nodeinfo/show.nodeinfo.jbuilder +0 -0
  143. /data/app/views/fedipub/{server → rails/server}/web_finger/host_meta.xrd.erb +0 -0
@@ -1,5 +1,5 @@
1
1
  context = true unless context == false
2
- set_json_ld_context(json) if context
2
+ Fedipub::Utils::Context.new.to_jbuilder(json) if context
3
3
 
4
4
  json.id following.federated_url
5
5
  json.type 'Follow'
@@ -0,0 +1 @@
1
+ json.partial! 'fedipub/rails/server/followings/following', following: @following
@@ -0,0 +1,10 @@
1
+ json.links [
2
+ {
3
+ rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
4
+ href: show_node_info_url,
5
+ },
6
+ {
7
+ rel: 'https://www.w3.org/ns/activitystreams#Application',
8
+ href: Fedipub::Actor.application_actor.federated_url,
9
+ },
10
+ ]
@@ -0,0 +1,20 @@
1
+ context = true unless context == false
2
+ interaction_policy = publishable.try(:interaction_policy)
3
+ content = publishable.to_activitypub_object
4
+
5
+ if context
6
+ ctx = Fedipub::Utils::Context.new
7
+ ctx.merge!(interaction_policy.delete('@context') || interaction_policy.delete(:@context)) if interaction_policy
8
+ ctx.merge!(content.delete('@context') || content.delete(:@context))
9
+ ctx.to_jbuilder(json) if context
10
+ end
11
+
12
+ content.each_pair do |key, value|
13
+ json.set! key, value
14
+ end
15
+
16
+ json.id publishable.federated_url
17
+ json.actor publishable.fedipub_actor.federated_url
18
+ json.to [Fediverse::Collection::PUBLIC]
19
+ json.cc [publishable.fedipub_actor.followers_url]
20
+ json.interactionPolicy interaction_policy if interaction_policy
@@ -1,4 +1,4 @@
1
- set_json_ld_context(json)
1
+ Fedipub::Utils::Context.new.to_jbuilder(json)
2
2
 
3
3
  json.id publishable.federated_url
4
4
  json.type 'Tombstone'
@@ -0,0 +1,5 @@
1
+ if @publishable.fedipub_tombstoned?
2
+ json.partial! 'fedipub/rails/server/published/tombstone', publishable: @publishable
3
+ else
4
+ json.partial! 'fedipub/rails/server/published/publishable', publishable: @publishable
5
+ end
@@ -1,4 +1,4 @@
1
- set_json_ld_context(json)
1
+ Fedipub::Utils::Context.new.to_jbuilder(json)
2
2
  json.id send(url_helper, actor)
3
3
  json.type 'OrderedCollection'
4
4
  json.totalItems collection.total_count
@@ -1,4 +1,4 @@
1
- set_json_ld_context(json)
1
+ Fedipub::Utils::Context.new.to_jbuilder(json)
2
2
  json.type 'OrderedCollectionPage'
3
3
  json.id send(url_helper, actor, page: collection.current_page)
4
4
  json.partOf send(url_helper, actor)
@@ -5,16 +5,24 @@ links = [
5
5
  {
6
6
  rel: 'self',
7
7
  type: Mime[:activitypub].to_s,
8
- href: @user.fedipub_actor.federated_url,
8
+ href: @actor.federated_url,
9
9
  },
10
10
  ]
11
11
 
12
+ if @actor.application_actor?
13
+ links << {
14
+ rel: 'https://www.w3.org/ns/activitystreams#Service',
15
+ type: Mime[:activitypub].to_s,
16
+ href: @actor.federated_url,
17
+ }
18
+ end
19
+
12
20
  # User profile URL if configured
13
21
  # TODO: Add a profile controller/action in dummy to test this
14
- if @user.fedipub_actor.profile_url
22
+ if @actor.profile_url
15
23
  links.push rel: 'https://webfinger.net/rel/profile-page',
16
24
  type: 'text/html',
17
- href: @user.fedipub_actor.profile_url
25
+ href: @actor.profile_url
18
26
  end
19
27
 
20
28
  # Remote following
@@ -0,0 +1,2 @@
1
+ # Automatically use default Rails logger for Fedipub
2
+ Fedipub.logger = Rails.logger
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file generated automatically from https://purl.archive.org/miscellany
4
+ require 'json/ld'
5
+ module JSON
6
+ module LD
7
+ class Context
8
+ add_preloaded('https://purl.archive.org/miscellany') do
9
+ new(processingMode: 'json-ld-1.1', term_definitions: {
10
+ 'Hashtag' => TermDefinition.new('Hashtag', id: 'https://www.w3.org/ns/activitystreams#Hashtag', simple: true),
11
+ 'as' => TermDefinition.new('as', id: 'https://www.w3.org/ns/activitystreams#', simple: true, prefix: true),
12
+ 'manuallyApprovesFollowers' => TermDefinition.new('manuallyApprovesFollowers', id: 'https://www.w3.org/ns/activitystreams#manuallyApprovesFollowers'),
13
+ 'movedTo' => TermDefinition.new('movedTo', id: 'https://www.w3.org/ns/activitystreams#movedTo', type_mapping: '@id'),
14
+ 'sensitive' => TermDefinition.new('sensitive', id: 'https://www.w3.org/ns/activitystreams#sensitive'),
15
+ 'xsd' => TermDefinition.new('xsd', id: 'http://www.w3.org/2001/XMLSchema#', simple: true, prefix: true),
16
+ })
17
+ end
18
+ alias_preloaded('http://purl.archive.org/miscellany', 'https://purl.archive.org/miscellany')
19
+ end
20
+ end
21
+ end
@@ -50,4 +50,5 @@ class JSON::LD::Context # rubocop:disable Style/ClassAndModuleChildren
50
50
  'xsd' => TermDefinition.new('xsd', id: 'http://www.w3.org/2001/XMLSchema#', simple: true, prefix: true),
51
51
  })
52
52
  end
53
+ alias_preloaded('https://w3id.org/security/v1', 'http://w3id.org/security/v1')
53
54
  end
data/config/routes.rb CHANGED
@@ -1,15 +1,15 @@
1
1
  Fedipub::Engine.routes.draw do
2
2
  if Fedipub.configuration.enable_discovery
3
3
  scope path: '/' do
4
- get '/.well-known/webfinger', to: 'server/web_finger#find', as: :webfinger
5
- get '/.well-known/host-meta', to: 'server/web_finger#host_meta', as: :host_meta
6
- get '/.well-known/nodeinfo', to: 'server/nodeinfo#index', as: :node_info
7
- get '/nodeinfo/2.0', to: 'server/nodeinfo#show', as: :show_node_info
4
+ get '/.well-known/webfinger', to: 'rails/server/web_finger#find', as: :webfinger
5
+ get '/.well-known/host-meta', to: 'rails/server/web_finger#host_meta', as: :host_meta
6
+ get '/.well-known/nodeinfo', to: 'rails/server/nodeinfo#index', as: :node_info
7
+ get '/nodeinfo/2.0', to: 'rails/server/nodeinfo#show', as: :show_node_info
8
8
  end
9
9
  end
10
10
 
11
11
  if Fedipub.configuration.client_routes_path
12
- scope Fedipub.configuration.client_routes_path, module: :client, as: :client do
12
+ scope Fedipub.configuration.client_routes_path, module: 'rails/client', as: :client do
13
13
  resources :activities, only: [:index] do
14
14
  collection do
15
15
  get :feed, to: 'activities#feed'
@@ -34,7 +34,7 @@ Fedipub::Engine.routes.draw do
34
34
  end
35
35
  end
36
36
 
37
- scope Fedipub.configuration.server_routes_path, module: :server, as: :server, defaults: { format: :activitypub } do
37
+ scope Fedipub.configuration.server_routes_path, module: 'rails/server', as: :server, defaults: { format: :activitypub } do
38
38
  resources :actors, only: [:show] do
39
39
  member do
40
40
  get :followers
@@ -45,9 +45,10 @@ Fedipub::Engine.routes.draw do
45
45
  resources :activities, only: [:show]
46
46
  resources :followings, only: [:show]
47
47
  end
48
-
48
+ resources :approval_stamps, only: [:show]
49
49
  scope :published do
50
50
  get ':publishable_type/:id', to: 'published#show', as: :published
51
51
  end
52
+ post :inbox, to: 'activities#create', as: :shared_inbox
52
53
  end
53
54
  end
@@ -0,0 +1,12 @@
1
+ class CreateFedipubApprovalStamps < ActiveRecord::Migration[7.2]
2
+ def change
3
+ create_table :fedipub_approval_stamps do |t|
4
+ t.string :uuid, null: false
5
+ t.string :approval_type, null: false
6
+ t.string :interacting_object_url, null: false
7
+ t.belongs_to :interaction_target, polymorphic: true, null: false
8
+ t.belongs_to :fedipub_actor, null: false, foreign_key: true
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ class AddSharedInboxUrlToFedipubActors < ActiveRecord::Migration[7.2]
2
+ def change
3
+ add_column :fedipub_actors, :shared_inbox_url, :string
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ # Processes incoming Accept activities for Follows
2
+ module Fedipub
3
+ module ActivityHandlers
4
+ class AcceptFollowHandler
5
+ def self.perform(activity)
6
+ original_activity = Fediverse::Request.dereference(activity['object'])
7
+
8
+ actor = Fedipub::Actor.find_or_create_by_object original_activity['actor']
9
+ target_actor = Fedipub::Actor.find_or_create_by_object original_activity['object']
10
+ raise 'Follow not accepted by target actor but by someone else' if activity['actor'] != target_actor.federated_url
11
+
12
+ follow = Fedipub::Following.find_by actor: actor, target_actor: target_actor
13
+ follow.accept!
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ # Processes incoming Delete activities
2
+ module Fedipub
3
+ module ActivityHandlers
4
+ class DeleteHandler
5
+ def self.perform(activity)
6
+ object = Fedipub::Utils::Object.find_distant_object_in_all(activity['object'])
7
+ return if object.blank?
8
+
9
+ object.run_callbacks :on_fedipub_delete_requested
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # Processes incoming Follow activities
2
+ module Fedipub
3
+ module ActivityHandlers
4
+ class FollowHandler
5
+ def self.perform(activity)
6
+ actor = Fedipub::Actor.find_or_create_by_object activity['actor']
7
+ target_actor = Fedipub::Actor.find_or_create_by_object activity['object']
8
+
9
+ Fedipub::Following.create! actor: actor, target_actor: target_actor, federated_url: activity['id']
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,33 @@
1
+ # Processes incoming QuoteRequest activities
2
+ #
3
+ # We automatically create persisted activities for these, because approval is
4
+ # needed and that might be immediate, or might be interactive later on.
5
+ #
6
+ # See https://w3id.org/fep/044f for protocol details
7
+ module Fedipub
8
+ module ActivityHandlers
9
+ class QuoteRequestHandler
10
+ def self.perform(activity)
11
+ activity_hash = Fediverse::Request.dereference(activity)
12
+
13
+ # Get the objects and actors involved
14
+ quoting_actor = Fedipub::Actor.find_or_create_by_object activity_hash[:actor]
15
+ object = Fedipub::Utils::Object.find_or_initialize(activity_hash[:object])
16
+ return unless object&.persisted? && quoting_actor&.persisted?
17
+
18
+ # Store the activity
19
+ activity = object.send(:create_fedipub_activity,
20
+ 'QuoteRequest',
21
+ actor: quoting_actor,
22
+ to: activity_hash[:to],
23
+ cc: activity_hash[:cc],
24
+ instrument: activity_hash[:instrument])
25
+
26
+ # Inform the object that there's a new quote request
27
+ object.try(:on_new_quote_request, activity) if activity.persisted?
28
+
29
+ activity
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,15 @@
1
+ # Processes incoming Undo activities for Delete activities
2
+ module Fedipub
3
+ module ActivityHandlers
4
+ class UndoDeleteHandler
5
+ def self.perform(activity)
6
+ # Get to original object
7
+ delete_activity = Fediverse::Request.dereference(activity['object'])
8
+ object = Fedipub::Utils::Object.find_distant_object_in_all(delete_activity['object'])
9
+ return if object.blank?
10
+
11
+ object.run_callbacks :on_fedipub_undelete_requested
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ # Processes incoming Undo activities for Follow activities
2
+ module Fedipub
3
+ module ActivityHandlers
4
+ class UndoFollowHandler
5
+ def self.perform(activity)
6
+ original_activity = activity['object']
7
+
8
+ actor = Fedipub::Actor.find_or_create_by_object original_activity['actor']
9
+ target_actor = Fedipub::Actor.find_or_create_by_object original_activity['object']
10
+
11
+ follow = Fedipub::Following.find_by actor: actor, target_actor: target_actor
12
+ follow&.destroy
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ require 'active_support'
2
+ require 'active_support/concern'
3
+
4
+ module Fedipub
5
+ module Concerns
6
+ module Announceable
7
+ extend ActiveSupport::Concern
8
+
9
+ # Announces (boosts) this entity by creating a new Activity.
10
+ #
11
+ # @param actor [Fedipub::Actor] The actor doing the announce; defaults to the entity's own actor.
12
+ #
13
+ # @return [Fedipub::Activity] the newly-created Announce activity
14
+ def announce!(actor: nil)
15
+ actor ||= try(:fedipub_actor) || self
16
+ create_fedipub_activity('Announce',
17
+ actor: actor,
18
+ to: [Fediverse::Collection::PUBLIC],
19
+ cc: [actor.followers_url])
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,30 @@
1
+ require 'active_support'
2
+ require 'active_support/concern'
3
+
4
+ module Fedipub
5
+ module Concerns
6
+ # Definition for a single local Application Actor, which represents the instance/server itself.
7
+ #
8
+ # Keys from this actor are used to sign outgoing GET requests.
9
+ module ApplicationActor
10
+ extend ActiveSupport::Concern
11
+
12
+ APPLICATION_ACTOR_ATTRIBUTES = {
13
+ username: '__application',
14
+ actor_type: 'Application',
15
+ entity: nil,
16
+ local: true,
17
+ }.freeze
18
+
19
+ included do
20
+ def self.application_actor
21
+ find_or_create_by!(APPLICATION_ACTOR_ATTRIBUTES)
22
+ end
23
+
24
+ def application_actor?
25
+ APPLICATION_ACTOR_ATTRIBUTES.all? { |attribute, value| attributes[attribute.to_s] == value }
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,135 @@
1
+ module Fedipub
2
+ module Concerns
3
+ # Concern to include in models that acts as actors.
4
+ #
5
+ # Do not include directly; use Fedipub::ActorEntity instead, which includes this and adds
6
+ # ActiveRecord and Rails-specific code
7
+ module GenericActorEntity
8
+ extend ActiveSupport::Concern
9
+
10
+ # Class methods automatically included in the concern.
11
+ module ClassMethods
12
+ # Configures the mapping between entity and actor
13
+ #
14
+ # @param username_field [Symbol] The method or attribute name that returns the preferred username for ActivityPub
15
+ # @param name_field [Symbol] The method or attribute name that returns the preferred name for ActivityPub
16
+ # @param profile_url_method [Symbol] The route method name that will generate the profile URL for ActivityPub
17
+ # @param actor_type [String] The ActivityStreams Actor type for this entity; defaults to 'Person'
18
+ # @param user_count_method [Symbol] A class method to call to count active users. Leave unspecified to leave this
19
+ # entity out of user counts. Method signature should accept a single parameter which will specify a date range
20
+ # If parameter is nil, the total user count should be returned. If the parameter is specified, the number of users
21
+ # active during the time period should be returned.
22
+ # @param auto_create_actors [Boolean] Whether to automatically create an actor when the entity is created
23
+ #
24
+ # @example
25
+ # acts_as_fedipub_actor username_field: :username, name_field: :display_name, profile_url_method: :url_for, actor_type: 'Person'
26
+ # rubocop:disable-next Metrics/ParameterLists
27
+ def acts_as_fedipub_actor(
28
+ name_field:,
29
+ username_field:,
30
+ profile_url_method: nil,
31
+ actor_type: 'Person',
32
+ user_count_method: nil,
33
+ auto_create_actors: true
34
+ )
35
+ Fedipub::Configuration.register_actor_class(
36
+ self,
37
+ username_field: username_field,
38
+ name_field: name_field,
39
+ profile_url_method: profile_url_method,
40
+ actor_type: actor_type,
41
+ user_count_method: user_count_method,
42
+ auto_create_actors: auto_create_actors
43
+ )
44
+ end
45
+
46
+ # Define a method that will be called after the entity receives a follow request.
47
+ # The follow request will be passed as an argument to the method.
48
+ #
49
+ # @param method_name [Symbol] The name of the method to call, or a block that will be called directly
50
+ #
51
+ # @example
52
+ # after_followed :accept_follow
53
+ def after_followed(method_name)
54
+ @after_followed = method_name
55
+ end
56
+
57
+ # Define a method that will be called after a follow request made by the entity is accepted
58
+ # The accepted follow request will be passed as an argument to the method.
59
+ #
60
+ # @param method_name [Symbol] The name of the method to call, or a block that will be called directly
61
+ #
62
+ # @example
63
+ # after_follow_accepted :follow_accepted
64
+ def after_follow_accepted(method_name)
65
+ @after_follow_accepted = method_name
66
+ end
67
+
68
+ # Define a method that will be called after an activity has been received
69
+ #
70
+ # @param activity_type [String] The activity action to handle, e.g. 'Create'. If you specify '*', the handler will be called for any activity type.
71
+ # @param object_type [String] The object type to handle, e.g. 'Note'. If you specify '*', the handler will be called for any object type.
72
+ # @param method_name [Symbol] The name of the class method to call. The method will receive the complete activity payload as a parameter.
73
+ #
74
+ # @example
75
+ # after_activity_received 'Create', 'Note', :create_note
76
+ def after_activity_received(activity_type, object_type, method_name)
77
+ Fediverse::Inbox.register_handler(activity_type, object_type, self, method_name)
78
+ end
79
+
80
+ private
81
+
82
+ def dispatch_callback(name, instance, *)
83
+ case name
84
+ when :after_followed
85
+ instance.send(@after_followed, *) if @after_followed
86
+ when :after_follow_accepted
87
+ instance.send(@after_follow_accepted, *) if @after_follow_accepted
88
+ end
89
+ end
90
+ end
91
+
92
+ # Add custom data to actor responses.
93
+ #
94
+ # Override in your own model to add extra data, which will be merged into the actor response
95
+ # generated by Fedipub. You can include extra `@context` for activitypub extensions and it will
96
+ # be merged with the main response context.
97
+ #
98
+ # @example
99
+ # def to_activitypub_object
100
+ # {
101
+ # "@context": {
102
+ # toot: "http://joinmastodon.org/ns#",
103
+ # attributionDomains: {
104
+ # "@id": "toot:attributionDomains",
105
+ # "@type": "@id"
106
+ # }
107
+ # },
108
+ # attributionDomains: [
109
+ # "example.com"
110
+ # ]
111
+ # }
112
+ # end
113
+ def to_activitypub_object
114
+ {}
115
+ end
116
+
117
+ private
118
+
119
+ # Result is used to determine if an actor related to this entity should be created as local actor or not
120
+ #
121
+ # Override it in your models if you need distant actors to be related to another entity.
122
+ def create_fedipub_actor_as_local?
123
+ true
124
+ end
125
+
126
+ def tombstone_fedipub_actor!
127
+ fedipub_actor.presence&.tombstone!
128
+ end
129
+
130
+ def untombstone_fedipub_actor!
131
+ fedipub_actor.presence&.untombstone!
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,112 @@
1
+ module Fedipub
2
+ module Concerns
3
+ # Concern to include in models that acts as data entities.
4
+ #
5
+ # Do not include directly; use Fedipub::DataEntity instead, which includes this and adds
6
+ # ActiveRecord and Rails-specific code
7
+ module GenericDataEntity
8
+ extend ActiveSupport::Concern
9
+ include Fedipub::Concerns::HandlesDeleteRequests
10
+ include Fedipub::Concerns::Likeable
11
+ include Fedipub::Concerns::Announceable
12
+
13
+ # Class methods automatically included in the concern.
14
+ module ClassMethods
15
+ # Configures the mapping between entity and Fediverse
16
+ #
17
+ # @param actor_entity_method [Symbol] Method returning an object responding to 'fedipub_actor', for local content
18
+ # @param url_param [Symbol] Column name of the object ID that should be used in URLs. Defaults to +:id+
19
+ # @param route_path_segment [Symbol] Segment used in Fedipub routes to display the ActivityPub representation.
20
+ # Defaults to the pluralized, underscored class name
21
+ # @param handles [String] Type of ActivityPub object handled by this entity type
22
+ # @param with [Symbol] Self class method that will handle incoming objects. Defaults to +:handle_incoming_fediverse_data+
23
+ # @param filter_method [Symbol] Self class method that determines if an incoming object should be handled. Note
24
+ # that the first model for which this method returns true will be used. If left empty, the model CAN be selected,
25
+ # so define them if many models handle the same data type.
26
+ # @param should_federate_method [Symbol] method to determine if an object should be federated. If the method returns false,
27
+ # no create/update activities will happen, and object will not be accessible at federated_url. Defaults to a method
28
+ # that always returns true.
29
+ # @param soft_deleted_method [Symbol, nil] If the model uses a soft-delete mechanism, this is the method to check
30
+ # if entity is soft-deleted. This is not required by the spec but greatly encouraged as the app will return a 410
31
+ # response with a Tombstone object instead of an 404 error.
32
+ # @param soft_delete_date_method [Symbol, nil] Method to get the date of the soft-deletion
33
+ #
34
+ # @example
35
+ # acts_as_fedipub_data handles: 'Note', with: :note_handler, route_path_segment: :articles, actor_entity_method: :user
36
+ # rubocop:disable-next Metrics/ParameterLists, Metrics/MethodLength
37
+ def acts_as_fedipub_data(
38
+ handles:,
39
+ with: :handle_incoming_fediverse_data,
40
+ route_path_segment: nil,
41
+ actor_entity_method: nil,
42
+ url_param: :id,
43
+ filter_method: nil,
44
+ should_federate_method: :default_should_federate?,
45
+ soft_deleted_method: nil,
46
+ soft_delete_date_method: nil
47
+ )
48
+ route_path_segment ||= name.pluralize.underscore
49
+
50
+ Fedipub::Configuration.register_data_type self,
51
+ route_path_segment: route_path_segment,
52
+ actor_entity_method: actor_entity_method,
53
+ url_param: url_param,
54
+ handles: handles,
55
+ with: with,
56
+ filter_method: filter_method,
57
+ should_federate_method: should_federate_method,
58
+ soft_deleted_method: soft_deleted_method,
59
+ soft_delete_date_method: soft_delete_date_method
60
+
61
+ # NOTE: Delete activities cannot be handled like this as we can't be sure to have the object's type
62
+ Fediverse::Inbox.register_handler 'Create', handles, self, with
63
+ Fediverse::Inbox.register_handler 'Update', handles, self, with
64
+ end
65
+
66
+ # Instantiates a new instance from an ActivityPub object
67
+ #
68
+ # @param activitypub_object [Hash]
69
+ #
70
+ # @return [self]
71
+ def new_from_activitypub_object(activitypub_object)
72
+ new from_activitypub_object(activitypub_object)
73
+ end
74
+ end
75
+
76
+ # Check whether the entity was created locally or comes from the Fediverse
77
+ #
78
+ # @return [Boolean]
79
+ def local_fedipub_entity?
80
+ attributes['federated_url'].blank?
81
+ end
82
+
83
+ def fedipub_tombstoned?
84
+ fedipub_data_configuration[:soft_deleted_method] ? send(fedipub_data_configuration[:soft_deleted_method]) : false
85
+ end
86
+
87
+ def fedipub_tombstoned_at
88
+ fedipub_data_configuration[:soft_delete_date_method] ? send(fedipub_data_configuration[:soft_delete_date_method]) : nil
89
+ end
90
+
91
+ def fedipub_data_configuration
92
+ Fedipub.data_entity_configuration(self)
93
+ end
94
+
95
+ def set_fedipub_actor
96
+ return fedipub_actor if fedipub_actor.present?
97
+
98
+ self.fedipub_actor = send(fedipub_data_configuration[:actor_entity_method])&.fedipub_actor if fedipub_data_configuration[:actor_entity_method]
99
+
100
+ raise 'Cannot determine actor from configuration' unless fedipub_actor
101
+ end
102
+
103
+ def ensure_fedipub_configuration!
104
+ raise("Entity not configured for #{self.class.name}. Did you use \"acts_as_fedipub_data\"?") unless Fedipub.data_entity? self
105
+ end
106
+
107
+ def default_should_federate?
108
+ true
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,42 @@
1
+ require 'active_support'
2
+ require 'active_support/concern'
3
+ require 'active_support/callbacks'
4
+
5
+ module Fedipub
6
+ module Concerns
7
+ # Model concern providing hooks for on_fedipub_delete_requested callback
8
+ #
9
+ # ```rb
10
+ # # Example migration
11
+ # add_column :my_table, :uuid, :text, default: nil, index: { unique: true }
12
+ # ```
13
+ #
14
+ # Usage:
15
+ #
16
+ # ```rb
17
+ # class MyModel < ApplicationRecord
18
+ # include Fedipub::Concerns::HandlesDeleteRequests
19
+ #
20
+ # on_fedipub_delete_requested -> { delete! }
21
+ # end
22
+ module HandlesDeleteRequests
23
+ extend ActiveSupport::Concern
24
+
25
+ # Class methods automatically included in the concern.
26
+ module ClassMethods
27
+ def on_fedipub_delete_requested(*)
28
+ set_callback(:on_fedipub_delete_requested, *)
29
+ end
30
+
31
+ def on_fedipub_undelete_requested(*)
32
+ set_callback(:on_fedipub_undelete_requested, *)
33
+ end
34
+ end
35
+
36
+ included do
37
+ define_callbacks :on_fedipub_delete_requested
38
+ define_callbacks :on_fedipub_undelete_requested
39
+ end
40
+ end
41
+ end
42
+ end