federails 0.3.0 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e489a0088b05ef80d9cc112164f054a030f0cb007406cb14789372a5a52c490f
4
- data.tar.gz: b6832c622e5c33bf85f520839604a9e6e516821097d70bb6c6acb7df92f0d4b3
3
+ metadata.gz: a3463b1a877ca8224ade011016236c1fdbcbe41e31712b609347f41592d30531
4
+ data.tar.gz: 16ec7975b26bc5fbec1e243a4653c27d8b0239c7dbda42544150c9e5680d65f5
5
5
  SHA512:
6
- metadata.gz: 59e24e51b83545daabfa7dc0c878183d8b0bc14276b559b529fb5ee48183739c49b1a1987eb4a5e8d0d795dcfbe6970d6a3478691f9a13cd12c399903df61695
7
- data.tar.gz: 01f6e7bef60bd2d15e142b7b55fecf25dd76ab67571c994c3c8aae1eba23fbcc75d02fb03775131a070b3cc6c18021d33ce91bb8645e937d94c1425a0a7433a3
6
+ metadata.gz: b54850967223bb1be7aadd54547f757a9e5fbe6ccc8d84249a4a2b340a494b275905c25e862c493b2d2b177a7cc5e12abf2d6e44c4f84b4bec76796a7b9cba5f
7
+ data.tar.gz: cc00b05153d4434eacc6409a4422575d5e851e07c332001a72fc818e5f64d1bf6f2cd35f6eaa5f4025b3150fe9009a370665a044d44f84a60399690186a8f5e8
data/README.md CHANGED
@@ -132,7 +132,7 @@ Federails provides a concern to include in your "user" model or whatever will pu
132
132
 
133
133
  class User < ApplicationRecord
134
134
  # Include the concern here:
135
- include Federails::Entity
135
+ include Federails::ActorEntity
136
136
 
137
137
  # Configure field names
138
138
  acts_as_federails_actor username_field: :username, name_field: :name, profile_url_method: :user_url
@@ -145,7 +145,7 @@ an existing model with existing data, you will need to generate the correspondin
145
145
  Usage example:
146
146
 
147
147
  ```rb
148
- actor = User.find(1).actor
148
+ actor = User.find(1).federails_actor
149
149
 
150
150
  actor.inbox
151
151
  actor.outbox
@@ -14,7 +14,7 @@ module Federails
14
14
  # GET /app/feed
15
15
  # GET /app/feed.json
16
16
  def feed
17
- @activities = Activity.feed_for(current_user.actor)
17
+ @activities = Activity.feed_for(current_user.federails_actor)
18
18
  end
19
19
 
20
20
  private
@@ -32,7 +32,7 @@ module Federails
32
32
  # POST /app/followings.json
33
33
  def create
34
34
  @following = Following.new(following_params)
35
- @following.actor = current_user.actor
35
+ @following.actor = current_user.federails_actor
36
36
  authorize @following, policy_class: Federails::Client::FollowingPolicy
37
37
 
38
38
  save_and_render
@@ -44,7 +44,7 @@ module Federails
44
44
  authorize Federails::Following, policy_class: Federails::Client::FollowingPolicy
45
45
 
46
46
  begin
47
- @following = Following.new_from_account following_account_params, actor: current_user.actor
47
+ @following = Following.new_from_account following_account_params, actor: current_user.federails_actor
48
48
  rescue ::ActiveRecord::RecordNotFound
49
49
  # Renders a 422 instead of a 404
50
50
  respond_to do |format|
@@ -86,7 +86,7 @@ module Federails
86
86
  end
87
87
 
88
88
  def save_and_render # rubocop:disable Metrics/AbcSize
89
- url = federails.client_actor_url current_user.actor
89
+ url = federails.client_actor_url current_user.federails_actor
90
90
 
91
91
  respond_to do |format|
92
92
  if @following.save
@@ -12,7 +12,7 @@ module Federails
12
12
 
13
13
  @total = @active_halfyear = @active_month = 0
14
14
  @has_user_counts = false
15
- Federails::Configuration.entity_types.each_value do |config|
15
+ Federails::Configuration.actor_types.each_value do |config|
16
16
  next unless (method = config[:user_count_method]&.to_sym)
17
17
 
18
18
  @has_user_counts = true
@@ -11,7 +11,7 @@ module Federails
11
11
  when %r{^https?://.+}
12
12
  @user = Federails::Actor.find_by_federation_url(resource)&.entity
13
13
  when /^acct:.+/
14
- Federails::Configuration.entity_types.each_value do |entity|
14
+ Federails::Configuration.actor_types.each_value do |entity|
15
15
  @user ||= entity[:class].find_by(entity[:username_field] => username)
16
16
  end
17
17
  end
@@ -1,4 +1,8 @@
1
1
  module Federails
2
2
  class ApplicationJob < ActiveJob::Base
3
+ discard_on ActiveJob::DeserializationError
4
+ after_discard do |_job, exception|
5
+ Rails.logger.info exception.to_s
6
+ end
3
7
  end
4
8
  end
@@ -1,5 +1,5 @@
1
1
  module Federails
2
- module Entity
2
+ module ActorEntity
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do # rubocop:todo Metrics/BlockLength
@@ -24,12 +24,12 @@ module Federails
24
24
  Fediverse::Inbox.register_handler(activity_type, object_type, self, method)
25
25
  end
26
26
 
27
- has_one :actor, class_name: 'Federails::Actor', as: :entity, dependent: :destroy
27
+ has_one :federails_actor, class_name: 'Federails::Actor', as: :entity, dependent: :destroy
28
28
 
29
- after_create :create_actor, if: lambda {
30
- raise("Entity not configured for #{self.class.name}. Did you use \"acts_as_federails_actor\"?") unless Federails::Configuration.entity_types.key? self.class.name
29
+ after_create :create_federails_actor, if: lambda {
30
+ raise("Entity not configured for #{self.class.name}. Did you use \"acts_as_federails_actor\"?") unless Federails.actor_entity? self
31
31
 
32
- Federails::Configuration.entity_types[self.class.name][:auto_create_actors]
32
+ Federails.actor_entity(self)[:auto_create_actors]
33
33
  }
34
34
 
35
35
  # Configures the mapping between entity and actor
@@ -53,7 +53,7 @@ module Federails
53
53
  user_count_method: nil,
54
54
  auto_create_actors: true
55
55
  )
56
- Federails::Configuration.register_entity(
56
+ Federails::Configuration.register_actor_class(
57
57
  self,
58
58
  username_field: username_field,
59
59
  name_field: name_field,
@@ -90,7 +90,7 @@ module Federails
90
90
 
91
91
  private
92
92
 
93
- def create_actor
93
+ def create_federails_actor
94
94
  Federails::Actor.create! entity: self
95
95
  end
96
96
  end
@@ -91,9 +91,9 @@ module Federails
91
91
  end
92
92
 
93
93
  def entity_configuration
94
- raise("Entity not configured for #{entity_type}. Did you use \"acts_as_federails_actor\"?") unless Federails::Configuration.entity_types.key? entity_type
94
+ raise("Entity not configured for #{entity_type}. Did you use \"acts_as_federails_actor\"?") unless Federails.actor_entity? entity_type
95
95
 
96
- Federails::Configuration.entity_types[entity_type]
96
+ Federails.actor_entity entity_type
97
97
  end
98
98
 
99
99
  class << self
@@ -102,8 +102,8 @@ module Federails
102
102
 
103
103
  if Fediverse::Webfinger.local_user? parts
104
104
  actor = nil
105
- Federails::Configuration.entity_types.each_value do |entity|
106
- actor ||= entity[:class].find_by(entity[:username_field] => parts[:username])&.actor
105
+ Federails::Configuration.actor_types.each_value do |entity|
106
+ actor ||= entity[:class].find_by(entity[:username_field] => parts[:username])&.federails_actor
107
107
  end
108
108
  raise ActiveRecord::RecordNotFound if actor.nil?
109
109
  else
@@ -2,7 +2,7 @@ module Federails
2
2
  class Following < ApplicationRecord
3
3
  include Federails::HasUuid
4
4
 
5
- enum status: { pending: 0, accepted: 1 }
5
+ enum :status, pending: 0, accepted: 1
6
6
 
7
7
  validates :target_actor_id, uniqueness: { scope: [:actor_id, :target_actor_id] }
8
8
 
@@ -10,7 +10,7 @@ module Federails
10
10
  end
11
11
 
12
12
  def accept?
13
- in_following? && @record.target_actor_id == @user.actor.id
13
+ in_following? && @record.target_actor_id == @user.federails_actor.id
14
14
  end
15
15
 
16
16
  def follow?
@@ -19,7 +19,7 @@ module Federails
19
19
 
20
20
  class Scope < Scope
21
21
  def resolve
22
- scope.with_actor(@user.actor)
22
+ scope.with_actor(@user.federails_actor)
23
23
  end
24
24
  end
25
25
 
@@ -28,7 +28,7 @@ module Federails
28
28
  def in_following?
29
29
  return false unless user_with_actor?
30
30
 
31
- @record.actor_id == @user.actor&.id || @record.target_actor_id == @user.actor&.id
31
+ @record.actor_id == @user.federails_actor&.id || @record.target_actor_id == @user.federails_actor&.id
32
32
  end
33
33
  end
34
34
  end
@@ -53,11 +53,11 @@ module Federails
53
53
  def owner?
54
54
  return false unless @user
55
55
 
56
- @record.actor_id == @user.actor.id
56
+ @record.actor_id == @user.federails_actor.id
57
57
  end
58
58
 
59
59
  def user_with_actor?
60
- @user && Federails.actor_entity?(@user) && !!@user.actor
60
+ @user && Federails.actor_entity?(@user) && !!@user.federails_actor
61
61
  end
62
62
  end
63
63
  end
@@ -11,14 +11,14 @@ Federation:
11
11
  <%end %>
12
12
 
13
13
  <%# Available when user has an associated actor. The actor must exist to create the link %>
14
- <% if Federails.actor_entity?(user) && user.actor.present? %>
15
- <li><%= link_to "You (#{user.actor.username})", federails.client_actor_path(user.actor) %></li>
14
+ <% if Federails.actor_entity?(user) && user.federails_actor.present? %>
15
+ <li><%= link_to "You (#{user.federails_actor.username})", federails.client_actor_path(user.federails_actor) %></li>
16
16
  <% end %>
17
17
  </ul>
18
18
 
19
19
  <%# Debug information%>
20
20
  <% if !Federails::actor_entity?(user) %>
21
21
  <p><%= user.class.name %> is not configured to have an associated actor; you won't be allowed to follow or be followed</p>
22
- <% elsif !user.actor.present? %>
22
+ <% elsif !user.federails_actor.present? %>
23
23
  <p>Your account does not have an associated actor; you won't be allowed to follow or be followed</p>
24
24
  <% end %>
@@ -6,7 +6,7 @@
6
6
  %>
7
7
  <p>
8
8
  <% if Federails::Client::FollowingPolicy.new(user, Federails::Following).create? %>
9
- <% follow = user.actor.follows? actor %>
9
+ <% follow = user.federails_actor.follows? actor %>
10
10
  <% if actor.entity == user %>
11
11
  <button type="button" role="button" disabled="disabled">That's you</button>
12
12
  <% elsif follow %>
@@ -16,7 +16,7 @@
16
16
  <%= button_to "Follow #{actor.username}", federails.follow_client_followings_path, params: { account: actor.at_address }, method: :post %>
17
17
  <% end %>
18
18
 
19
- <% followed = actor.follows? user.actor %>
19
+ <% followed = actor.follows? user.federails_actor %>
20
20
  <% if followed %>
21
21
  <% if followed.pending? %>
22
22
  <%= actor.username %> wants to follow you.
@@ -1,7 +1,7 @@
1
1
  <div>
2
2
  <b><%= link_to following.actor.name, federails.client_actor_url(following.actor) %></b>
3
3
  (<%= following.actor.at_address %>) (<%= following.status %>)
4
- <% if following.pending? && following.target_actor == current_user.actor %>
4
+ <% if following.pending? && following.target_actor == current_user.federails_actor %>
5
5
  <%= button_to 'Accept', federails.accept_client_following_path(following), method: :put %>
6
6
  <% end %>
7
7
  </div>
@@ -5,16 +5,16 @@ links = [
5
5
  {
6
6
  rel: 'self',
7
7
  type: Mime[:activitypub].to_s,
8
- href: @user.actor.federated_url,
8
+ href: @user.federails_actor.federated_url,
9
9
  },
10
10
  ]
11
11
 
12
12
  # User profile URL if configured
13
13
  # TODO: Add a profile controller/action in dummy to test this
14
- if @user.actor.profile_url
14
+ if @user.federails_actor.profile_url
15
15
  links.push rel: 'https://webfinger.net/rel/profile-page',
16
16
  type: 'text/html',
17
- href: @user.actor.profile_url
17
+ href: @user.federails_actor.profile_url
18
18
  end
19
19
 
20
20
  # Remote following
@@ -59,12 +59,12 @@ module Federails
59
59
  Federails::Engine.routes.default_url_options[:port] = value
60
60
  end
61
61
 
62
- # List of entity types
63
- mattr_reader :entity_types
64
- @@entity_types = {}
62
+ # List of actor types (classes using Federails::ActorEntity)
63
+ mattr_reader :actor_types
64
+ @@actor_types = {}
65
65
 
66
- def self.register_entity(klass, config = {})
67
- @@entity_types[klass.name] = config.merge(class: klass)
66
+ def self.register_actor_class(klass, config = {})
67
+ @@actor_types[klass.name] = config.merge(class: klass)
68
68
  end
69
69
  end
70
70
  # rubocop:enable Style/ClassVars
@@ -1,3 +1,3 @@
1
1
  module Federails
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
data/lib/federails.rb CHANGED
@@ -10,35 +10,58 @@ module Federails
10
10
  # Make factories available
11
11
  config.factory_bot.definition_file_paths += [File.expand_path('spec/factories', __dir__)] if defined?(FactoryBotRails)
12
12
 
13
- def self.configure
14
- yield @@configuration
15
- end
13
+ class << self
14
+ def configure
15
+ yield @@configuration
16
+ end
16
17
 
17
- def self.config_from(name) # rubocop:disable Metrics/MethodLength
18
- config = Rails.application.config_for name
19
- [
20
- :app_name,
21
- :app_version,
22
- :force_ssl,
23
- :site_host,
24
- :site_port,
25
- :enable_discovery,
26
- :open_registrations,
27
- :app_layout,
28
- :server_routes_path,
29
- :client_routes_path,
30
- :remote_follow_url_method,
31
- :base_client_controller,
32
- ].each { |key| Configuration.send :"#{key}=", config[key] if config.key?(key) }
33
- end
18
+ def config_from(name) # rubocop:disable Metrics/MethodLength
19
+ config = Rails.application.config_for name
20
+ [
21
+ :app_name,
22
+ :app_version,
23
+ :force_ssl,
24
+ :site_host,
25
+ :site_port,
26
+ :enable_discovery,
27
+ :open_registrations,
28
+ :app_layout,
29
+ :server_routes_path,
30
+ :client_routes_path,
31
+ :remote_follow_url_method,
32
+ :base_client_controller,
33
+ ].each { |key| Configuration.send :"#{key}=", config[key] if config.key?(key) }
34
+ end
35
+
36
+ # @return [Boolean] True if the given model is a possible actor
37
+ #
38
+ # @example
39
+ # puts "Follow #{some_actor.name}" if actor_entity? current_user
40
+ def actor_entity?(class_or_instance)
41
+ Configuration.actor_types.key? class_or_instance_name(class_or_instance)
42
+ end
43
+
44
+ # @return [Hash] The configuration for the given actor entity
45
+ def actor_entity(class_or_instance)
46
+ klass = class_or_instance_name(class_or_instance)
47
+ raise "#{klass} is not a configured actor entity" unless Configuration.actor_types.key?(klass)
48
+
49
+ Configuration.actor_types[klass]
50
+ end
51
+
52
+ private
34
53
 
35
- # @return [Boolean] True if the given model is a possible entity
36
- #
37
- # @example
38
- # puts "Follow #{some_actor.name}" if actor_entity? current_user
39
- def self.actor_entity?(class_or_instance)
40
- klass = class_or_instance.is_a?(Class) ? class_or_instance.name : class_or_instance.class.name
41
- Configuration.entity_types.key? klass
54
+ # @return [String] Class name of the provided class or instance
55
+ def class_or_instance_name(class_or_instance)
56
+ case class_or_instance
57
+ when String
58
+ class_or_instance
59
+ when Class
60
+ class_or_instance.name
61
+ else
62
+ class_or_instance.class.name
63
+ end
64
+ end
42
65
  end
43
66
  end
44
67
  # rubocop:enable Style/ClassVars
@@ -15,11 +15,12 @@ module Fediverse
15
15
  handlers.each_pair do |klass, method|
16
16
  klass.send method, payload
17
17
  end
18
- return unless handlers.empty?
18
+ return true unless handlers.empty?
19
19
 
20
20
  # FIXME: Fails silently
21
21
  # raise NotImplementedError
22
22
  Rails.logger.debug { "Unhandled activity type: #{payload['type']}" }
23
+ false
23
24
  end
24
25
 
25
26
  private
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: federails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Tancoigne
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-23 00:00:00.000000000 Z
11
+ date: 2024-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -133,7 +133,7 @@ files:
133
133
  - app/jobs/federails/application_job.rb
134
134
  - app/jobs/federails/notify_inbox_job.rb
135
135
  - app/mailers/federails/application_mailer.rb
136
- - app/models/concerns/federails/entity.rb
136
+ - app/models/concerns/federails/actor_entity.rb
137
137
  - app/models/concerns/federails/has_uuid.rb
138
138
  - app/models/federails/activity.rb
139
139
  - app/models/federails/actor.rb