diaspora/lib/archive_validator.rb
cmrd Senya f85f167f50 Implement archive import backend
This implements archive import feature.

The feature is divided in two main subfeatures: archive validation and archive import.

Archive validation performs different validation on input user archive. This can be
used without actually running import, e.g. when user wants to check the archive
before import from the frontend. Validators may add messages and modify the archive.

Validators are separated in two types: critical validators and non-critical validators.

If validations by critical validators fail it means we can't import archive.

If non-critical validations fail, we can import archive, but some warning messages
are rendered.

Also validators may change archive contents, e.g. when some entity can't be
imported it may be removed from the archive.

Validators' job is to take away complexity from the importer and perform the validations
which are not implemented in other parts of the system, e.g. DB validations or
diaspora_federation entity validations.

Archive importer then takes the modified archive from the validator and imports it.

In order to incapsulate high-level migration logic a MigrationService is
introduced. MigrationService links ArchiveValidator, ArchiveImporter and
AccountMigration.

Also here is introduced a rake task which may be used by podmins to run archive
import.
2019-04-26 18:41:27 +03:00

60 lines
1.4 KiB
Ruby

# frozen_string_literal: true
require "yajl"
# ArchiveValidator checks for errors in archive. It also find non-critical problems and fixes them in the archive hash
# so that the ArchiveImporter doesn't have to handle this issues. Non-critical problems found are indicated as warnings.
# Also it performs necessary data fetch where required.
class ArchiveValidator
include ArchiveImporter::ArchiveHelper
def initialize(archive)
@archive = archive
end
def validate
run_validators(CRITICAL_VALIDATORS, errors)
run_validators(NON_CRITICAL_VALIDATORS, warnings)
rescue KeyError => e
errors.push("Missing mandatory data: #{e}")
rescue Yajl::ParseError => e
errors.push("Bad JSON provided: #{e}")
end
def errors
@errors ||= []
end
def warnings
@warnings ||= []
end
def archive_hash
@archive_hash ||= Yajl::Parser.new.parse(archive)
end
CRITICAL_VALIDATORS = [
SchemaValidator,
AuthorPrivateKeyValidator
].freeze
NON_CRITICAL_VALIDATORS = [
ContactsValidator,
PostsValidator,
RelayablesValidator,
OthersRelayablesValidator
].freeze
private_constant :CRITICAL_VALIDATORS, :NON_CRITICAL_VALIDATORS
private
attr_reader :archive
def run_validators(list, messages)
list.each do |validator_class|
validator = validator_class.new(archive_hash)
messages.concat(validator.messages)
end
end
end