闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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