FIX: Store special characters in permalink URL as encoded characters

see https://meta.discourse.org/t/permalink-not-working-with-cyrilics-symbols/301130
This commit is contained in:
Gerhard Schlager
2024-05-27 22:33:30 +02:00
committed by Gerhard Schlager
parent 9061282515
commit 602ef2c819
2 changed files with 43 additions and 11 deletions

View File

@ -7,7 +7,7 @@ class Permalink < ActiveRecord::Base
belongs_to :tag
belongs_to :user
before_validation :normalize_url
before_validation :normalize_url, :encode_url
validates :url, uniqueness: true
@ -66,10 +66,6 @@ class Permalink < ActiveRecord::Base
find_by(url: normalize_url(url))
end
def normalize_url
self.url = Permalink.normalize_url(url) if url
end
def target_url
return relative_external_url if external_url
return post.relative_url if post
@ -80,10 +76,6 @@ class Permalink < ActiveRecord::Base
nil
end
def relative_external_url
external_url.match?(%r{\A/[^/]}) ? "#{Discourse.base_path}#{external_url}" : external_url
end
def self.filter_by(url = nil)
permalinks =
Permalink.includes(:topic, :post, :category, :tag, :user).order("permalinks.created_at desc")
@ -92,6 +84,20 @@ class Permalink < ActiveRecord::Base
permalinks.limit!(100)
permalinks.to_a
end
private
def normalize_url
self.url = Permalink.normalize_url(url) if url
end
def encode_url
self.url = UrlHelper.encode(url) if url
end
def relative_external_url
external_url.match?(%r{\A/[^/]}) ? "#{Discourse.base_path}#{external_url}" : external_url
end
end
# == Schema Information