PERF: Only send down suggested payload when loading last chunk.

This commit is contained in:
Guo Xiang Tan 2017-09-26 14:42:27 +08:00
parent c06b782cab
commit 5d37f8673b
10 changed files with 80 additions and 17 deletions

@ -741,6 +741,11 @@ export default RestModel.extend({
const store = this.store;
return ajax(url, {data}).then(result => {
const posts = Ember.get(result, "post_stream.posts");
if (result.suggested_topics) {
this.set('topic.suggested_topics', result.suggested_topics);
}
if (posts) {
posts.forEach(p => this.storePost(store.createRecord('post', p)));
}

@ -18,13 +18,6 @@ const TopicDetails = RestModel.extend({
});
}
if (details.suggested_topics) {
const store = this.store;
details.suggested_topics = details.suggested_topics.map(function (st) {
return store.createRecord('topic', st);
});
}
if (details.participants) {
details.participants = details.participants.map(function (p) {
p.topic = topic;

@ -99,6 +99,17 @@ const Topic = RestModel.extend({
return newTags;
},
@computed("suggested_topics")
suggestedTopics(suggestedTopics) {
if (suggestedTopics) {
const store = this.store;
return this.set('suggested_topics', suggestedTopics.map(st => {
return store.createRecord('topic', st);
}));
}
},
replyCount: function() {
return this.get('posts_count') - 1;
}.property('posts_count'),

@ -4,9 +4,9 @@
{{basic-topic-list
hideCategory="true"
showPosters="true"
topics=topic.details.suggested_topics}}
topics=topic.suggestedTopics}}
{{else}}
{{basic-topic-list topics=topic.details.suggested_topics}}
{{basic-topic-list topics=topic.suggestedTopics}}
{{/if}}
</div>
<h3 class='suggested-topics-message'>{{{browseMoreMessage}}}</h3>

@ -245,7 +245,7 @@
{{plugin-outlet name="topic-above-suggested" args=(hash model=model)}}
{{#if model.details.suggested_topics.length}}
{{#if model.suggestedTopics.length}}
{{suggested-topics topic=model}}
{{/if}}
{{/if}}

@ -717,7 +717,12 @@ class TopicsController < ApplicationController
return
end
topic_view_serializer = TopicViewSerializer.new(@topic_view, scope: guardian, root: false, include_raw: !!params[:include_raw])
topic_view_serializer = TopicViewSerializer.new(@topic_view,
scope: guardian,
root: false,
include_raw: !!params[:include_raw],
page: params[:page]
)
respond_to do |format|
format.html do

@ -0,0 +1,15 @@
module SuggestedTopicsMixin
def self.included(klass)
klass.attributes :suggested_topics
end
def include_suggested_topics?
object.next_page.nil? && object.suggested_topics&.topics.present?
end
def suggested_topics
object.suggested_topics.topics.map do |t|
SuggestedTopicSerializer.new(t, scope: scope, root: false)
end
end
end

@ -1,5 +1,6 @@
class TopicViewPostsSerializer < ApplicationSerializer
include PostStreamSerializerMixin
include SuggestedTopicsMixin
attributes :id

@ -3,6 +3,7 @@ require_dependency 'new_post_manager'
class TopicViewSerializer < ApplicationSerializer
include PostStreamSerializerMixin
include SuggestedTopicsMixin
include ApplicationHelper
def self.attributes_from_topic(*list)
@ -94,12 +95,6 @@ class TopicViewSerializer < ApplicationSerializer
end
end
if object.suggested_topics&.topics.present?
result[:suggested_topics] = object.suggested_topics.topics.map do |t|
SuggestedTopicSerializer.new(t, scope: scope, root: false)
end
end
if object.links.present?
result[:links] = object.links.map do |user|
TopicLinkSerializer.new(user, scope: scope, root: false)

@ -0,0 +1,38 @@
require 'rails_helper'
describe TopicViewSerializer do
let(:topic) { Fabricate(:topic) }
let(:user) { Fabricate(:user) }
describe '#suggested_topics' do
let(:topic2) { Fabricate(:topic) }
before do
TopicUser.update_last_read(user, topic2.id, 0, 0)
end
describe 'when loading last chunk' do
it 'should include suggested topics' do
topic_view = TopicView.new(topic.id, user)
json = described_class.new(topic_view, scope: Guardian.new(user), root: false).as_json
expect(json[:suggested_topics].first.id).to eq(topic2.id)
end
end
describe 'when not loading last chunk' do
let(:post) { Fabricate(:post, topic: topic) }
let(:post2) { Fabricate(:post, topic: topic) }
it 'should not include suggested topics' do
post
post2
topic_view = TopicView.new(topic.id, user, post_ids: [post.id])
topic_view.next_page
json = described_class.new(topic_view, scope: Guardian.new(user), root: false).as_json
expect(json[:suggested_topics]).to eq(nil)
end
end
end
end