A platform for community discussion. Free, open, simple.
FIX: Honour any registered filter as a category `default_view` (#42736)
Régis Hanol13ae50c1b161
diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rbindex 1bb81ab118e..dada9172797 100644--- a/app/controllers/list_controller.rb+++ b/app/controllers/list_controller.rb@@ -9,6 +9,7 @@ class ListController < ApplicationControllerbefore_action :set_category,only: [:category_default,+ :category_none_default,# filtered topics listsDiscourse.filters.map { |f| :"category_#{f}" },Discourse.filters.map { |f| :"category_none_#{f}" },@@ -30,6 +31,7 @@ class ListController < ApplicationControllerDiscourse.anonymous_filters.map { |f| "#{f}_feed" },# anonymous categorized filters:category_default,+ :category_none_default,Discourse.anonymous_filters.map { |f| :"category_#{f}" },Discourse.anonymous_filters.map { |f| :"category_none_#{f}" },# category feeds@@ -148,10 +150,11 @@ class ListController < ApplicationControllerdef category_defaultcanonical_url "#{Discourse.base_url_no_prefix}#{@category.url}"- view_method = @category.default_view- view_method = "latest" if %w[hot latest top].exclude?(view_method)+ public_send(category_default_view, category: @category.id)+ end- public_send(view_method, category: @category.id)+ def category_none_default+ public_send(category_default_view, category: @category.id, no_subcategories: true)enddef topics_by@@ -433,6 +436,16 @@ class ListController < ApplicationControllerroute_paramsend+ # `default_view` is free-form, so only ever dispatch to a filter this request+ # could have reached directly at /c/<slug>/<id>/l/<filter>.+ def category_default_view+ filters = Discourse.filters+ filters &= Discourse.anonymous_filters if current_user.nil?++ view = @category.default_view+ filters.include?(view&.to_sym) ? view : "latest"+ end+def set_categorycategory_slug_path_with_id = params.require(:category_slug_path_with_id)diff --git a/app/serializers/topic_list_serializer.rb b/app/serializers/topic_list_serializer.rbindex a034834e82e..2e6c17541de 100644--- a/app/serializers/topic_list_serializer.rb+++ b/app/serializers/topic_list_serializer.rb@@ -2,6 +2,7 @@class TopicListSerializer < ApplicationSerializerattributes :can_create_topic,+ :filter,:more_topics_url,:for_period,:per_page,diff --git a/config/routes.rb b/config/routes.rbindex 4f9a35c4683..f9fb02f3c1a 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -1406,7 +1406,7 @@ Discourse::Application.routes.draw doget "c/*category_slug_path_with_id.rss" => "list#category_feed", :format => :rssscope path: "c/*category_slug_path_with_id" do- get "/none" => "list#category_none_latest"+ get "/none" => "list#category_none_default", :as => "category_none_default"TopTopic.periods.each do |period|get "/none/l/top/#{period}", to: redirect("/none/l/top?period=#{period}", status: 301)diff --git a/plugins/discourse-topic-voting/spec/requests/category_default_view_spec.rb b/plugins/discourse-topic-voting/spec/requests/category_default_view_spec.rbnew file mode 100644index 00000000000..1ba95c62ecf--- /dev/null+++ b/plugins/discourse-topic-voting/spec/requests/category_default_view_spec.rb@@ -0,0 +1,45 @@+# frozen_string_literal: true++RSpec.describe "Category default view of votes" do+ fab!(:category)+ fab!(:most_voted) { Fabricate(:topic, category:, bumped_at: 2.days.ago) }+ fab!(:recently_bumped) { Fabricate(:topic, category:, bumped_at: 1.minute.ago) }++ before do+ SiteSetting.topic_voting_enabled = true+ DiscourseTopicVoting::CategorySetting.create!(category:)+ Category.reset_voting_cache+ category.update!(default_view: "votes")+ DiscourseTopicVoting::TopicVoteCount.create!(topic: most_voted, votes_count: 5)+ end++ def topic_ids_for(url)+ get url+ expect(response.status).to eq(200)+ response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] }+ end++ it "orders the category by votes" do+ expect(topic_ids_for("/c/#{category.slug}/#{category.id}.json")).to eq(+ [most_voted.id, recently_bumped.id],+ )+ end++ it "orders the category by votes when excluding subcategories" do+ expect(topic_ids_for("/c/#{category.slug}/#{category.id}/none.json")).to eq(+ [most_voted.id, recently_bumped.id],+ )+ end++ it "preloads the votes ordered list into the page" do+ get "/c/#{category.slug}/#{category.id}"+ expect(response.status).to eq(200)++ preloaded = Nokogiri.HTML5(response.body).at_css("#data-preloaded").text+ topic_list = JSON.parse(JSON.parse(preloaded)["topic_list"])["topic_list"]++ expect(topic_list["topics"].map { |topic| topic["id"] }).to eq(+ [most_voted.id, recently_bumped.id],+ )+ end+enddiff --git a/spec/requests/api/schemas/json/category_topics_response.json b/spec/requests/api/schemas/json/category_topics_response.jsonindex 3b38142d00e..f775ec29cda 100644--- a/spec/requests/api/schemas/json/category_topics_response.json+++ b/spec/requests/api/schemas/json/category_topics_response.json@@ -39,6 +39,9 @@"type": "object","additionalProperties": false,"properties": {+ "filter": {+ "type": "string"+ },"can_create_topic": {"type": "boolean"},diff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rbindex 8f1c42baff6..01bb0d60ba1 100644--- a/spec/requests/list_controller_spec.rb+++ b/spec/requests/list_controller_spec.rb@@ -1193,6 +1193,29 @@ RSpec.describe ListController dojson = response.parsed_bodyexpect(json["topic_list"]["for_period"]).to be_blankend++ def filter_for(default_view, path = "")+ category.update!(default_view: default_view)+ get "/c/#{category.slug}/#{category.id}#{path}.json"+ expect(response.status).to eq(200)+ response.parsed_body["topic_list"]["filter"]+ end++ it "falls back to latest when the default view is not a filter this request can reach" do+ expect(filter_for("destroy")).to eq("latest")+ expect(filter_for("categories")).to eq("latest")+ expect(filter_for("unread")).to eq("latest")+ end++ it "honours a logged in only default view for logged in users" do+ sign_in(user)++ expect(filter_for("unread")).to eq("unread")+ end++ it "honours the default view when excluding subcategories" do+ expect(filter_for("hot", "/none")).to eq("hot")+ endenddescribe "renders canonical tag" do