PERF: do not include suggested topics when loading new posts

When a new post is triggered via message bus post stream will attempt to load
it, previously the `/topic/TOPIC_ID/posts.json` would unconditionally include
suggested topics, this caused excessive load on the server.

New pattern defaults to exclude suggested and related topics from this API
unless people explicitly ask for suggested.
This commit is contained in:
Sam 2019-02-22 10:37:18 +11:00
parent f86ca5631a
commit 31d41f532e
4 changed files with 36 additions and 6 deletions

View File

@ -907,10 +907,13 @@ export default RestModel.extend({
},
fetchNextWindow(postNumber, asc, callback) {
let includeSuggested = !this.get("topic.suggested_topics");
const url = `/t/${this.get("topic.id")}/posts.json`;
let data = {
post_number: postNumber,
asc: asc
asc: asc,
include_suggested: includeSuggested
};
data = _.merge(data, this.get("streamFilters"));
@ -950,8 +953,10 @@ export default RestModel.extend({
return Ember.RSVP.resolve([]);
}
let includeSuggested = !this.get("topic.suggested_topics");
const url = "/t/" + this.get("topic.id") + "/posts.json";
const data = { post_ids: postIds };
const data = { post_ids: postIds, include_suggested: includeSuggested };
const store = this.store;
return ajax(url, { data }).then(result => {

View File

@ -197,13 +197,17 @@ class TopicsController < ApplicationController
def posts
params.require(:topic_id)
params.permit(:post_ids, :post_number, :username_filters, :filter)
params.permit(:post_ids, :post_number, :username_filters, :filter, :include_suggested)
include_suggested = params[:include_suggested] == "true"
options = {
filter_post_number: params[:post_number],
post_ids: params[:post_ids],
asc: ActiveRecord::Type::Boolean.new.deserialize(params[:asc]),
filter: params[:filter]
filter: params[:filter],
include_suggested: include_suggested,
include_related: include_suggested,
}
fetch_topic_view(options)

View File

@ -51,6 +51,9 @@ class TopicView
@post_number = [@post_number.to_i, 1].max
@page = [@page.to_i, 1].max
@include_suggested = options.fetch(:include_suggested) { true }
@include_related = options.fetch(:include_related) { true }
@chunk_size =
case
when @print then TopicView.print_chunk_size
@ -402,11 +405,19 @@ class TopicView
end
def suggested_topics
@suggested_topics ||= TopicQuery.new(@user).list_suggested_for(topic, pm_params: pm_params)
if @include_suggested
@suggested_topics ||= TopicQuery.new(@user).list_suggested_for(topic, pm_params: pm_params)
else
nil
end
end
def related_messages
@related_messages ||= TopicQuery.new(@user).list_related_for(topic, pm_params: pm_params)
if @include_related
@related_messages ||= TopicQuery.new(@user).list_related_for(topic, pm_params: pm_params)
else
nil
end
end
# This is pending a larger refactor, that allows custom orders

View File

@ -1720,6 +1720,9 @@ RSpec.describe TopicsController do
let(:topic) { post.topic }
it 'returns first post of the topic' do
# we need one for suggested
create_post
get "/t/#{topic.id}/posts.json"
expect(response.status).to eq(200)
@ -1727,6 +1730,13 @@ RSpec.describe TopicsController do
body = JSON.parse(response.body)
expect(body["post_stream"]["posts"].first["id"]).to eq(post.id)
expect(body["suggested_topics"]).to eq(nil)
get "/t/#{topic.id}/posts.json?include_suggested=true"
body = JSON.parse(response.body)
expect(body["suggested_topics"]).not_to eq(nil)
end
describe 'filtering by post number with filters' do