From 193f04842ebb8e842932e5fb77449278f202d183 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Sat, 26 Sep 2015 22:53:25 +0530 Subject: [PATCH] FIX: permalinks should work for Discourse categories --- app/controllers/list_controller.rb | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rb index d894ee1f37c..bea88a69e62 100644 --- a/app/controllers/list_controller.rb +++ b/app/controllers/list_controller.rb @@ -228,11 +228,11 @@ class ListController < ApplicationController parent_category_id = nil if parent_slug_or_id.present? parent_category_id = Category.query_parent_category(parent_slug_or_id) - raise Discourse::NotFound if parent_category_id.blank? + redirect_or_not_found and return if parent_category_id.blank? end @category = Category.query_category(slug_or_id, parent_category_id) - raise Discourse::NotFound if !@category + redirect_or_not_found and return if !@category @description_meta = @category.description_text guardian.ensure_can_see!(@category) @@ -308,4 +308,23 @@ class ListController < ApplicationController periods 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