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