2017-03-31 02:42:33 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'csv'
|
|
|
|
|
|
|
|
class ImportWorker
|
|
|
|
include Sidekiq::Worker
|
2017-04-24 07:38:37 +09:00
|
|
|
|
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-10-04 07:39:32 +09:00
|
|
|
Import::RelationshipWorker.push_bulk(import_rows) do |row|
|
|
|
|
[@import.account_id, row.first, relationship_type]
|
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 import_contents
|
|
|
|
Paperclip.io_adapters.for(@import.data).read
|
|
|
|
end
|
2017-03-31 02:42:33 +09:00
|
|
|
|
2017-10-04 07:39:32 +09:00
|
|
|
def relationship_type
|
|
|
|
case @import.type
|
|
|
|
when 'following'
|
|
|
|
'follow'
|
|
|
|
when 'blocking'
|
|
|
|
'block'
|
|
|
|
when 'muting'
|
|
|
|
'mute'
|
2017-03-31 02:42:33 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-04 07:39:32 +09:00
|
|
|
def import_rows
|
2018-10-05 00:36:11 +09:00
|
|
|
rows = CSV.new(import_contents).reject(&:blank?)
|
|
|
|
rows = rows.take(FollowLimitValidator.limit_for_account(@import.account)) if @import.type == 'following'
|
|
|
|
rows
|
2017-03-31 02:42:33 +09:00
|
|
|
end
|
|
|
|
end
|