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.
117 lines
2.7 KiB
Ruby
117 lines
2.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class ArchiveImporter
|
|
include ArchiveHelper
|
|
include Diaspora::Logging
|
|
|
|
attr_accessor :user
|
|
|
|
def initialize(archive_hash)
|
|
@archive_hash = archive_hash
|
|
end
|
|
|
|
def import
|
|
import_tag_followings
|
|
import_aspects
|
|
import_contacts
|
|
import_posts
|
|
import_relayables
|
|
import_subscriptions
|
|
import_others_relayables
|
|
end
|
|
|
|
def create_user(attr)
|
|
allowed_keys = %w[
|
|
email strip_exif show_community_spotlight_in_stream language disable_mail auto_follow_back
|
|
]
|
|
data = convert_keys(archive_hash["user"], allowed_keys)
|
|
data.merge!(
|
|
username: attr[:username],
|
|
password: attr[:password],
|
|
password_confirmation: attr[:password]
|
|
)
|
|
self.user = User.build(data)
|
|
user.save!
|
|
end
|
|
|
|
private
|
|
|
|
attr_reader :archive_hash
|
|
|
|
def import_contacts
|
|
import_collection(contacts, ContactImporter)
|
|
end
|
|
|
|
def set_auto_follow_back_aspect
|
|
name = archive_hash["user"]["auto_follow_back_aspect"]
|
|
return if name.nil?
|
|
|
|
aspect = user.aspects.find_by(name: name)
|
|
user.update(auto_follow_back_aspect: aspect) if aspect
|
|
end
|
|
|
|
def import_aspects
|
|
contact_groups.each do |group|
|
|
begin
|
|
user.aspects.create!(group.slice("name", "chat_enabled"))
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
logger.warn "#{self}: #{e}"
|
|
end
|
|
end
|
|
set_auto_follow_back_aspect
|
|
end
|
|
|
|
def import_posts
|
|
import_collection(posts, PostImporter)
|
|
end
|
|
|
|
def import_relayables
|
|
import_collection(relayables, OwnRelayableImporter)
|
|
end
|
|
|
|
def import_others_relayables
|
|
import_collection(others_relayables, EntityImporter)
|
|
end
|
|
|
|
def import_collection(collection, importer_class)
|
|
collection.each do |object|
|
|
importer_class.new(object, user).import
|
|
end
|
|
end
|
|
|
|
def import_tag_followings
|
|
archive_hash.fetch("user").fetch("followed_tags", []).each do |tag_name|
|
|
begin
|
|
tag = ActsAsTaggableOn::Tag.find_or_create_by(name: tag_name)
|
|
user.tag_followings.create!(tag: tag)
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
logger.warn "#{self}: #{e}"
|
|
end
|
|
end
|
|
end
|
|
|
|
def import_subscriptions
|
|
post_subscriptions.each do |post_guid|
|
|
post = Post.find_or_fetch_by(archive_author_diaspora_id, post_guid)
|
|
if post.nil?
|
|
logger.warn "#{self}: post with guid #{post_guid} not found, can't subscribe"
|
|
next
|
|
end
|
|
begin
|
|
user.participations.create!(target: post)
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
logger.warn "#{self}: #{e}"
|
|
end
|
|
end
|
|
end
|
|
|
|
def convert_keys(hash, allowed_keys)
|
|
hash
|
|
.slice(*allowed_keys)
|
|
.symbolize_keys
|
|
end
|
|
|
|
def to_s
|
|
"#{self.class}:#{archive_author_diaspora_id}:#{user.diaspora_handle}"
|
|
end
|
|
end
|