handle audicence fields

This commit is contained in:
Sebastian Jambor 2023-01-05 14:33:43 +01:00
parent c8ea90f6f1
commit 509b04c2a0
3 changed files with 58 additions and 27 deletions

View file

@ -3,12 +3,12 @@
class ActivityLogAudienceHelper
def self.audience(activity_log_event)
domain = Rails.configuration.x.local_domain
domain = Rails.configuration.x.web_domain
if activity_log_event.type == 'outbound'
actor = activity_log_event.data['actor']
if actor and match = actor.match(Regexp.new("https://#{domain}/users/(.*)"))
if actor and match = actor.match(Regexp.new("https://#{domain}/users/([^/]*)"))
return [match.captures[0]]
else
return []
@ -16,10 +16,10 @@ class ActivityLogAudienceHelper
end
if activity_log_event.type == 'inbound'
if match = activity_log_event.path.match(Regexp.new("https://#{domain}/users/(.*)/inbox"))
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
end
return []
@ -27,4 +27,24 @@ class ActivityLogAudienceHelper
return []
end
private
def self.actors(string_or_array)
domain = Rails.configuration.x.web_domain
if string_or_array.nil?
[]
elsif string_or_array.is_a?(String)
self.actors([string_or_array])
else
string_or_array.map do |string|
if match = string.match(Regexp.new("https://#{domain}/users/([^/]*)"))
match.captures[0]
else
nil
end
end.compact
end
end
end

View file

@ -1,8 +1,12 @@
{
"@context": "https://www.w3.org/ns/activitystreams",
"actor": "https://other.org/users/bob/",
"id": "https://other.org/users/bob/statuses/109473290785654613/activity",
"object": {
"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",
@ -20,19 +24,20 @@
"https://example.com/users/one-bcc"
],
"type": "Note"
},
"to": [
"https://www.w3.org/ns/activitystreams#Public",
"https://example.com/users/first-to",
"https://example.com/users/second-to",
"https://other.org/users/other-instance"
],
"bto": "https://example.com/users/single-bto",
"cc": [
"https://example.com/users/one-cc"
},
"to": [
"https://www.w3.org/ns/activitystreams#Public",
"https://example.com/users/first-to",
"https://example.com/users/second-to",
"https://other.org/users/other-instance"
],
"bto": "https://example.com/users/single-bto",
"cc": [
"https://example.com/users/one-cc"
],
"bcc": [
"https://example.com/users/one-bcc"
],
"type": "Create"
"type": "Create"
}
}

View file

@ -10,28 +10,28 @@ end
RSpec.describe ActivityLogAudienceHelper do
describe '#audience' do
around do |example|
before = Rails.configuration.x.local_domain
before = Rails.configuration.x.web_domain
example.run
Rails.configuration.x.local_domain = before
Rails.configuration.x.web_domain = before
end
describe 'for inbound events' do
it 'returns the author if the domain matches' do
Rails.configuration.x.local_domain = 'example.com'
Rails.configuration.x.web_domain = 'example.com'
outbound_event = activity_log_event_fixture('outbound.json')
expect(ActivityLogAudienceHelper.audience(outbound_event)).to eq ['alice']
end
it 'returns nothing if the domain does not match' do
Rails.configuration.x.local_domain = 'does-not-match.com'
Rails.configuration.x.web_domain = 'does-not-match.com'
outbound_event = activity_log_event_fixture('outbound.json')
expect(ActivityLogAudienceHelper.audience(outbound_event)).to eq []
end
it 'returns nothing if the activity does not have an actor' do
Rails.configuration.x.local_domain = 'example.com'
Rails.configuration.x.web_domain = 'example.com'
outbound_event = activity_log_event_fixture('outbound.json')
outbound_event.data.delete('actor')
@ -41,17 +41,23 @@ RSpec.describe ActivityLogAudienceHelper do
describe 'for outbound events' do
it 'returns the inbox owner if it is sent to a personal inbox' do
Rails.configuration.x.local_domain = 'example.com'
Rails.configuration.x.web_domain = 'example.com'
inbound_event = activity_log_event_fixture('inbound-to-users-inbox.json')
expect(ActivityLogAudienceHelper.audience(inbound_event)).to eq ['bob']
end
it 'returns direct audience from to, bto, cc, bcc if sent to public inbox' do
Rails.configuration.x.local_domain = 'example.com'
Rails.configuration.x.web_domain = 'example.com'
inbound_event = activity_log_event_fixture('inbound-with-multiple-recipients.json')
expect(ActivityLogAudienceHelper.audience(inbound_event)).to match_array(['first-to', 'second-to', 'single-bto', 'one-cc', 'one-bcc'])
expect(ActivityLogAudienceHelper.audience(inbound_event)).to match_array([
'first-to',
'second-to',
'single-bto',
'one-cc',
'one-bcc'
])
end
end
end