about summary refs log tree commit diff
path: root/app/services/import_service.rb
diff options
context:
space:
mode:
authorThibaut Girka <thib@sitedethib.com>2019-02-10 20:57:51 +0100
committerThibaut Girka <thib@sitedethib.com>2019-02-10 21:10:09 +0100
commitbf94a43496210bd99c5344f10f7fbf3c49fd15b8 (patch)
tree177aea725d51ff379a0d5cc7d09cc7118696cb07 /app/services/import_service.rb
parent1a70fca168bec82e3521c2039bbdecc256f328af (diff)
parent3031b8a8f22a74a1b691cfe8a31a816ae9cc480d (diff)
Merge branch 'master' into glitch-soc/merge-upstream
Conflicts:
- app/controllers/oauth/authorized_applications_controller.rb
  Two changes too close to each other
- app/controllers/settings/sessions_controller.rb
- app/lib/user_settings_decorator.rb
  Two changes too close to each other
- app/models/media_attachment.rb
  New changes too close to glitch-soc only changes.
- app/models/user.rb
  Two changes too close to each other.
- app/services/remove_status_service.rb
  Kept direct timeline code which had been removed upstream.
- app/views/settings/preferences/show.html.haml
  Two changes too close to each other.
- config/locales/en.yml
  Introduction of a new string too close to glitch-soc-only's “flavour”
- config/locales/ja.yml
  Introduction of a new string too close to glitch-soc-only's “flavour”
- config/locales/pl.yml
  Introduction of a new string too close to glitch-soc-only's “flavour”
- config/locales/simple_form.en.yml
  Introduction of a new string too close to glitch-soc-only's “skin”
- config/locales/simple_form.pl.yml
  Introduction of a new string too close to glitch-soc-only's “skin”
- config/settings.yml
  Reverted upstream's decision of enabling posting application by default.
Diffstat (limited to 'app/services/import_service.rb')
-rw-r--r--app/services/import_service.rb90
1 files changed, 90 insertions, 0 deletions
diff --git a/app/services/import_service.rb b/app/services/import_service.rb
new file mode 100644
index 000000000..3f558626e
--- /dev/null
+++ b/app/services/import_service.rb
@@ -0,0 +1,90 @@
+# frozen_string_literal: true
+
+require 'csv'
+
+class ImportService < BaseService
+  ROWS_PROCESSING_LIMIT = 20_000
+
+  def call(import)
+    @import  = import
+    @account = @import.account
+    @data    = CSV.new(import_data).reject(&:blank?)
+
+    case @import.type
+    when 'following'
+      import_follows!
+    when 'blocking'
+      import_blocks!
+    when 'muting'
+      import_mutes!
+    when 'domain_blocking'
+      import_domain_blocks!
+    end
+  end
+
+  private
+
+  def import_follows!
+    import_relationships!('follow', 'unfollow', @account.following, follow_limit)
+  end
+
+  def import_blocks!
+    import_relationships!('block', 'unblock', @account.blocking, ROWS_PROCESSING_LIMIT)
+  end
+
+  def import_mutes!
+    import_relationships!('mute', 'unmute', @account.muting, ROWS_PROCESSING_LIMIT)
+  end
+
+  def import_domain_blocks!
+    items = @data.take(ROWS_PROCESSING_LIMIT).map { |row| row.first.strip }
+
+    if @import.overwrite?
+      presence_hash = items.each_with_object({}) { |id, mapping| mapping[id] = true }
+
+      @account.domain_blocks.find_each do |domain_block|
+        if presence_hash[domain_block.domain]
+          items.delete(domain_block.domain)
+        else
+          @account.unblock_domain!(domain_block.domain)
+        end
+      end
+    end
+
+    items.each do |domain|
+      @account.block_domain!(domain)
+    end
+
+    AfterAccountDomainBlockWorker.push_bulk(items) do |domain|
+      [@account.id, domain]
+    end
+  end
+
+  def import_relationships!(action, undo_action, overwrite_scope, limit)
+    items = @data.take(limit).map { |row| row.first.strip }
+
+    if @import.overwrite?
+      presence_hash = items.each_with_object({}) { |id, mapping| mapping[id] = true }
+
+      overwrite_scope.find_each do |target_account|
+        if presence_hash[target_account.acct]
+          items.delete(target_account.acct)
+        else
+          Import::RelationshipWorker.perform_async(@account.id, target_account.acct, undo_action)
+        end
+      end
+    end
+
+    Import::RelationshipWorker.push_bulk(items) do |acct|
+      [@account.id, acct, action]
+    end
+  end
+
+  def import_data
+    Paperclip.io_adapters.for(@import.data).read
+  end
+
+  def follow_limit
+    FollowLimitValidator.limit_for_account(@account)
+  end
+end