Skip to content

Commit

Permalink
Migration: Importing user blocks
Browse files Browse the repository at this point in the history
Migration: fix error on blocks import
  • Loading branch information
tclaus authored and SuperTux88 committed Jul 12, 2021
1 parent 7896dba commit 1ec7bd7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/archive_importer.rb
Expand Up @@ -18,6 +18,7 @@ def import
import_relayables
import_subscriptions
import_others_relayables
import_blocks
end

def create_user(attr)
Expand Down Expand Up @@ -86,6 +87,10 @@ def import_others_relayables
import_collection(others_relayables, EntityImporter)
end

def import_blocks
import_collection(blocks, BlockImporter)
end

def import_collection(collection, importer_class)
collection.each do |object|
importer_class.new(object, user).import
Expand Down
4 changes: 4 additions & 0 deletions lib/archive_importer/archive_helper.rb
Expand Up @@ -34,6 +34,10 @@ def person
@person ||= Person.find_or_fetch_by_identifier(archive_author_diaspora_id)
end

def blocks
@blocks ||= archive_hash.fetch("user").fetch("blocks", [])
end

def private_key
OpenSSL::PKey::RSA.new(serialized_private_key)
end
Expand Down
30 changes: 30 additions & 0 deletions lib/archive_importer/block_importer.rb
@@ -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 changes: 27 additions & 0 deletions spec/lib/archive_importer/block_importer_spec.rb
@@ -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

0 comments on commit 1ec7bd7

Please sign in to comment.