move.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # frozen_string_literal: true
  2. class ActivityPub::Activity::Move < ActivityPub::Activity
  3. PROCESSING_COOLDOWN = 7.days.seconds
  4. def perform
  5. return if origin_account.uri != object_uri || processed?
  6. mark_as_processing!
  7. target_account = ActivityPub::FetchRemoteAccountService.new.call(target_uri)
  8. if target_account.nil? || target_account.suspended? || !target_account.also_known_as.include?(origin_account.uri)
  9. unmark_as_processing!
  10. return
  11. end
  12. # In case for some reason we didn't have a redirect for the profile already, set it
  13. origin_account.update(moved_to_account: target_account)
  14. # Initiate a re-follow for each follower
  15. MoveWorker.perform_async(origin_account.id, target_account.id)
  16. end
  17. private
  18. def origin_account
  19. @account
  20. end
  21. def target_uri
  22. value_or_id(@json['target'])
  23. end
  24. def processed?
  25. redis.exists("move_in_progress:#{@account.id}")
  26. end
  27. def mark_as_processing!
  28. redis.setex("move_in_progress:#{@account.id}", PROCESSING_COOLDOWN, true)
  29. end
  30. def unmark_as_processing!
  31. redis.del("move_in_progress:#{@account.id}")
  32. end
  33. end