Migration: Importing user blocks
Migration: fix error on blocks import
This commit is contained in:
parent
7896dbada5
commit
1ec7bd7ce1
4 changed files with 66 additions and 0 deletions
|
|
@ -18,6 +18,7 @@ class ArchiveImporter
|
||||||
import_relayables
|
import_relayables
|
||||||
import_subscriptions
|
import_subscriptions
|
||||||
import_others_relayables
|
import_others_relayables
|
||||||
|
import_blocks
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_user(attr)
|
def create_user(attr)
|
||||||
|
|
@ -86,6 +87,10 @@ class ArchiveImporter
|
||||||
import_collection(others_relayables, EntityImporter)
|
import_collection(others_relayables, EntityImporter)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def import_blocks
|
||||||
|
import_collection(blocks, BlockImporter)
|
||||||
|
end
|
||||||
|
|
||||||
def import_collection(collection, importer_class)
|
def import_collection(collection, importer_class)
|
||||||
collection.each do |object|
|
collection.each do |object|
|
||||||
importer_class.new(object, user).import
|
importer_class.new(object, user).import
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,10 @@ class ArchiveImporter
|
||||||
@person ||= Person.find_or_fetch_by_identifier(archive_author_diaspora_id)
|
@person ||= Person.find_or_fetch_by_identifier(archive_author_diaspora_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def blocks
|
||||||
|
@blocks ||= archive_hash.fetch("user").fetch("blocks", [])
|
||||||
|
end
|
||||||
|
|
||||||
def private_key
|
def private_key
|
||||||
OpenSSL::PKey::RSA.new(serialized_private_key)
|
OpenSSL::PKey::RSA.new(serialized_private_key)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
30
lib/archive_importer/block_importer.rb
Normal file
30
lib/archive_importer/block_importer.rb
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class ArchiveImporter
|
||||||
|
class BlockImporter
|
||||||
|
include Diaspora::Logging
|
||||||
|
attr_reader :json, :user
|
||||||
|
|
||||||
|
def initialize(json, user)
|
||||||
|
@json = json
|
||||||
|
@user = user
|
||||||
|
end
|
||||||
|
|
||||||
|
def import
|
||||||
|
p = Person.find_or_fetch_by_identifier(json)
|
||||||
|
migrant_person = handle_migrant_person(p)
|
||||||
|
user.blocks.create(person_id: migrant_person.id)
|
||||||
|
rescue ActiveRecord::RecordInvalid,
|
||||||
|
DiasporaFederation::Discovery::DiscoveryError => e
|
||||||
|
logger.warn "#{self}: #{e}"
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def handle_migrant_person(person)
|
||||||
|
return person if person.account_migration.nil?
|
||||||
|
|
||||||
|
person.account_migration.newest_person
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
27
spec/lib/archive_importer/block_importer_spec.rb
Normal file
27
spec/lib/archive_importer/block_importer_spec.rb
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
describe ArchiveImporter::BlockImporter do
|
||||||
|
let(:target) { FactoryBot.create(:user) }
|
||||||
|
|
||||||
|
describe "#import" do
|
||||||
|
it "adds a block entry to the user" do
|
||||||
|
person_to_block = FactoryBot.create(:person)
|
||||||
|
block_importer = ArchiveImporter::BlockImporter.new(person_to_block.diaspora_handle, target)
|
||||||
|
block_importer.import
|
||||||
|
|
||||||
|
expect(target.blocks.count).to eq(1)
|
||||||
|
expect(target.blocks.first.person_id).to eq(person_to_block.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "handles unfetchable person to block" do
|
||||||
|
expect_any_instance_of(DiasporaFederation::Discovery::Discovery).to receive(:fetch_and_save).and_raise(
|
||||||
|
DiasporaFederation::Discovery::DiscoveryError, "discovery error reasons"
|
||||||
|
)
|
||||||
|
|
||||||
|
block_importer = ArchiveImporter::BlockImporter.new("unknown_person@bad_pod.tld", target)
|
||||||
|
block_importer.import
|
||||||
|
|
||||||
|
expect(target.blocks).to be_empty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in a new issue