Rewrite import feature (#21054)
This commit is contained in:
60
app/services/bulk_import_row_service.rb
Normal file
60
app/services/bulk_import_row_service.rb
Normal file
@@ -0,0 +1,60 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class BulkImportRowService
|
||||
def call(row)
|
||||
@account = row.bulk_import.account
|
||||
@data = row.data
|
||||
@type = row.bulk_import.type.to_sym
|
||||
|
||||
case @type
|
||||
when :following, :blocking, :muting
|
||||
target_acct = @data['acct']
|
||||
target_domain = domain(target_acct)
|
||||
@target_account = stoplight_wrap_request(target_domain) { ResolveAccountService.new.call(target_acct, { check_delivery_availability: true }) }
|
||||
return false if @target_account.nil?
|
||||
when :bookmarks
|
||||
target_uri = @data['uri']
|
||||
target_domain = Addressable::URI.parse(target_uri).normalized_host
|
||||
@target_status = ActivityPub::TagManager.instance.uri_to_resource(target_uri, Status)
|
||||
return false if @target_status.nil? && ActivityPub::TagManager.instance.local_uri?(target_uri)
|
||||
|
||||
@target_status ||= stoplight_wrap_request(target_domain) { ActivityPub::FetchRemoteStatusService.new.call(target_uri) }
|
||||
return false if @target_status.nil?
|
||||
end
|
||||
|
||||
case @type
|
||||
when :following
|
||||
FollowService.new.call(@account, @target_account, reblogs: @data['show_reblogs'], notify: @data['notify'], languages: @data['languages'])
|
||||
when :blocking
|
||||
BlockService.new.call(@account, @target_account)
|
||||
when :muting
|
||||
MuteService.new.call(@account, @target_account, notifications: @data['hide_notifications'])
|
||||
when :bookmarks
|
||||
return false unless StatusPolicy.new(@account, @target_status).show?
|
||||
|
||||
@account.bookmarks.find_or_create_by!(status: @target_status)
|
||||
end
|
||||
|
||||
true
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
false
|
||||
end
|
||||
|
||||
def domain(uri)
|
||||
domain = uri.is_a?(Account) ? uri.domain : uri.split('@')[1]
|
||||
TagManager.instance.local_domain?(domain) ? nil : TagManager.instance.normalize_domain(domain)
|
||||
end
|
||||
|
||||
def stoplight_wrap_request(domain, &block)
|
||||
if domain.present?
|
||||
Stoplight("source:#{domain}", &block)
|
||||
.with_fallback { nil }
|
||||
.with_threshold(1)
|
||||
.with_cool_off_time(5.minutes.seconds)
|
||||
.with_error_handler { |error, handle| error.is_a?(HTTP::Error) || error.is_a?(OpenSSL::SSL::SSLError) ? handle.call(error) : raise(error) }
|
||||
.run
|
||||
else
|
||||
yield
|
||||
end
|
||||
end
|
||||
end
|
160
app/services/bulk_import_service.rb
Normal file
160
app/services/bulk_import_service.rb
Normal file
@@ -0,0 +1,160 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class BulkImportService < BaseService
|
||||
def call(import)
|
||||
@import = import
|
||||
@account = @import.account
|
||||
|
||||
case @import.type.to_sym
|
||||
when :following
|
||||
import_follows!
|
||||
when :blocking
|
||||
import_blocks!
|
||||
when :muting
|
||||
import_mutes!
|
||||
when :domain_blocking
|
||||
import_domain_blocks!
|
||||
when :bookmarks
|
||||
import_bookmarks!
|
||||
end
|
||||
|
||||
@import.update!(state: :finished, finished_at: Time.now.utc) if @import.processed_items == @import.total_items
|
||||
rescue
|
||||
@import.update!(state: :finished, finished_at: Time.now.utc)
|
||||
|
||||
raise
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def extract_rows_by_acct
|
||||
local_domain_suffix = "@#{Rails.configuration.x.local_domain}"
|
||||
@import.rows.to_a.index_by { |row| row.data['acct'].delete_suffix(local_domain_suffix) }
|
||||
end
|
||||
|
||||
def import_follows!
|
||||
rows_by_acct = extract_rows_by_acct
|
||||
|
||||
if @import.overwrite?
|
||||
@account.following.find_each do |followee|
|
||||
row = rows_by_acct.delete(followee.acct)
|
||||
|
||||
if row.nil?
|
||||
UnfollowService.new.call(@account, followee)
|
||||
else
|
||||
row.destroy
|
||||
@import.processed_items += 1
|
||||
@import.imported_items += 1
|
||||
|
||||
# Since we're updating the settings of an existing relationship, we can safely call
|
||||
# FollowService directly
|
||||
FollowService.new.call(@account, followee, reblogs: row.data['show_reblogs'], notify: row.data['notify'], languages: row.data['languages'])
|
||||
end
|
||||
end
|
||||
|
||||
# Save pending infos due to `overwrite?` handling
|
||||
@import.save!
|
||||
end
|
||||
|
||||
Import::RowWorker.push_bulk(rows_by_acct.values) do |row|
|
||||
[row.id]
|
||||
end
|
||||
end
|
||||
|
||||
def import_blocks!
|
||||
rows_by_acct = extract_rows_by_acct
|
||||
|
||||
if @import.overwrite?
|
||||
@account.blocking.find_each do |blocked_account|
|
||||
row = rows_by_acct.delete(blocked_account.acct)
|
||||
|
||||
if row.nil?
|
||||
UnblockService.new.call(@account, blocked_account)
|
||||
else
|
||||
row.destroy
|
||||
@import.processed_items += 1
|
||||
@import.imported_items += 1
|
||||
BlockService.new.call(@account, blocked_account)
|
||||
end
|
||||
end
|
||||
|
||||
# Save pending infos due to `overwrite?` handling
|
||||
@import.save!
|
||||
end
|
||||
|
||||
Import::RowWorker.push_bulk(rows_by_acct.values) do |row|
|
||||
[row.id]
|
||||
end
|
||||
end
|
||||
|
||||
def import_mutes!
|
||||
rows_by_acct = extract_rows_by_acct
|
||||
|
||||
if @import.overwrite?
|
||||
@account.muting.find_each do |muted_account|
|
||||
row = rows_by_acct.delete(muted_account.acct)
|
||||
|
||||
if row.nil?
|
||||
UnmuteService.new.call(@account, muted_account)
|
||||
else
|
||||
row.destroy
|
||||
@import.processed_items += 1
|
||||
@import.imported_items += 1
|
||||
MuteService.new.call(@account, muted_account, notifications: row.data['hide_notifications'])
|
||||
end
|
||||
end
|
||||
|
||||
# Save pending infos due to `overwrite?` handling
|
||||
@import.save!
|
||||
end
|
||||
|
||||
Import::RowWorker.push_bulk(rows_by_acct.values) do |row|
|
||||
[row.id]
|
||||
end
|
||||
end
|
||||
|
||||
def import_domain_blocks!
|
||||
domains = @import.rows.map { |row| row.data['domain'] }
|
||||
|
||||
if @import.overwrite?
|
||||
@account.domain_blocks.find_each do |domain_block|
|
||||
domain = domains.delete(domain_block)
|
||||
|
||||
@account.unblock_domain!(domain_block.domain) if domain.nil?
|
||||
end
|
||||
end
|
||||
|
||||
@import.rows.delete_all
|
||||
domains.each { |domain| @account.block_domain!(domain) }
|
||||
@import.update!(processed_items: @import.total_items, imported_items: @import.total_items)
|
||||
|
||||
AfterAccountDomainBlockWorker.push_bulk(domains) do |domain|
|
||||
[@account.id, domain]
|
||||
end
|
||||
end
|
||||
|
||||
def import_bookmarks!
|
||||
rows_by_uri = @import.rows.index_by { |row| row.data['uri'] }
|
||||
|
||||
if @import.overwrite?
|
||||
@account.bookmarks.includes(:status).find_each do |bookmark|
|
||||
row = rows_by_uri.delete(ActivityPub::TagManager.instance.uri_for(bookmark.status))
|
||||
|
||||
if row.nil?
|
||||
bookmark.destroy!
|
||||
else
|
||||
row.destroy
|
||||
@import.processed_items += 1
|
||||
@import.imported_items += 1
|
||||
end
|
||||
end
|
||||
|
||||
# Save pending infos due to `overwrite?` handling
|
||||
@import.save!
|
||||
end
|
||||
|
||||
Import::RowWorker.push_bulk(rows_by_uri.values) do |row|
|
||||
[row.id]
|
||||
end
|
||||
end
|
||||
end
|
@@ -2,6 +2,9 @@
|
||||
|
||||
require 'csv'
|
||||
|
||||
# NOTE: This is a deprecated service, only kept to not break ongoing imports
|
||||
# on upgrade. See `BulkImportService` for its replacement.
|
||||
|
||||
class ImportService < BaseService
|
||||
ROWS_PROCESSING_LIMIT = 20_000
|
||||
|
||||
|
Reference in New Issue
Block a user