accept.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # frozen_string_literal: true
  2. class ActivityPub::Activity::Accept < ActivityPub::Activity
  3. def perform
  4. return accept_follow_for_relay if relay_follow?
  5. return accept_follow!(follow_request_from_object) unless follow_request_from_object.nil?
  6. case @object['type']
  7. when 'Follow'
  8. accept_embedded_follow
  9. end
  10. end
  11. private
  12. def accept_embedded_follow
  13. target_account = account_from_uri(target_uri)
  14. return if target_account.nil? || !target_account.local?
  15. follow_request = FollowRequest.find_by(account: target_account, target_account: @account)
  16. accept_follow!(follow_request)
  17. end
  18. def accept_follow!(request)
  19. return if request.nil?
  20. is_first_follow = !request.target_account.followers.local.exists?
  21. request.authorize!
  22. RemoteAccountRefreshWorker.perform_async(request.target_account_id) if is_first_follow
  23. end
  24. def accept_follow_for_relay
  25. relay.update!(state: :accepted)
  26. end
  27. def relay
  28. @relay ||= Relay.find_by(follow_activity_id: object_uri) unless object_uri.nil?
  29. end
  30. def relay_follow?
  31. relay.present?
  32. end
  33. def target_uri
  34. @target_uri ||= value_or_id(@object['actor'])
  35. end
  36. end