2017-03-31 02:42:33 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'csv'
|
|
|
|
|
|
|
|
class ImportWorker
|
|
|
|
include Sidekiq::Worker
|
2017-04-04 07:53:20 +09:00
|
|
|
sidekiq_options queue: 'pull', retry: false
|
2017-03-31 02:42:33 +09:00
|
|
|
|
2017-04-12 04:40:14 +09:00
|
|
|
attr_reader :import
|
|
|
|
|
2017-03-31 02:42:33 +09:00
|
|
|
def perform(import_id)
|
2017-04-12 04:40:14 +09:00
|
|
|
@import = Import.find(import_id)
|
2017-03-31 02:42:33 +09:00
|
|
|
|
2017-04-12 04:40:14 +09:00
|
|
|
case @import.type
|
2017-03-31 02:42:33 +09:00
|
|
|
when 'blocking'
|
2017-04-12 04:40:14 +09:00
|
|
|
process_blocks
|
2017-03-31 02:42:33 +09:00
|
|
|
when 'following'
|
2017-04-12 04:40:14 +09:00
|
|
|
process_follows
|
2017-03-31 02:42:33 +09:00
|
|
|
end
|
|
|
|
|
2017-04-12 04:40:14 +09:00
|
|
|
@import.destroy
|
2017-03-31 02:42:33 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-04-12 04:40:14 +09:00
|
|
|
def from_account
|
|
|
|
@import.account
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_contents
|
|
|
|
Paperclip.io_adapters.for(@import.data).read
|
|
|
|
end
|
2017-03-31 02:42:33 +09:00
|
|
|
|
2017-04-12 04:40:14 +09:00
|
|
|
def import_rows
|
|
|
|
CSV.new(import_contents).reject(&:blank?)
|
|
|
|
end
|
2017-03-31 02:42:33 +09:00
|
|
|
|
2017-04-12 04:40:14 +09:00
|
|
|
def process_blocks
|
|
|
|
import_rows.each do |row|
|
2017-03-31 02:42:33 +09:00
|
|
|
begin
|
2017-04-12 04:40:14 +09:00
|
|
|
target_account = FollowRemoteAccountService.new.call(row.first)
|
2017-03-31 02:42:33 +09:00
|
|
|
next if target_account.nil?
|
|
|
|
BlockService.new.call(from_account, target_account)
|
|
|
|
rescue Goldfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-12 04:40:14 +09:00
|
|
|
def process_follows
|
|
|
|
import_rows.each do |row|
|
2017-03-31 02:42:33 +09:00
|
|
|
begin
|
2017-04-12 04:40:14 +09:00
|
|
|
FollowService.new.call(from_account, row.first)
|
2017-04-06 09:04:13 +09:00
|
|
|
rescue Mastodon::NotPermittedError, ActiveRecord::RecordNotFound, Goldfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError
|
2017-03-31 02:42:33 +09:00
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|