delete.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. lock_or_return("delete_in_progress:#{@account.id}") do
  13. SuspendAccountService.new.call(@account)
  14. @account.destroy!
  15. end
  16. end
  17. def delete_note
  18. return if object_uri.nil?
  19. unless invalid_origin?(object_uri)
  20. RedisLock.acquire(lock_options) { |_lock| delete_later!(object_uri) }
  21. Tombstone.find_or_create_by(uri: object_uri, account: @account)
  22. end
  23. @status = Status.find_by(uri: object_uri, account: @account)
  24. @status ||= Status.find_by(uri: @object['atomUri'], account: @account) if @object.is_a?(Hash) && @object['atomUri'].present?
  25. return if @status.nil?
  26. if @status.public_visibility? || @status.unlisted_visibility?
  27. forward_for_reply
  28. forward_for_reblogs
  29. end
  30. delete_now!
  31. end
  32. def forward_for_reblogs
  33. return if @json['signature'].blank?
  34. rebloggers_ids = @status.reblogs.includes(:account).references(:account).merge(Account.local).pluck(:account_id)
  35. inboxes = Account.where(id: ::Follow.where(target_account_id: rebloggers_ids).select(:account_id)).inboxes - [@account.preferred_inbox_url]
  36. ActivityPub::DeliveryWorker.push_bulk(inboxes) do |inbox_url|
  37. [payload, rebloggers_ids.first, inbox_url]
  38. end
  39. end
  40. def replied_to_status
  41. return @replied_to_status if defined?(@replied_to_status)
  42. @replied_to_status = @status.thread
  43. end
  44. def reply_to_local?
  45. !replied_to_status.nil? && replied_to_status.account.local?
  46. end
  47. def forward_for_reply
  48. return unless @json['signature'].present? && reply_to_local?
  49. ActivityPub::RawDistributionWorker.perform_async(Oj.dump(@json), replied_to_status.account_id, [@account.preferred_inbox_url])
  50. end
  51. def delete_now!
  52. RemoveStatusService.new.call(@status)
  53. end
  54. def payload
  55. @payload ||= Oj.dump(@json)
  56. end
  57. def lock_options
  58. { redis: Redis.current, key: "create:#{object_uri}" }
  59. end
  60. def invalid_origin?(url)
  61. return true if unsupported_uri_scheme?(url)
  62. needle = Addressable::URI.parse(url).host
  63. haystack = Addressable::URI.parse(@account.uri).host
  64. !haystack.casecmp(needle).zero?
  65. end
  66. end