follow.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334
  1. # frozen_string_literal: true
  2. class ActivityPub::Activity::Follow < ActivityPub::Activity
  3. def perform
  4. target_account = account_from_uri(object_uri)
  5. return if target_account.nil? || !target_account.local? || delete_arrived_first?(@json['id']) || @account.requested?(target_account)
  6. if target_account.blocking?(@account) || target_account.domain_blocking?(@account.domain) || target_account.moved?
  7. reject_follow_request!(target_account)
  8. return
  9. end
  10. # Fast-forward repeat follow requests
  11. if @account.following?(target_account)
  12. AuthorizeFollowService.new.call(@account, target_account, skip_follow_request: true, follow_request_uri: @json['id'])
  13. return
  14. end
  15. follow_request = FollowRequest.create!(account: @account, target_account: target_account, uri: @json['id'])
  16. if target_account.locked?
  17. NotifyService.new.call(target_account, follow_request)
  18. else
  19. AuthorizeFollowService.new.call(@account, target_account)
  20. NotifyService.new.call(target_account, ::Follow.find_by(account: @account, target_account: target_account))
  21. end
  22. end
  23. def reject_follow_request!(target_account)
  24. json = Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new(FollowRequest.new(account: @account, target_account: target_account, uri: @json['id']), serializer: ActivityPub::RejectFollowSerializer, adapter: ActivityPub::Adapter).as_json).sign!(target_account))
  25. ActivityPub::DeliveryWorker.perform_async(json, target_account.id, @account.inbox_url)
  26. end
  27. end