mirror of
https://github.com/discourse/discourse.git
synced 2025-06-04 22:34:43 +08:00
FIX: Topic#featured_link
may contain more than a URL.
This commit is contained in:
15
app/jobs/onceoff/fix_featured_link_for_topics.rb
Normal file
15
app/jobs/onceoff/fix_featured_link_for_topics.rb
Normal file
@ -0,0 +1,15 @@
|
||||
module Jobs
|
||||
class FixFeaturedLinkForTopics < Jobs::Onceoff
|
||||
def execute_onceoff(args)
|
||||
Topic.where("featured_link IS NOT NULL").find_each do |topic|
|
||||
featured_link = topic.featured_link
|
||||
|
||||
begin
|
||||
URI.parse(featured_link)
|
||||
rescue URI::InvalidURIError
|
||||
topic.update!(featured_link: URI.extract(featured_link).first)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -88,7 +88,7 @@ class Topic < ActiveRecord::Base
|
||||
(!t.user_id || !t.user.staff?)
|
||||
}
|
||||
|
||||
validates :featured_link, allow_nil: true, format: URI::regexp(%w(http https))
|
||||
validates :featured_link, allow_nil: true, url: true
|
||||
validate if: :featured_link do
|
||||
errors.add(:featured_link, :invalid_category) unless !featured_link_changed? ||
|
||||
Guardian.new.can_edit_featured_link?(category_id)
|
||||
@ -1265,14 +1265,7 @@ SQL
|
||||
end
|
||||
|
||||
def featured_link_root_domain
|
||||
url = URI.extract(self.featured_link).first
|
||||
|
||||
begin
|
||||
MiniSuffix.domain(URI.parse(url).hostname)
|
||||
rescue URI::InvalidURIError => e
|
||||
Rails.logger.warn("#{e.message}: #{e.backtrace.join("\n")}")
|
||||
nil
|
||||
end
|
||||
MiniSuffix.domain(URI.parse(self.featured_link).hostname)
|
||||
end
|
||||
|
||||
private
|
||||
|
Reference in New Issue
Block a user