import_worker.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. when 'muting'
  15. process_mutes
  16. end
  17. @import.destroy
  18. end
  19. private
  20. def from_account
  21. @import.account
  22. end
  23. def import_contents
  24. Paperclip.io_adapters.for(@import.data).read
  25. end
  26. def import_rows
  27. CSV.new(import_contents).reject(&:blank?)
  28. end
  29. def process_mutes
  30. import_rows.each do |row|
  31. begin
  32. target_account = FollowRemoteAccountService.new.call(row.first)
  33. next if target_account.nil?
  34. MuteService.new.call(from_account, target_account)
  35. rescue Goldfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError
  36. next
  37. end
  38. end
  39. end
  40. def process_blocks
  41. import_rows.each do |row|
  42. begin
  43. target_account = FollowRemoteAccountService.new.call(row.first)
  44. next if target_account.nil?
  45. BlockService.new.call(from_account, target_account)
  46. rescue Goldfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError
  47. next
  48. end
  49. end
  50. end
  51. def process_follows
  52. import_rows.each do |row|
  53. begin
  54. FollowService.new.call(from_account, row.first)
  55. rescue Mastodon::NotPermittedError, ActiveRecord::RecordNotFound, Goldfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError
  56. next
  57. end
  58. end
  59. end
  60. end