PERF: Don't include entire post stream when we're loading more posts.

This commit is contained in:
Guo Xiang Tan 2018-06-22 10:38:31 +08:00
parent 657c8a013a
commit bad6a5142c
4 changed files with 43 additions and 4 deletions

View File

@ -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)));
}

View File

@ -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

View File

@ -8,4 +8,12 @@ class TopicViewPostsSerializer < ApplicationSerializer
object.topic.id
end
def include_stream?
false
end
def include_timeline_lookup?
false
end
end

View File

@ -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