Skip to content

Commit

Permalink
Refactor imports
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire committed Apr 7, 2019
1 parent 67b3b62 commit ea1c26a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
10 changes: 5 additions & 5 deletions app/services/import_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def import_blocks!

def import_mutes!
parse_import_data!(['Account address'])
import_relationships!('mute', 'unmute', @account.muting, ROWS_PROCESSING_LIMIT)
import_relationships!('mute', 'unmute', @account.muting, ROWS_PROCESSING_LIMIT, notifications: 'Hide notifications')
end

def import_domain_blocks!
Expand Down Expand Up @@ -63,8 +63,8 @@ def import_domain_blocks!
end
end

def import_relationships!(action, undo_action, overwrite_scope, limit)
items = @data.take(limit).map { |row| [row['Account address']&.strip, row['Hide notifications']&.strip] }.reject { |(id, _)| id.blank? }
def import_relationships!(action, undo_action, overwrite_scope, limit, extra_fields = {})
items = @data.take(limit).map { |row| [row['Account address']&.strip, Hash[extra_fields.map { |key, header| [key, row[header]&.strip] }]] }.reject { |(id, _)| id.blank? }

if @import.overwrite?
presence_hash = items.each_with_object({}) { |(id, extra), mapping| mapping[id] = [true, extra] }
Expand All @@ -73,15 +73,15 @@ def import_relationships!(action, undo_action, overwrite_scope, limit)
if presence_hash[target_account.acct]
items.delete(target_account.acct)
extra = presence_hash[target_account.acct][1]
Import::RelationshipWorker.perform_async(@account.id, target_account.acct, action, ActiveModel::Type::Boolean.new.cast(extra))
Import::RelationshipWorker.perform_async(@account.id, target_account.acct, action, extra)
else
Import::RelationshipWorker.perform_async(@account.id, target_account.acct, undo_action)
end
end
end

Import::RelationshipWorker.push_bulk(items) do |acct, extra|
[@account.id, acct, action, ActiveModel::Type::Boolean.new.cast(extra)]
[@account.id, acct, action, extra]
end
end

Expand Down
5 changes: 3 additions & 2 deletions app/workers/import/relationship_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ class Import::RelationshipWorker

sidekiq_options queue: 'pull', retry: 8, dead: false

def perform(account_id, target_account_uri, relationship, extra = nil)
def perform(account_id, target_account_uri, relationship, options = {})
from_account = Account.find(account_id)
target_account = ResolveAccountService.new.call(target_account_uri)
options.symbolize_keys!

return if target_account.nil?

Expand All @@ -21,7 +22,7 @@ def perform(account_id, target_account_uri, relationship, extra = nil)
when 'unblock'
UnblockService.new.call(from_account, target_account)
when 'mute'
MuteService.new.call(from_account, target_account, notifications: extra)
MuteService.new.call(from_account, target_account, options)
when 'unmute'
UnmuteService.new.call(from_account, target_account)
end
Expand Down

0 comments on commit ea1c26a

Please sign in to comment.