unsuspend_account_service.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # frozen_string_literal: true
  2. class UnsuspendAccountService < BaseService
  3. include Payloadable
  4. # Restores a recently-unsuspended account
  5. # @param [Account] account Account to restore
  6. def call(account)
  7. @account = account
  8. refresh_remote_account!
  9. return if @account.nil? || @account.suspended?
  10. merge_into_home_timelines!
  11. merge_into_list_timelines!
  12. publish_media_attachments!
  13. distribute_update_actor!
  14. end
  15. private
  16. def refresh_remote_account!
  17. return if @account.local?
  18. # While we had the remote account suspended, it could be that
  19. # it got suspended on its origin, too. So, we need to refresh
  20. # it straight away so it gets marked as remotely suspended in
  21. # that case.
  22. @account.update!(last_webfingered_at: nil)
  23. @account = ResolveAccountService.new.call(@account)
  24. # Worth noting that it is possible that the remote has not only
  25. # been suspended, but deleted permanently, in which case
  26. # @account would now be nil.
  27. end
  28. def distribute_update_actor!
  29. return unless @account.local?
  30. account_reach_finder = AccountReachFinder.new(@account)
  31. ActivityPub::DeliveryWorker.push_bulk(account_reach_finder.inboxes, limit: 1_000) do |inbox_url|
  32. [signed_activity_json, @account.id, inbox_url]
  33. end
  34. end
  35. def merge_into_home_timelines!
  36. @account.followers_for_local_distribution.reorder(nil).find_each do |follower|
  37. FeedManager.instance.merge_into_home(@account, follower)
  38. end
  39. end
  40. def merge_into_list_timelines!
  41. @account.lists_for_local_distribution.reorder(nil).find_each do |list|
  42. FeedManager.instance.merge_into_list(@account, list)
  43. end
  44. end
  45. def publish_media_attachments!
  46. attachment_names = MediaAttachment.attachment_definitions.keys
  47. @account.media_attachments.reorder(nil).find_each do |media_attachment|
  48. attachment_names.each do |attachment_name|
  49. attachment = media_attachment.public_send(attachment_name)
  50. styles = MediaAttachment::DEFAULT_STYLES | attachment.styles.keys
  51. next if attachment.blank?
  52. styles.each do |style|
  53. case Paperclip::Attachment.default_options[:storage]
  54. when :s3
  55. # Prevent useless S3 calls if ACLs are disabled
  56. next if ENV['S3_PERMISSION'] == ''
  57. begin
  58. attachment.s3_object(style).acl.put(acl: Paperclip::Attachment.default_options[:s3_permissions])
  59. rescue Aws::S3::Errors::NoSuchKey
  60. Rails.logger.warn "Tried to change acl on non-existent key #{attachment.s3_object(style).key}"
  61. rescue Aws::S3::Errors::NotImplemented => e
  62. Rails.logger.error "Error trying to change ACL on #{attachment.s3_object(style).key}: #{e.message}"
  63. end
  64. when :fog, :azure
  65. # Not supported
  66. when :filesystem
  67. begin
  68. FileUtils.chmod(0o666 & ~File.umask, attachment.path(style)) unless attachment.path(style).nil?
  69. rescue Errno::ENOENT
  70. Rails.logger.warn "Tried to change permission on non-existent file #{attachment.path(style)}"
  71. end
  72. end
  73. CacheBusterWorker.perform_async(attachment.path(style)) if Rails.configuration.x.cache_buster_enabled
  74. end
  75. end
  76. end
  77. end
  78. def signed_activity_json
  79. @signed_activity_json ||= Oj.dump(serialize_payload(@account, ActivityPub::UpdateSerializer, signer: @account))
  80. end
  81. end