FIX: permalink redirects should work on tag paths

This commit is contained in:
Neil Lalonde 2016-10-04 12:01:42 -04:00
parent 08c4de388e
commit 600b23c0a4
3 changed files with 25 additions and 25 deletions

View File

@ -329,6 +329,25 @@ class ApplicationController < ActionController::Base
request.session_options[:skip] = true request.session_options[:skip] = true
end end
def permalink_redirect_or_not_found
url = request.fullpath
permalink = Permalink.find_by_url(url)
if permalink.present?
# permalink present, redirect to that URL
if permalink.external_url
redirect_to permalink.external_url, status: :moved_permanently
elsif permalink.target_url
redirect_to "#{Discourse::base_uri}#{permalink.target_url}", status: :moved_permanently
else
raise Discourse::NotFound
end
else
# redirect to 404
raise Discourse::NotFound
end
end
private private
def locale_from_header def locale_from_header

View File

@ -259,7 +259,7 @@ class ListController < ApplicationController
parent_category_id = nil parent_category_id = nil
if parent_slug_or_id.present? if parent_slug_or_id.present?
parent_category_id = Category.query_parent_category(parent_slug_or_id) parent_category_id = Category.query_parent_category(parent_slug_or_id)
redirect_or_not_found and return if parent_category_id.blank? && !id permalink_redirect_or_not_found and return if parent_category_id.blank? && !id
end end
@category = Category.query_category(slug_or_id, parent_category_id) @category = Category.query_category(slug_or_id, parent_category_id)
@ -270,7 +270,7 @@ class ListController < ApplicationController
(redirect_to category.url, status: 301) && return if category (redirect_to category.url, status: 301) && return if category
end end
redirect_or_not_found and return if !@category permalink_redirect_or_not_found and return if !@category
@description_meta = @category.description_text @description_meta = @category.description_text
raise Discourse::NotFound unless guardian.can_see?(@category) raise Discourse::NotFound unless guardian.can_see?(@category)
@ -349,23 +349,4 @@ class ListController < ApplicationController
periods periods
end end
def redirect_or_not_found
url = request.fullpath
permalink = Permalink.find_by_url(url)
if permalink.present?
# permalink present, redirect to that URL
if permalink.external_url
redirect_to permalink.external_url, status: :moved_permanently
elsif permalink.target_url
redirect_to "#{Discourse::base_uri}#{permalink.target_url}", status: :moved_permanently
else
raise Discourse::NotFound
end
else
# redirect to 404
raise Discourse::NotFound
end
end
end end

View File

@ -78,7 +78,7 @@ class TagsController < ::ApplicationController
canonical_url "#{Discourse.base_url_no_prefix}#{public_send(url_method(params.slice(:category, :parent_category)))}" canonical_url "#{Discourse.base_url_no_prefix}#{public_send(url_method(params.slice(:category, :parent_category)))}"
if @list.topics.size == 0 && params[:tag_id] != 'none' && !Tag.where(name: @tag_id).exists? if @list.topics.size == 0 && params[:tag_id] != 'none' && !Tag.where(name: @tag_id).exists?
raise Discourse::NotFound permalink_redirect_or_not_found
else else
respond_with_list(@list) respond_with_list(@list)
end end
@ -220,13 +220,13 @@ class TagsController < ::ApplicationController
parent_category_id = nil parent_category_id = nil
if parent_slug_or_id.present? if parent_slug_or_id.present?
parent_category_id = Category.query_parent_category(parent_slug_or_id) parent_category_id = Category.query_parent_category(parent_slug_or_id)
redirect_or_not_found and return if parent_category_id.blank? category_redirect_or_not_found and return if parent_category_id.blank?
end end
@filter_on_category = Category.query_category(slug_or_id, parent_category_id) @filter_on_category = Category.query_category(slug_or_id, parent_category_id)
end end
redirect_or_not_found and return if !@filter_on_category category_redirect_or_not_found and return if !@filter_on_category
guardian.ensure_can_see!(@filter_on_category) guardian.ensure_can_see!(@filter_on_category)
end end
@ -305,7 +305,7 @@ class TagsController < ::ApplicationController
options options
end end
def redirect_or_not_found def category_redirect_or_not_found
# automatic redirects for renamed categories # automatic redirects for renamed categories
url = params[:parent_category] ? "c/#{params[:parent_category]}/#{params[:category]}" : "c/#{params[:category]}" url = params[:parent_category] ? "c/#{params[:parent_category]}/#{params[:category]}" : "c/#{params[:category]}"
permalink = Permalink.find_by_url(url) permalink = Permalink.find_by_url(url)