follow.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # frozen_string_literal: true
  2. class ActivityPub::Activity::Follow < ActivityPub::Activity
  3. include Payloadable
  4. def perform
  5. target_account = account_from_uri(object_uri)
  6. return if target_account.nil? || !target_account.local? || delete_arrived_first?(@json['id'])
  7. # Update id of already-existing follow requests
  8. existing_follow_request = ::FollowRequest.find_by(account: @account, target_account: target_account)
  9. unless existing_follow_request.nil?
  10. existing_follow_request.update!(uri: @json['id'])
  11. return
  12. end
  13. if target_account.blocking?(@account) || target_account.domain_blocking?(@account.domain) || target_account.moved? || target_account.instance_actor?
  14. reject_follow_request!(target_account)
  15. return
  16. end
  17. # Fast-forward repeat follow requests
  18. existing_follow = ::Follow.find_by(account: @account, target_account: target_account)
  19. unless existing_follow.nil?
  20. existing_follow.update!(uri: @json['id'])
  21. AuthorizeFollowService.new.call(@account, target_account, skip_follow_request: true, follow_request_uri: @json['id'])
  22. return
  23. end
  24. follow_request = FollowRequest.create!(account: @account, target_account: target_account, uri: @json['id'])
  25. if target_account.locked? || @account.silenced?
  26. LocalNotificationWorker.perform_async(target_account.id, follow_request.id, 'FollowRequest', 'follow_request')
  27. else
  28. AuthorizeFollowService.new.call(@account, target_account)
  29. LocalNotificationWorker.perform_async(target_account.id, ::Follow.find_by(account: @account, target_account: target_account).id, 'Follow', 'follow')
  30. end
  31. end
  32. def reject_follow_request!(target_account)
  33. json = Oj.dump(serialize_payload(FollowRequest.new(account: @account, target_account: target_account, uri: @json['id']), ActivityPub::RejectFollowSerializer))
  34. ActivityPub::DeliveryWorker.perform_async(json, target_account.id, @account.inbox_url)
  35. end
  36. end