mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 07:53:49 +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?)
|
(!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
|
validate if: :featured_link do
|
||||||
errors.add(:featured_link, :invalid_category) unless !featured_link_changed? ||
|
errors.add(:featured_link, :invalid_category) unless !featured_link_changed? ||
|
||||||
Guardian.new.can_edit_featured_link?(category_id)
|
Guardian.new.can_edit_featured_link?(category_id)
|
||||||
@ -1265,14 +1265,7 @@ SQL
|
|||||||
end
|
end
|
||||||
|
|
||||||
def featured_link_root_domain
|
def featured_link_root_domain
|
||||||
url = URI.extract(self.featured_link).first
|
MiniSuffix.domain(URI.parse(self.featured_link).hostname)
|
||||||
|
|
||||||
begin
|
|
||||||
MiniSuffix.domain(URI.parse(url).hostname)
|
|
||||||
rescue URI::InvalidURIError => e
|
|
||||||
Rails.logger.warn("#{e.message}: #{e.backtrace.join("\n")}")
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -11,6 +11,22 @@ describe Topic do
|
|||||||
context 'validations' do
|
context 'validations' do
|
||||||
let(:topic) { Fabricate.build(:topic) }
|
let(:topic) { Fabricate.build(:topic) }
|
||||||
|
|
||||||
|
context "#featured_link" do
|
||||||
|
describe 'when featured_link contains more than a URL' do
|
||||||
|
it 'should not be valid' do
|
||||||
|
topic.featured_link = 'http://meta.discourse.org TEST'
|
||||||
|
expect(topic).to_not be_valid
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'when featured_link is a valid URL' do
|
||||||
|
it 'should be valid' do
|
||||||
|
topic.featured_link = 'http://meta.discourse.org'
|
||||||
|
expect(topic).to be_valid
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "#title" do
|
context "#title" do
|
||||||
it { is_expected.to validate_presence_of :title }
|
it { is_expected.to validate_presence_of :title }
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user