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.

78 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::RawDistributionWorker.perform_async(delete_actor_json, @account.id)
  22. ActivityPub::DeliveryWorker.push_bulk(Relay.enabled.pluck(:inbox_url)) do |inbox_url|
  23. [delete_actor_json, @account.id, inbox_url]
  24. end
  25. end
  26. @account.statuses.reorder(nil).find_in_batches do |statuses|
  27. BatchedRemoveStatusService.new.call(statuses)
  28. end
  29. [
  30. @account.media_attachments,
  31. @account.stream_entries,
  32. @account.notifications,
  33. @account.favourites,
  34. @account.active_relationships,
  35. @account.passive_relationships,
  36. ].each do |association|
  37. destroy_all(association)
  38. end
  39. end
  40. def purge_profile!
  41. @account.suspended = true
  42. @account.display_name = ''
  43. @account.note = ''
  44. @account.statuses_count = 0
  45. @account.avatar.destroy
  46. @account.header.destroy
  47. @account.save!
  48. end
  49. def unsubscribe_push_subscribers!
  50. destroy_all(@account.subscriptions)
  51. end
  52. def destroy_all(association)
  53. association.in_batches.destroy_all
  54. end
  55. def delete_actor_json
  56. return @delete_actor_json if defined?(@delete_actor_json)
  57. payload = ActiveModelSerializers::SerializableResource.new(
  58. @account,
  59. serializer: ActivityPub::DeleteActorSerializer,
  60. adapter: ActivityPub::Adapter
  61. ).as_json
  62. @delete_actor_json = Oj.dump(ActivityPub::LinkedDataSignature.new(payload).sign!(@account))
  63. end
  64. end