federails 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +171 -7
  3. data/Rakefile +5 -5
  4. data/app/controllers/federails/application_controller.rb +19 -0
  5. data/app/controllers/federails/client/activities_controller.rb +21 -0
  6. data/app/controllers/federails/client/actors_controller.rb +37 -0
  7. data/app/controllers/federails/client/followings_controller.rb +93 -0
  8. data/app/controllers/federails/server/activities_controller.rb +65 -0
  9. data/app/controllers/federails/server/actors_controller.rb +34 -0
  10. data/app/controllers/federails/server/followings_controller.rb +18 -0
  11. data/app/controllers/federails/server/nodeinfo_controller.rb +22 -0
  12. data/app/controllers/federails/server/server_controller.rb +17 -0
  13. data/app/controllers/federails/server/web_finger_controller.rb +38 -0
  14. data/app/jobs/federails/notify_inbox_job.rb +12 -0
  15. data/app/mailers/federails/application_mailer.rb +2 -2
  16. data/app/models/concerns/federails/entity.rb +46 -0
  17. data/app/models/federails/activity.rb +40 -0
  18. data/app/models/federails/actor.rb +152 -0
  19. data/app/models/federails/following.rb +43 -0
  20. data/app/policies/federails/client/activity_policy.rb +6 -0
  21. data/app/policies/federails/client/actor_policy.rb +15 -0
  22. data/app/policies/federails/client/following_policy.rb +35 -0
  23. data/app/policies/federails/federails_policy.rb +59 -0
  24. data/app/policies/federails/server/activity_policy.rb +6 -0
  25. data/app/policies/federails/server/actor_policy.rb +23 -0
  26. data/app/policies/federails/server/following_policy.rb +6 -0
  27. data/app/views/federails/client/activities/_activity.html.erb +5 -0
  28. data/app/views/federails/client/activities/_activity.json.jbuilder +1 -0
  29. data/app/views/federails/client/activities/_index.json.jbuilder +1 -0
  30. data/app/views/federails/client/activities/feed.html.erb +4 -0
  31. data/app/views/federails/client/activities/feed.json.jbuilder +1 -0
  32. data/app/views/federails/client/activities/index.html.erb +5 -0
  33. data/app/views/federails/client/activities/index.json.jbuilder +1 -0
  34. data/app/views/federails/client/actors/_actor.json.jbuilder +14 -0
  35. data/app/views/federails/client/actors/_lookup_form.html.erb +5 -0
  36. data/app/views/federails/client/actors/index.html.erb +24 -0
  37. data/app/views/federails/client/actors/index.json.jbuilder +1 -0
  38. data/app/views/federails/client/actors/show.html.erb +100 -0
  39. data/app/views/federails/client/actors/show.json.jbuilder +1 -0
  40. data/app/views/federails/client/followings/_follow.html.erb +4 -0
  41. data/app/views/federails/client/followings/_follower.html.erb +7 -0
  42. data/app/views/federails/client/followings/_following.json.jbuilder +1 -0
  43. data/app/views/federails/client/followings/_form.html.erb +21 -0
  44. data/app/views/federails/client/followings/index.html.erb +29 -0
  45. data/app/views/federails/client/followings/index.json.jbuilder +1 -0
  46. data/app/views/federails/client/followings/show.html.erb +21 -0
  47. data/app/views/federails/client/followings/show.json.jbuilder +1 -0
  48. data/app/views/federails/server/activities/_activity.json.jbuilder +9 -0
  49. data/app/views/federails/server/activities/outbox.json.jbuilder +18 -0
  50. data/app/views/federails/server/activities/show.json.jbuilder +1 -0
  51. data/app/views/federails/server/actors/_actor.json.jbuilder +11 -0
  52. data/app/views/federails/server/actors/followers.json.jbuilder +18 -0
  53. data/app/views/federails/server/actors/following.json.jbuilder +18 -0
  54. data/app/views/federails/server/actors/show.json.jbuilder +1 -0
  55. data/app/views/federails/server/followings/_following.json.jbuilder +7 -0
  56. data/app/views/federails/server/followings/show.json.jbuilder +1 -0
  57. data/app/views/federails/server/nodeinfo/index.json.jbuilder +6 -0
  58. data/app/views/federails/server/nodeinfo/show.json.jbuilder +19 -0
  59. data/app/views/federails/server/web_finger/find.json.jbuilder +20 -0
  60. data/app/views/federails/server/web_finger/host_meta.xml.erb +5 -0
  61. data/config/routes.rb +43 -0
  62. data/db/migrate/20200712133150_create_federails_actors.rb +24 -0
  63. data/db/migrate/20200712143127_create_federails_followings.rb +14 -0
  64. data/db/migrate/20200712174938_create_federails_activities.rb +11 -0
  65. data/db/migrate/20240731145400_change_actor_entity_rel_to_polymorphic.rb +11 -0
  66. data/lib/federails/configuration.rb +88 -0
  67. data/lib/federails/engine.rb +6 -0
  68. data/lib/federails/utils/host.rb +54 -0
  69. data/lib/federails/version.rb +1 -1
  70. data/lib/federails.rb +33 -3
  71. data/lib/fediverse/inbox.rb +71 -0
  72. data/lib/fediverse/notifier.rb +21 -0
  73. data/lib/fediverse/request.rb +38 -0
  74. data/lib/fediverse/webfinger.rb +93 -0
  75. data/lib/generators/federails/install/USAGE +9 -0
  76. data/lib/generators/federails/install/install_generator.rb +10 -0
  77. data/lib/generators/federails/install/templates/federails.rb +1 -0
  78. data/lib/generators/federails/install/templates/federails.yml +23 -0
  79. data/lib/tasks/factory_bot.rake +15 -0
  80. metadata +165 -10
  81. data/app/views/layouts/federails/application.html.erb +0 -15
@@ -0,0 +1,93 @@
1
+ require 'faraday'
2
+ require 'faraday/follow_redirects'
3
+
4
+ require 'federails/utils/host'
5
+
6
+ module Fediverse
7
+ class Webfinger
8
+ class << self
9
+ ACCOUNT_REGEX = /(?<username>[a-z0-9\-_.]+)(?:@(?<domain>.*))?/
10
+
11
+ def split_resource_account(account)
12
+ /\Aacct:#{ACCOUNT_REGEX}\z/io.match account
13
+ end
14
+
15
+ def split_account(account)
16
+ /\A#{ACCOUNT_REGEX}\z/io.match account
17
+ end
18
+
19
+ def local_user?(account)
20
+ account[:username] && (account[:domain].nil? || (account[:domain] == Federails::Utils::Host.localhost))
21
+ end
22
+
23
+ def fetch_actor(username, domain)
24
+ fetch_actor_url webfinger(username, domain)
25
+ end
26
+
27
+ def fetch_actor_url(url)
28
+ webfinger_to_actor get_json url
29
+ end
30
+
31
+ # Returns actor id
32
+ def webfinger(username, domain)
33
+ scheme = Federails.configuration.force_ssl ? 'https' : 'http'
34
+ json = get_json "#{scheme}://#{domain}/.well-known/webfinger", resource: "acct:#{username}@#{domain}"
35
+ link = json['links'].find { |l| l['type'] == 'application/activity+json' }
36
+
37
+ link['href'] if link
38
+ end
39
+
40
+ private
41
+
42
+ def webfinger_to_actor(data)
43
+ uri = URI.parse data['id']
44
+ server = uri.host
45
+ server += ":#{uri.port}" if uri.port && [80, 443].exclude?(uri.port)
46
+ Federails::Actor.new federated_url: data['id'],
47
+ username: data['preferredUsername'],
48
+ name: data['name'],
49
+ server: server,
50
+ inbox_url: data['inbox'],
51
+ outbox_url: data['outbox'],
52
+ followers_url: data['followers'],
53
+ followings_url: data['following'],
54
+ profile_url: data['url']
55
+ end
56
+
57
+ def get_json(url, payload = {})
58
+ response = get(url, payload: payload, headers: { accept: 'application/json' })
59
+
60
+ if response.status != 200
61
+ Rails.logger.debug { "Unhandled status code #{response.status} for GET #{url}" }
62
+ raise ActiveRecord::RecordNotFound
63
+ end
64
+
65
+ JSON.parse(response.body)
66
+ rescue JSON::ParserError
67
+ Rails.logger.debug { "Invalid JSON response GET #{url}" }
68
+
69
+ raise ActiveRecord::RecordNotFound
70
+ end
71
+
72
+ # Only perform a GET request and throws an ActiveRecord::RecordNotFound
73
+ # on error.
74
+ # That's "ok-ish"; when an actor is unavailable, whatever the reason is, it's
75
+ # not found...
76
+ def get(url, payload: {}, headers: {})
77
+ connection = Faraday.new url: url, params: payload, headers: headers do |faraday|
78
+ faraday.response :follow_redirects # use Faraday::FollowRedirects::Middleware
79
+ faraday.adapter Faraday.default_adapter
80
+ end
81
+
82
+ begin
83
+ response = connection.get
84
+ rescue Faraday::ConnectionFailed
85
+ Rails.logger.debug { "Failed to reach server for GET #{url}" }
86
+ raise ActiveRecord::RecordNotFound
87
+ end
88
+
89
+ response
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,9 @@
1
+ Description:
2
+ Copies configuration file and initializer
3
+
4
+ Example:
5
+ bin/rails generate federails:install
6
+
7
+ This will create:
8
+ config/federails.yml
9
+ config/initializers/federails.rb
@@ -0,0 +1,10 @@
1
+ module Federails
2
+ class InstallGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('templates', __dir__)
4
+
5
+ def copy_files
6
+ copy_file 'federails.yml', 'config/federails.yml'
7
+ copy_file 'federails.rb', 'config/initializers/federails.rb'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1 @@
1
+ Federails.config_from 'federails'
@@ -0,0 +1,23 @@
1
+ ---
2
+ defaults: &defaults
3
+ app_name:
4
+ app_version:
5
+ force_ssl: false
6
+ site_host: http://localhost
7
+ site_port: 3000
8
+ enable_discovery: true
9
+ app_layout: 'layouts/application'
10
+ server_routes_path: federation
11
+ client_routes_path: app
12
+
13
+ development:
14
+ <<: *defaults
15
+
16
+ test:
17
+ <<: *defaults
18
+ site_port: null
19
+
20
+ production:
21
+ <<: *defaults
22
+ force_ssl: true
23
+ site_port: 443
@@ -0,0 +1,15 @@
1
+ namespace :factory_bot do
2
+ desc 'Verify that all FactoryBot factories are valid'
3
+ task lint: :environment do
4
+ if Rails.env.test?
5
+ conn = ActiveRecord::Base.connection
6
+ conn.transaction do
7
+ FactoryBot.lint traits: true
8
+ raise ActiveRecord::Rollback
9
+ end
10
+ else
11
+ system("bundle exec rake app:factory_bot:lint RAILS_ENV='test'")
12
+ raise if $CHILD_STATUS.exitstatus.nonzero?
13
+ end
14
+ end
15
+ end
metadata CHANGED
@@ -1,15 +1,99 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: federails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Tancoigne
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-04 00:00:00.000000000 Z
11
+ date: 2024-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday-follow_redirects
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json-ld
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: json-ld-preloaded
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 3.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: kaminari
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 1.2.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 1.2.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: pundit
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 2.3.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 2.3.0
13
97
  - !ruby/object:Gem::Dependency
14
98
  name: rails
15
99
  requirement: !ruby/object:Gem::Requirement
@@ -24,7 +108,7 @@ dependencies:
24
108
  - - ">="
25
109
  - !ruby/object:Gem::Version
26
110
  version: 7.0.4
27
- description: Ruby on Rails engine to add support to Activity Pub
111
+ description: An ActivityPub engine for Ruby on Rails
28
112
  email:
29
113
  - manu@experimentslabs.com
30
114
  executables: []
@@ -36,15 +120,84 @@ files:
36
120
  - app/assets/config/federails_manifest.js
37
121
  - app/assets/stylesheets/federails/application.css
38
122
  - app/controllers/federails/application_controller.rb
123
+ - app/controllers/federails/client/activities_controller.rb
124
+ - app/controllers/federails/client/actors_controller.rb
125
+ - app/controllers/federails/client/followings_controller.rb
126
+ - app/controllers/federails/server/activities_controller.rb
127
+ - app/controllers/federails/server/actors_controller.rb
128
+ - app/controllers/federails/server/followings_controller.rb
129
+ - app/controllers/federails/server/nodeinfo_controller.rb
130
+ - app/controllers/federails/server/server_controller.rb
131
+ - app/controllers/federails/server/web_finger_controller.rb
39
132
  - app/helpers/federails/application_helper.rb
40
133
  - app/jobs/federails/application_job.rb
134
+ - app/jobs/federails/notify_inbox_job.rb
41
135
  - app/mailers/federails/application_mailer.rb
136
+ - app/models/concerns/federails/entity.rb
137
+ - app/models/federails/activity.rb
138
+ - app/models/federails/actor.rb
42
139
  - app/models/federails/application_record.rb
43
- - app/views/layouts/federails/application.html.erb
140
+ - app/models/federails/following.rb
141
+ - app/policies/federails/client/activity_policy.rb
142
+ - app/policies/federails/client/actor_policy.rb
143
+ - app/policies/federails/client/following_policy.rb
144
+ - app/policies/federails/federails_policy.rb
145
+ - app/policies/federails/server/activity_policy.rb
146
+ - app/policies/federails/server/actor_policy.rb
147
+ - app/policies/federails/server/following_policy.rb
148
+ - app/views/federails/client/activities/_activity.html.erb
149
+ - app/views/federails/client/activities/_activity.json.jbuilder
150
+ - app/views/federails/client/activities/_index.json.jbuilder
151
+ - app/views/federails/client/activities/feed.html.erb
152
+ - app/views/federails/client/activities/feed.json.jbuilder
153
+ - app/views/federails/client/activities/index.html.erb
154
+ - app/views/federails/client/activities/index.json.jbuilder
155
+ - app/views/federails/client/actors/_actor.json.jbuilder
156
+ - app/views/federails/client/actors/_lookup_form.html.erb
157
+ - app/views/federails/client/actors/index.html.erb
158
+ - app/views/federails/client/actors/index.json.jbuilder
159
+ - app/views/federails/client/actors/show.html.erb
160
+ - app/views/federails/client/actors/show.json.jbuilder
161
+ - app/views/federails/client/followings/_follow.html.erb
162
+ - app/views/federails/client/followings/_follower.html.erb
163
+ - app/views/federails/client/followings/_following.json.jbuilder
164
+ - app/views/federails/client/followings/_form.html.erb
165
+ - app/views/federails/client/followings/index.html.erb
166
+ - app/views/federails/client/followings/index.json.jbuilder
167
+ - app/views/federails/client/followings/show.html.erb
168
+ - app/views/federails/client/followings/show.json.jbuilder
169
+ - app/views/federails/server/activities/_activity.json.jbuilder
170
+ - app/views/federails/server/activities/outbox.json.jbuilder
171
+ - app/views/federails/server/activities/show.json.jbuilder
172
+ - app/views/federails/server/actors/_actor.json.jbuilder
173
+ - app/views/federails/server/actors/followers.json.jbuilder
174
+ - app/views/federails/server/actors/following.json.jbuilder
175
+ - app/views/federails/server/actors/show.json.jbuilder
176
+ - app/views/federails/server/followings/_following.json.jbuilder
177
+ - app/views/federails/server/followings/show.json.jbuilder
178
+ - app/views/federails/server/nodeinfo/index.json.jbuilder
179
+ - app/views/federails/server/nodeinfo/show.json.jbuilder
180
+ - app/views/federails/server/web_finger/find.json.jbuilder
181
+ - app/views/federails/server/web_finger/host_meta.xml.erb
44
182
  - config/routes.rb
183
+ - db/migrate/20200712133150_create_federails_actors.rb
184
+ - db/migrate/20200712143127_create_federails_followings.rb
185
+ - db/migrate/20200712174938_create_federails_activities.rb
186
+ - db/migrate/20240731145400_change_actor_entity_rel_to_polymorphic.rb
45
187
  - lib/federails.rb
188
+ - lib/federails/configuration.rb
46
189
  - lib/federails/engine.rb
190
+ - lib/federails/utils/host.rb
47
191
  - lib/federails/version.rb
192
+ - lib/fediverse/inbox.rb
193
+ - lib/fediverse/notifier.rb
194
+ - lib/fediverse/request.rb
195
+ - lib/fediverse/webfinger.rb
196
+ - lib/generators/federails/install/USAGE
197
+ - lib/generators/federails/install/install_generator.rb
198
+ - lib/generators/federails/install/templates/federails.rb
199
+ - lib/generators/federails/install/templates/federails.yml
200
+ - lib/tasks/factory_bot.rake
48
201
  - lib/tasks/federails_tasks.rake
49
202
  homepage: https://experimentslabs.com
50
203
  licenses:
@@ -52,7 +205,9 @@ licenses:
52
205
  metadata:
53
206
  rubygems_mfa_required: 'true'
54
207
  homepage_uri: https://experimentslabs.com
55
- post_install_message:
208
+ source_code_uri: https://gitlab.com/experimentslabs/federails/
209
+ changelog_uri: https://gitlab.com/experimentslabs/federails/-/blob/main/CHANGELOG.md
210
+ post_install_message:
56
211
  rdoc_options: []
57
212
  require_paths:
58
213
  - lib
@@ -60,15 +215,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
60
215
  requirements:
61
216
  - - ">="
62
217
  - !ruby/object:Gem::Version
63
- version: '0'
218
+ version: 3.1.2
64
219
  required_rubygems_version: !ruby/object:Gem::Requirement
65
220
  requirements:
66
221
  - - ">="
67
222
  - !ruby/object:Gem::Version
68
223
  version: '0'
69
224
  requirements: []
70
- rubygems_version: 3.4.1
71
- signing_key:
225
+ rubygems_version: 3.4.15
226
+ signing_key:
72
227
  specification_version: 4
73
- summary: Ruby on Rails engine to add support to Activity Pub
228
+ summary: An ActivityPub engine for Ruby on Rails
74
229
  test_files: []
@@ -1,15 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <title>Federails</title>
5
- <%= csrf_meta_tags %>
6
- <%= csp_meta_tag %>
7
-
8
- <%= stylesheet_link_tag "federails/application", media: "all" %>
9
- </head>
10
- <body>
11
-
12
- <%= yield %>
13
-
14
- </body>
15
- </html>