delete.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # frozen_string_literal: true
  2. class ActivityPub::Activity::Delete < ActivityPub::Activity
  3. def perform
  4. if @account.uri == object_uri
  5. delete_person
  6. else
  7. delete_note
  8. end
  9. end
  10. private
  11. def delete_person
  12. with_lock("delete_in_progress:#{@account.id}", autorelease: 2.hours, raise_on_failure: false) do
  13. DeleteAccountService.new.call(@account, reserve_username: false, skip_activitypub: true)
  14. end
  15. end
  16. def delete_note
  17. return if object_uri.nil?
  18. with_lock("delete_status_in_progress:#{object_uri}", raise_on_failure: false) do
  19. unless invalid_origin?(object_uri)
  20. # This lock ensures a concurrent `ActivityPub::Activity::Create` either
  21. # does not create a status at all, or has finished saving it to the
  22. # database before we try to load it.
  23. # Without the lock, `delete_later!` could be called after `delete_arrived_first?`
  24. # and `Status.find` before `Status.create!`
  25. with_lock("create:#{object_uri}") { delete_later!(object_uri) }
  26. Tombstone.find_or_create_by(uri: object_uri, account: @account)
  27. end
  28. @status = Status.find_by(uri: object_uri, account: @account)
  29. @status ||= Status.find_by(uri: @object['atomUri'], account: @account) if @object.is_a?(Hash) && @object['atomUri'].present?
  30. return if @status.nil?
  31. forwarder.forward! if forwarder.forwardable?
  32. delete_now!
  33. end
  34. end
  35. def forwarder
  36. @forwarder ||= ActivityPub::Forwarder.new(@account, @json, @status)
  37. end
  38. def delete_now!
  39. RemoveStatusService.new.call(@status, redraft: false)
  40. end
  41. end