diff --git a/app/lib/activity_log_audience_helper.rb b/app/lib/activity_log_audience_helper.rb index a9d630e4f..f0af297f7 100644 --- a/app/lib/activity_log_audience_helper.rb +++ b/app/lib/activity_log_audience_helper.rb @@ -19,7 +19,9 @@ class ActivityLogAudienceHelper if match = activity_log_event.path.match(Regexp.new("https://#{domain}/users/([^/]*)/inbox")) return [match.captures[0]] elsif activity_log_event.path == "https://#{domain}/inbox" - return ['to', 'bto', 'cc', 'bcc'].map { |target| actors(activity_log_event.data[target]) }.flatten + return ['to', 'bto', 'cc', 'bcc'] + .map { |target| actors(activity_log_event.data[target]) } + .flatten end return [] @@ -41,10 +43,17 @@ class ActivityLogAudienceHelper string_or_array.map do |string| if match = string.match(Regexp.new("https://#{domain}/users/([^/]*)")) match.captures[0] + elsif string.ends_with?("/followers") + Account + .joins( + "JOIN follows ON follows.account_id = accounts.id + JOIN accounts AS followed ON follows.target_account_id = followed.id + WHERE followed.followers_url = '#{string}'") + .map { |account| account.username } else nil end - end.compact + end.flatten.compact end end end diff --git a/spec/fixtures/activity_log_events/inbound-with-follower-recipients.json b/spec/fixtures/activity_log_events/inbound-with-follower-recipients.json new file mode 100644 index 000000000..bbdb9cbf9 --- /dev/null +++ b/spec/fixtures/activity_log_events/inbound-with-follower-recipients.json @@ -0,0 +1,25 @@ +{ + "timestamp":"2022-12-08T17:12:38Z", + "type": "inbound", + "path": "https://example.com/inbox", + "data": { + "@context": "https://www.w3.org/ns/activitystreams", + "actor": "https://other.org/users/bob/", + "id": "https://other.org/users/bob/statuses/109473290785654613/activity", + "object": { + "attributedTo": "https://other.org/users/bob/", + "content": "A post to selected audiences", + "id": "https://other.org/users/bob/statuses/109473290785654613", + "to": [ + "https://www.w3.org/ns/activitystreams#Public", + "https://other.org/users/bob/followers" + ], + "type": "Note" + }, + "to": [ + "https://www.w3.org/ns/activitystreams#Public", + "https://other.org/users/bob/followers" + ], + "type": "Create" + } +} diff --git a/spec/lib/activity_log_audience_helper_spec.rb b/spec/lib/activity_log_audience_helper_spec.rb index 85a7276bf..971e68d96 100644 --- a/spec/lib/activity_log_audience_helper_spec.rb +++ b/spec/lib/activity_log_audience_helper_spec.rb @@ -59,6 +59,20 @@ RSpec.describe ActivityLogAudienceHelper do 'one-bcc' ]) end + + it 'returns followers from to, bto, cc, bcc if sent to public inbox' do + Rails.configuration.x.web_domain = 'example.com' + inbound_event = activity_log_event_fixture('inbound-with-follower-recipients.json') + + bob = Fabricate(:account, username: 'bob', followers_url: 'https://other.org/users/bob/followers') + Fabricate(:account, username: 'first_follower').follow!(bob) + Fabricate(:account, username: 'second_follower').follow!(bob) + + expect(ActivityLogAudienceHelper.audience(inbound_event)).to match_array([ + 'first_follower', + 'second_follower', + ]) + end end end end