You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.8 KiB

  1. # frozen_string_literal: true
  2. class SuspendAccountService < BaseService
  3. def call(account, **options)
  4. @account = account
  5. @options = options
  6. purge_user!
  7. purge_profile!
  8. purge_content!
  9. unsubscribe_push_subscribers!
  10. end
  11. private
  12. def purge_user!
  13. if @options[:remove_user]
  14. @account.user&.destroy
  15. else
  16. @account.user&.disable!
  17. end
  18. end
  19. def purge_content!
  20. if @account.local?
  21. ActivityPub::DeliveryWorker.push_bulk(delivery_inboxes) do |inbox_url|
  22. [delete_actor_json, @account.id, inbox_url]
  23. end
  24. end
  25. @account.statuses.reorder(nil).find_in_batches do |statuses|
  26. BatchedRemoveStatusService.new.call(statuses)
  27. end
  28. [
  29. @account.media_attachments,
  30. @account.stream_entries,
  31. @account.notifications,
  32. @account.favourites,
  33. @account.active_relationships,
  34. @account.passive_relationships,
  35. ].each do |association|
  36. destroy_all(association)
  37. end
  38. end
  39. def purge_profile!
  40. @account.suspended = true
  41. @account.display_name = ''
  42. @account.note = ''
  43. @account.statuses_count = 0
  44. @account.avatar.destroy
  45. @account.header.destroy
  46. @account.save!
  47. end
  48. def unsubscribe_push_subscribers!
  49. destroy_all(@account.subscriptions)
  50. end
  51. def destroy_all(association)
  52. association.in_batches.destroy_all
  53. end
  54. def delete_actor_json
  55. return @delete_actor_json if defined?(@delete_actor_json)
  56. payload = ActiveModelSerializers::SerializableResource.new(
  57. @account,
  58. serializer: ActivityPub::DeleteActorSerializer,
  59. adapter: ActivityPub::Adapter
  60. ).as_json
  61. @delete_actor_json = Oj.dump(ActivityPub::LinkedDataSignature.new(payload).sign!(@account))
  62. end
  63. def delivery_inboxes
  64. Account.inboxes + Relay.enabled.pluck(:inbox_url)
  65. end
  66. end