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