diff --git a/app/controllers/activitypub/inboxes_controller.rb b/app/controllers/activitypub/inboxes_controller.rb index 582506902..2ffb47874 100644 --- a/app/controllers/activitypub/inboxes_controller.rb +++ b/app/controllers/activitypub/inboxes_controller.rb @@ -76,7 +76,13 @@ class ActivityPub::InboxesController < ActivityPub::BaseController end def process_payload - event = ActivityLogEvent.new('inbound', "https://#{Rails.configuration.x.web_domain}#{request.path}", Oj.load(body, mode: :strict)) + raw_signature = request.headers['Signature'] + tree = SignatureParamsParser.new.parse(raw_signature) + signature_params = SignatureParamsTransformer.new.apply(tree) + + sender = actor_from_key_id(signature_params['keyId']) + + event = ActivityLogEvent.new('inbound', sender.uri, "https://#{Rails.configuration.x.web_domain}#{request.path}", Oj.load(body, mode: :strict)) @activity_log_publisher.publish(event) diff --git a/app/lib/activity_log_audience_helper.rb b/app/lib/activity_log_audience_helper.rb index 5ab44351a..d486d6442 100644 --- a/app/lib/activity_log_audience_helper.rb +++ b/app/lib/activity_log_audience_helper.rb @@ -6,9 +6,9 @@ class ActivityLogAudienceHelper domain = Rails.configuration.x.web_domain if activity_log_event.type == 'outbound' - actor = activity_log_event.data['actor'] + sender = activity_log_event.sender - if actor and match = actor.match(Regexp.new("https://#{domain}/users/([^/]*)")) + if sender and match = sender.match(Regexp.new("https://#{domain}/users/([^/]*)")) [match.captures[0]] else [] diff --git a/app/lib/activity_log_event.rb b/app/lib/activity_log_event.rb index 2c1b53e1c..486dc3561 100644 --- a/app/lib/activity_log_event.rb +++ b/app/lib/activity_log_event.rb @@ -1,16 +1,17 @@ # frozen_string_literal: true class ActivityLogEvent - attr_accessor :type, :path, :data, :timestamp + attr_accessor :type, :sender, :path, :data, :timestamp def self.from_json_string(json_string) json = Oj.load(json_string, mode: :strict) - ActivityLogEvent.new(json['type'], json['path'], json['data']) + ActivityLogEvent.new(json['type'], json['sender'], json['path'], json['data']) end - def initialize(type, path, data, timestamp = Time.now.utc.iso8601) + def initialize(type, sender, path, data, timestamp = Time.now.utc.iso8601) @type = type + @sender = sender @path = path @data = data @timestamp = timestamp diff --git a/app/lib/activity_logger.rb b/app/lib/activity_logger.rb index cbd02a4ea..471d35fb5 100644 --- a/app/lib/activity_logger.rb +++ b/app/lib/activity_logger.rb @@ -28,7 +28,7 @@ class ActivityLogger Thread.new { while true - event = ActivityLogEvent.new('keep-alive', nil, nil) + event = ActivityLogEvent.new('keep-alive', nil, nil, nil) @@loggers.each_key do |key| ActivityLogger.log(key, event) end diff --git a/app/workers/activitypub/delivery_worker.rb b/app/workers/activitypub/delivery_worker.rb index a90566ad6..251ef20f7 100644 --- a/app/workers/activitypub/delivery_worker.rb +++ b/app/workers/activitypub/delivery_worker.rb @@ -29,10 +29,6 @@ class ActivityPub::DeliveryWorker def perform(json, source_account_id, inbox_url, options = {}) return unless DeliveryFailureTracker.available?(inbox_url) - event = ActivityLogEvent.new('outbound', inbox_url, Oj.load(json, mode: :strict)) - - @activity_log_publisher.publish(event) - @options = options.with_indifferent_access @json = json @source_account = Account.find(source_account_id) @@ -40,6 +36,11 @@ class ActivityPub::DeliveryWorker @host = Addressable::URI.parse(inbox_url).normalized_site @performed = false + event = ActivityLogEvent.new('outbound', "https://#{Rails.configuration.x.web_domain}/users/#{@source_account.username}", inbox_url, Oj.load(json, mode: :strict)) + + @activity_log_publisher.publish(event) + + perform_request ensure if @inbox_url.present? diff --git a/lib/activity_log_subscriber.rb b/lib/activity_log_subscriber.rb index 67dd8b87c..28b8a8210 100644 --- a/lib/activity_log_subscriber.rb +++ b/lib/activity_log_subscriber.rb @@ -7,8 +7,7 @@ class ActivityLogSubscriber redis.subscribe('activity_log') do |on| on.message do |channel, message| - json = Oj.load(message, mode: :strict) - event = ActivityLogEvent.new(json['type'], json['path'], json['data']) + event = ActivityLogEvent.from_json_string(message) ActivityLogAudienceHelper.audience(event) .each { |username| ActivityLogger.log(username, event) } diff --git a/spec/fixtures/activity_log_events/inbound-to-users-inbox.json b/spec/fixtures/activity_log_events/inbound-to-users-inbox.json index 6f868f896..3441ca16c 100644 --- a/spec/fixtures/activity_log_events/inbound-to-users-inbox.json +++ b/spec/fixtures/activity_log_events/inbound-to-users-inbox.json @@ -1,5 +1,6 @@ { "timestamp":"2022-12-08T17:12:38Z", + "sender": "https://other.org/users/alice", "type": "inbound", "path": "https://example.com/users/bob/inbox", "data": { diff --git a/spec/fixtures/activity_log_events/inbound-with-duplicate-recipients.json b/spec/fixtures/activity_log_events/inbound-with-duplicate-recipients.json index f9080cd3b..2822daaab 100644 --- a/spec/fixtures/activity_log_events/inbound-with-duplicate-recipients.json +++ b/spec/fixtures/activity_log_events/inbound-with-duplicate-recipients.json @@ -1,5 +1,6 @@ { "timestamp":"2022-12-08T17:12:38Z", + "sender": "https://other.org/users/bob/", "type": "inbound", "path": "https://example.com/inbox", "data": { diff --git a/spec/fixtures/activity_log_events/inbound-with-follower-recipients.json b/spec/fixtures/activity_log_events/inbound-with-follower-recipients.json index bbdb9cbf9..c4e780149 100644 --- a/spec/fixtures/activity_log_events/inbound-with-follower-recipients.json +++ b/spec/fixtures/activity_log_events/inbound-with-follower-recipients.json @@ -1,5 +1,6 @@ { "timestamp":"2022-12-08T17:12:38Z", + "sender": "https://other.org/users/bob/", "type": "inbound", "path": "https://example.com/inbox", "data": { diff --git a/spec/fixtures/activity_log_events/inbound-with-multiple-recipients.json b/spec/fixtures/activity_log_events/inbound-with-multiple-recipients.json index 6ed42b288..8e911caba 100644 --- a/spec/fixtures/activity_log_events/inbound-with-multiple-recipients.json +++ b/spec/fixtures/activity_log_events/inbound-with-multiple-recipients.json @@ -1,5 +1,6 @@ { "timestamp":"2022-12-08T17:12:38Z", + "sender": "https://other.org/users/bob/", "type": "inbound", "path": "https://example.com/inbox", "data": { diff --git a/spec/fixtures/activity_log_events/outbound.json b/spec/fixtures/activity_log_events/outbound.json index c0940f19d..6e30f1df0 100644 --- a/spec/fixtures/activity_log_events/outbound.json +++ b/spec/fixtures/activity_log_events/outbound.json @@ -1,5 +1,6 @@ { "timestamp":"2022-12-08T17:12:38Z", + "sender": "https://example.com/users/alice", "type": "outbound", "path": "https://other.org/users/bob/inbox", "data": { diff --git a/spec/lib/activity_logger_spec.rb b/spec/lib/activity_logger_spec.rb index fbd733409..16963be23 100644 --- a/spec/lib/activity_logger_spec.rb +++ b/spec/lib/activity_logger_spec.rb @@ -1,12 +1,6 @@ require 'json' require 'rails_helper' -def activity_log_event_fixture(name) - json_string = File.read(Rails.root.join('spec', 'fixtures', 'activity_log_events', name)) - - ActivityLogEvent.from_json_string(json_string) -end - RSpec.describe ActivityLogger do after(:each) do