inboxes_controller.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # frozen_string_literal: true
  2. class ActivityPub::InboxesController < ActivityPub::BaseController
  3. include SignatureVerification
  4. include JsonLdHelper
  5. include AccountOwnedConcern
  6. before_action :skip_unknown_actor_activity
  7. before_action :require_actor_signature!
  8. skip_before_action :authenticate_user!
  9. def create
  10. upgrade_account
  11. process_collection_synchronization
  12. process_payload
  13. head 202
  14. end
  15. private
  16. def skip_unknown_actor_activity
  17. head 202 if unknown_affected_account?
  18. end
  19. def unknown_affected_account?
  20. json = Oj.load(body, mode: :strict)
  21. json.is_a?(Hash) && %w(Delete Update).include?(json['type']) && json['actor'].present? && json['actor'] == value_or_id(json['object']) && !Account.where(uri: json['actor']).exists?
  22. rescue Oj::ParseError
  23. false
  24. end
  25. def account_required?
  26. params[:account_username].present?
  27. end
  28. def skip_temporary_suspension_response?
  29. true
  30. end
  31. def body
  32. return @body if defined?(@body)
  33. @body = request.body.read
  34. @body.force_encoding('UTF-8') if @body.present?
  35. request.body.rewind if request.body.respond_to?(:rewind)
  36. @body
  37. end
  38. def upgrade_account
  39. if signed_request_account&.ostatus?
  40. signed_request_account.update(last_webfingered_at: nil)
  41. ResolveAccountWorker.perform_async(signed_request_account.acct)
  42. end
  43. DeliveryFailureTracker.reset!(signed_request_actor.inbox_url)
  44. end
  45. def process_collection_synchronization
  46. raw_params = request.headers['Collection-Synchronization']
  47. return if raw_params.blank? || ENV['DISABLE_FOLLOWERS_SYNCHRONIZATION'] == 'true' || signed_request_account.nil?
  48. # Re-using the syntax for signature parameters
  49. tree = SignatureParamsParser.new.parse(raw_params)
  50. params = SignatureParamsTransformer.new.apply(tree)
  51. ActivityPub::PrepareFollowersSynchronizationService.new.call(signed_request_account, params)
  52. rescue Parslet::ParseFailed
  53. Rails.logger.warn 'Error parsing Collection-Synchronization header'
  54. end
  55. def process_payload
  56. ActivityPub::ProcessingWorker.perform_async(signed_request_actor.id, body, @account&.id, signed_request_actor.class.name)
  57. end
  58. end