From fa2c06da936e7529a2491694212b8e059569a204 Mon Sep 17 00:00:00 2001 From: Daniel Waterworth Date: Sun, 3 Nov 2019 16:32:31 +0000 Subject: [PATCH] DEV: Change tag routing for categories on the server-side These are the changes to tags routing that correspond to the category routing changes of d84c34ad. The new scheme is: /tags/c/*slug_path/:id/none/:tag_id/ENDPOINT /tags/c/*slug_path/:id/none/:tag_id /tags/c/*slug_path/:id/:tag_id/ENDPOINT /tags/c/*slug_path/:id/:tag_id --- app/controllers/tags_controller.rb | 69 ++++++++++++++++++++++-------- config/routes.rb | 20 ++++++--- 2 files changed, 66 insertions(+), 23 deletions(-) diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 9e2cfde2026..8aabc8f3db3 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -293,29 +293,52 @@ class TagsController < ::ApplicationController end def set_category_from_params - slug_or_id = params[:category] - return true if slug_or_id.nil? + if request.path_parameters.include?(:category_slug_path_with_id) + parts = params[:category_slug_path_with_id].split('/') - parent_slug_or_id = params[:parent_category] + if !parts.empty? && parts.last =~ /\A\d+\Z/ + id = parts.pop.to_i + end + slug_path = parts unless parts.empty? - parent_category_id = nil - if parent_slug_or_id.present? - parent_category_id = Category.query_parent_category(parent_slug_or_id) - category_redirect_or_not_found && (return) if parent_category_id.blank? + if id.present? + @filter_on_category = Category.find_by_id(id) + elsif slug_path.present? + if (1..2).include?(slug_path.size) + @filter_on_category = Category.find_by_slug(*slug_path.reverse) + end + + # Legacy paths + if @filter_on_category.nil? && parts.last =~ /\A\d+-/ + @filter_on_category = Category.find_by_id(parts.last.to_i) + end + end + else + slug_or_id = params[:category] + return true if slug_or_id.nil? + + @filter_on_category = Category.query_category(slug_or_id, nil) end - @filter_on_category = Category.query_category(slug_or_id, parent_category_id) - category_redirect_or_not_found && (return) if !@filter_on_category guardian.ensure_can_see!(@filter_on_category) end - # TODO: this is duplication of ListController def page_params route_params = { format: 'json' } - route_params[:category] = @filter_on_category.slug_for_url if @filter_on_category - route_params[:parent_category] = @filter_on_category.parent_category.slug_for_url if @filter_on_category && @filter_on_category.parent_category + + if @filter_on_category + if request.path_parameters.include?(:category_slug_path_with_id) + slug_path = @filter_on_category.slug_path + + route_params[:category_slug_path_with_id] = + (slug_path + [@filter_on_category.id.to_s]).join("/") + else + route_params[:category] = @filter_on_category.slug_for_url + end + end + route_params end @@ -345,15 +368,27 @@ class TagsController < ::ApplicationController def construct_url_with(action, opts) method = url_method(opts) - begin - url = if action == :prev - public_send(method, opts.merge(prev_page_params)) - else # :next - public_send(method, opts.merge(next_page_params)) + page_params = + case action + when :prev + prev_page_params + when :next + next_page_params + else + raise "unreachable" end + + if page_params.include?(:category_slug_path_with_id) + opts = opts.dup + opts.delete(:category) + end + + begin + url = public_send(method, opts.merge(page_params)) rescue ActionController::UrlGenerationError raise Discourse::NotFound end + url.sub('.json?', '?') end diff --git a/config/routes.rb b/config/routes.rb index bc66be9ecfa..81cc8d095ee 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -836,11 +836,22 @@ Discourse::Application.routes.draw do get '/unused' => 'tags#list_unused' delete '/unused' => 'tags#destroy_unused' constraints(tag_id: /[^\/]+?/, format: /json|rss/) do + scope path: '/c/*category_slug_path_with_id' do + Discourse.filters.each do |filter| + get "/none/:tag_id/l/#{filter}" => "tags#show_#{filter}", as: "tag_category_none_show_#{filter}", defaults: { no_subcategories: true } + end + + get '/none/:tag_id' => 'tags#show', as: 'tag_category_none_show', defaults: { no_subcategories: true } + + Discourse.filters.each do |filter| + get "/:tag_id/l/#{filter}" => "tags#show_#{filter}", as: "tag_category_show_#{filter}" + end + + get '/:tag_id' => 'tags#show', as: 'tag_category_show' + end + get '/:tag_id.rss' => 'tags#tag_feed' get '/:tag_id' => 'tags#show', as: 'tag_show' - get '/c/:category/:tag_id' => 'tags#show', as: 'tag_category_show' - get '/c/:category/none/:tag_id' => 'tags#show', as: 'tag_category_none_show', defaults: { no_subcategories: true } - get '/c/:parent_category/:category/:tag_id' => 'tags#show', as: 'tag_parent_category_category_show' get '/intersection/:tag_id/*additional_tag_ids' => 'tags#show', as: 'tag_intersection' get '/:tag_id/notifications' => 'tags#notifications' put '/:tag_id/notifications' => 'tags#update_notifications' @@ -849,9 +860,6 @@ Discourse::Application.routes.draw do Discourse.filters.each do |filter| get "/:tag_id/l/#{filter}" => "tags#show_#{filter}", as: "tag_show_#{filter}" - get "/c/:category/:tag_id/l/#{filter}" => "tags#show_#{filter}", as: "tag_category_show_#{filter}" - get "/c/:category/none/:tag_id/l/#{filter}" => "tags#show_#{filter}", as: "tag_category_none_show_#{filter}", defaults: { no_subcategories: true } - get "/c/:parent_category/:category/:tag_id/l/#{filter}" => "tags#show_#{filter}", as: "tag_parent_category_category_show_#{filter}" end end end