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.

59 lines
1.2 KiB

  1. # frozen_string_literal: true
  2. require 'csv'
  3. class ImportWorker
  4. include Sidekiq::Worker
  5. sidekiq_options queue: 'pull', retry: false
  6. attr_reader :import
  7. def perform(import_id)
  8. @import = Import.find(import_id)
  9. case @import.type
  10. when 'blocking'
  11. process_blocks
  12. when 'following'
  13. process_follows
  14. end
  15. @import.destroy
  16. end
  17. private
  18. def from_account
  19. @import.account
  20. end
  21. def import_contents
  22. Paperclip.io_adapters.for(@import.data).read
  23. end
  24. def import_rows
  25. CSV.new(import_contents).reject(&:blank?)
  26. end
  27. def process_blocks
  28. import_rows.each do |row|
  29. begin
  30. target_account = FollowRemoteAccountService.new.call(row.first)
  31. next if target_account.nil?
  32. BlockService.new.call(from_account, target_account)
  33. rescue Goldfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError
  34. next
  35. end
  36. end
  37. end
  38. def process_follows
  39. import_rows.each do |row|
  40. begin
  41. FollowService.new.call(from_account, row.first)
  42. rescue Mastodon::NotPermittedError, ActiveRecord::RecordNotFound, Goldfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError
  43. next
  44. end
  45. end
  46. end
  47. end