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