Merge pull request #8264 from tclaus/migration_importing_blocks
Migration: Importing user blocks
This commit is contained in:
commit
8d5abe8892
5 changed files with 67 additions and 1 deletions
|
|
@ -32,7 +32,7 @@ Although the chat was never enabled per default and was marked as experimental,
|
|||
## Features
|
||||
* Add client-side cropping of profile image uploads [#7581](https://github.com/diaspora/diaspora/pull/7581)
|
||||
* Add client-site rescaling of post images if they exceed the maximum possible size [#7734](https://github.com/diaspora/diaspora/pull/7734)
|
||||
* Add backend for archive import [#7660](https://github.com/diaspora/diaspora/pull/7660) [#8254](https://github.com/diaspora/diaspora/pull/8254)
|
||||
* Add backend for archive import [#7660](https://github.com/diaspora/diaspora/pull/7660) [#8254](https://github.com/diaspora/diaspora/pull/8254) [#8264](https://github.com/diaspora/diaspora/pull/8264)
|
||||
* For pods running PostgreSQL, make sure that no upper-case/mixed-case tags exist, and create a `lower(name)` index on tags to speed up ActsAsTaggableOn [#8206](https://github.com/diaspora/diaspora/pull/8206)
|
||||
* Allow podmins/moderators to see all local public posts to improve moderation [#8232](https://github.com/diaspora/diaspora/pull/8232)
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ class ArchiveImporter
|
|||
import_relayables
|
||||
import_subscriptions
|
||||
import_others_relayables
|
||||
import_blocks
|
||||
end
|
||||
|
||||
def create_user(attr)
|
||||
|
|
@ -86,6 +87,10 @@ class ArchiveImporter
|
|||
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
|
||||
|
|
|
|||
|
|
@ -34,6 +34,10 @@ class ArchiveImporter
|
|||
@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
|
||||
|
|
|
|||
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