Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
diff --git a/app/serializers/export/others_data_serializer.rb b/app/serializers/export/others_data_serializer.rb
index 96a819c..8769545 100644
--- a/app/serializers/export/others_data_serializer.rb
+++ b/app/serializers/export/others_data_serializer.rb
@@ -11,24 +11,55 @@ module Export
# Authors of posts where we participated and authors are not in contacts
has_many :non_contact_authors, each_serializer: PersonMetadataSerializer
+ def initialize(user_id)
+ @user_id = user_id
+ super(object)
+ end
+
private
+ def object
+ User.find(@user_id)
+ end
+
+ module AlwaysBeFlat
+ def map(*, &block)
+ flat_map {|element| element.map(&block)}
+ end
+ end
+
def relayables
%i[comments likes poll_participations].map {|relayable|
- others_relayables.send(relayable)
- }.sum
+ others_relayables.send(relayable).find_each(batch_size: 20)
+ }.extend AlwaysBeFlat
end
def others_relayables
@others_relayables ||= Diaspora::Exporter::OthersRelayables.new(object.person_id)
end
+ def posts_with_activity
+ Diaspora::Exporter::PostsWithActivity.new(object).query
+ end
+
+ module AlwaysBeFlatAndLazy
+ def map(*, &block)
+ flat_map {|element| element.map(&block)}.force
+ end
+ end
+
def posts
- @posts ||= Diaspora::Exporter::PostsWithActivity.new(object).query
+ posts_with_activity.pluck(:id).each_slice(20).lazy.map {|ids|
+ Post.find(ids)
+ }.extend(AlwaysBeFlatAndLazy)
end
def non_contact_authors
- Diaspora::Exporter::NonContactAuthors.new(posts, object).query
+ Diaspora::Exporter::NonContactAuthors.new(posts_with_activity, object).query.find_each(batch_size: 50)
+ end
+
+ def serializable_data
+ {}
end
end
end
diff --git a/app/serializers/export/own_post_serializer.rb b/app/serializers/export/own_post_serializer.rb
index 150ecd4..39e12d8 100644
--- a/app/serializers/export/own_post_serializer.rb
+++ b/app/serializers/export/own_post_serializer.rb
@@ -27,7 +27,7 @@ module Export
end
def excluded_subscription_key
- entity.public ? :subscribed_users_ids : :subscribed_pods_uris
+ object.public? ? :subscribed_users_ids : :subscribed_pods_uris
end
end
end
diff --git a/app/serializers/export/user_serializer.rb b/app/serializers/export/user_serializer.rb
index 09f26d3..812be23 100644
--- a/app/serializers/export/user_serializer.rb
+++ b/app/serializers/export/user_serializer.rb
@@ -18,10 +18,37 @@ module Export
has_many :relayables, each_serializer: Export::OwnRelayablesSerializer
+ def initialize(user_id)
+ @user_id = user_id
+ super(object)
+ end
+
private
+ def object
+ User.find(@user_id)
+ end
+
+ def posts
+ object.posts.find_each(batch_size: 20)
+ end
+
+ def contacts
+ object.contacts.find_each(batch_size: 100)
+ end
+
+ module AlwaysBeFlat
+ def map(*, &block)
+ flat_map {|element| element.map(&block)}
+ end
+ end
+
def relayables
- [*comments, *likes, *poll_participations]
+ [comments, likes, poll_participations].map {|relayable|
+ relayable.find_each(batch_size: 20)
+ }.tap {|relayables|
+ relayables.extend AlwaysBeFlat
+ }
end
%i[comments likes poll_participations].each {|collection|
@@ -45,5 +72,9 @@ module Export
def post_subscriptions
Post.subscribed_by(object).pluck(:guid)
end
+
+ def serializable_data
+ {}
+ end
end
end
diff --git a/app/serializers/federation_entity_serializer.rb b/app/serializers/federation_entity_serializer.rb
index 0458fc7..cafe142 100644
--- a/app/serializers/federation_entity_serializer.rb
+++ b/app/serializers/federation_entity_serializer.rb
@@ -11,6 +11,6 @@ class FederationEntitySerializer < ActiveModel::Serializer
end
def entity
- @entity ||= Diaspora::Federation::Entities.build(object)
+ Diaspora::Federation::Entities.build(object)
end
end
diff --git a/lib/diaspora/exporter.rb b/lib/diaspora/exporter.rb
index 2c9e279..3586af9 100644
--- a/lib/diaspora/exporter.rb
+++ b/lib/diaspora/exporter.rb
@@ -20,8 +20,8 @@ module Diaspora
def full_archive
{version: SERIALIZED_VERSION}
- .merge(Export::UserSerializer.new(@user).as_json)
- .merge(Export::OthersDataSerializer.new(@user).as_json)
+ .merge(Export::UserSerializer.new(@user.id).as_json)
+ .merge(Export::OthersDataSerializer.new(@user.id).as_json)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment