From bad6a5142cd103b95a64250b3b6e89539187e279 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 22 Jun 2018 10:38:31 +0800 Subject: [PATCH] PERF: Don't include entire post stream when we're loading more posts. --- .../discourse/models/post-stream.js.es6 | 5 ++-- .../post_stream_serializer_mixin.rb | 8 ++++-- .../topic_view_posts_serializer.rb | 8 ++++++ .../topic_view_posts_serializer_spec.rb | 26 +++++++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 spec/serializers/topic_view_posts_serializer_spec.rb diff --git a/app/assets/javascripts/discourse/models/post-stream.js.es6 b/app/assets/javascripts/discourse/models/post-stream.js.es6 index a2a30048314..7b4b27249ea 100644 --- a/app/assets/javascripts/discourse/models/post-stream.js.es6 +++ b/app/assets/javascripts/discourse/models/post-stream.js.es6 @@ -854,13 +854,14 @@ export default RestModel.extend({ const url = "/t/" + this.get("topic.id") + "/posts.json"; const data = { post_ids: postIds }; const store = this.store; - return ajax(url, { data }).then(result => { - const posts = Ember.get(result, "post_stream.posts"); + return ajax(url, { data }).then(result => { if (result.suggested_topics) { this.set("topic.suggested_topics", result.suggested_topics); } + const posts = Ember.get(result, "post_stream.posts"); + if (posts) { posts.forEach(p => this.storePost(store.createRecord("post", p))); } diff --git a/app/serializers/post_stream_serializer_mixin.rb b/app/serializers/post_stream_serializer_mixin.rb index 26213f059c8..02f9b582cc8 100644 --- a/app/serializers/post_stream_serializer_mixin.rb +++ b/app/serializers/post_stream_serializer_mixin.rb @@ -3,14 +3,18 @@ require_dependency 'post_serializer' require_dependency 'timeline_lookup' module PostStreamSerializerMixin - def self.included(klass) klass.attributes :post_stream klass.attributes :timeline_lookup end + def include_stream? + true + end + def post_stream - result = { posts: posts, stream: object.filtered_post_ids } + result = { posts: posts } + result[:stream] = object.filtered_post_ids if include_stream? result[:gaps] = GapSerializer.new(object.gaps, root: false) if object.gaps.present? result end diff --git a/app/serializers/topic_view_posts_serializer.rb b/app/serializers/topic_view_posts_serializer.rb index 98c6cabff76..b51acf525f3 100644 --- a/app/serializers/topic_view_posts_serializer.rb +++ b/app/serializers/topic_view_posts_serializer.rb @@ -8,4 +8,12 @@ class TopicViewPostsSerializer < ApplicationSerializer object.topic.id end + def include_stream? + false + end + + def include_timeline_lookup? + false + end + end diff --git a/spec/serializers/topic_view_posts_serializer_spec.rb b/spec/serializers/topic_view_posts_serializer_spec.rb new file mode 100644 index 00000000000..f69b4080645 --- /dev/null +++ b/spec/serializers/topic_view_posts_serializer_spec.rb @@ -0,0 +1,26 @@ +require 'rails_helper' + +RSpec.describe TopicViewPostsSerializer do + let(:user) { Fabricate(:user) } + let(:post) { Fabricate(:post) } + let(:topic) { post.topic } + let(:topic_view) { TopicView.new(topic, user, post_ids: [post.id]) } + + subject do + described_class.new(topic_view, + scope: Guardian.new(Fabricate(:admin)), + root: false + ) + end + + it 'should return the right attributes' do + body = JSON.parse(subject.to_json) + + posts = body["post_stream"]["posts"] + + expect(posts.count).to eq(1) + expect(posts.first["id"]).to eq(post.id) + expect(body["post_stream"]["stream"]).to eq(nil) + expect(body["post_stream"]["timeline_lookup"]).to eq(nil) + end +end