FIX: Include more_topic_url in the response to /categories_and_{latest, top} (#21738)

The field more_topic_url is already included in the response preloaded in categories#index

However this field was missing if a request was subsequently made to update the page using
the end-points /categories_and_latest or /categories_and_top. This could lead the client
app to display incorrect information if it relied on this information to update the UI.
This commit is contained in:
Sérgio Saquetim 2023-05-25 15:24:48 -03:00 committed by GitHub
parent b2e13d1fdd
commit 6a65fa982d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 0 deletions

View File

@ -327,11 +327,16 @@ class CategoriesController < ApplicationController
if topics_filter == :latest
result.topic_list = TopicQuery.new(current_user, topic_options).list_latest
result.topic_list.more_topics_url =
url_for(
public_send("latest_path", sort: topic_options[:order] == "created" ? :created : nil),
)
elsif topics_filter == :top
result.topic_list =
TopicQuery.new(current_user, topic_options).list_top_for(
SiteSetting.top_page_default_timeframe.to_sym,
)
result.topic_list.more_topics_url = url_for(public_send("top_path"))
end
render_serialized(result, CategoryAndTopicListsSerializer, root: false)

View File

@ -244,6 +244,23 @@ RSpec.describe CategoriesController do
[topic1.id, topic3.id, topic2.id],
)
end
it "does not include the sort parameter in more_topics_url" do
# we need to create more topics for more_topics_url to be serialized
SiteSetting.categories_topics = 5
Fabricate.times(
5,
:topic,
category: category,
created_at: 1.day.ago,
bumped_at: 1.day.ago,
)
get "/categories_and_latest.json"
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["more_topics_url"]).to start_with("/latest")
expect(response.parsed_body["topic_list"]["more_topics_url"]).not_to include("sort")
end
end
context "when order is set to created" do
@ -258,6 +275,23 @@ RSpec.describe CategoriesController do
[topic3.id, topic2.id, topic1.id],
)
end
it "includes the sort parameter in more_topics_url" do
# we need to create more topics for more_topics_url to be serialized
SiteSetting.categories_topics = 5
Fabricate.times(
5,
:topic,
category: category,
created_at: 1.day.ago,
bumped_at: 1.day.ago,
)
get "/categories_and_latest.json"
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["more_topics_url"]).to start_with("/latest")
expect(response.parsed_body["topic_list"]["more_topics_url"]).to include("sort=created")
end
end
end
@ -885,6 +919,25 @@ RSpec.describe CategoriesController do
).not_to include(uncategorized.id)
end
it "includes more_topics_url in the response to /categories_and_latest" do
SiteSetting.categories_topics = 5
get "/categories_and_latest.json"
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["more_topics_url"]).to start_with("/latest")
end
it "includes more_topics_url in the response to /categories_and_top" do
SiteSetting.categories_topics = 5
Fabricate.times(10, :topic, category: category, like_count: 1000, posts_count: 100)
TopTopic.refresh!
get "/categories_and_top.json"
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["more_topics_url"]).to start_with("/top")
end
describe "Showing top topics from private categories" do
it "returns the top topic from the private category when the user is a member" do
restricted_group = Fabricate(:group)