mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +08:00
FIX: PG::UniqueViolation
when trying to use the same embed code
Previously providing an embed code already in use would result in a logged server error. After this commit the error is gracefully bubbled up from the `PostCreator`
This commit is contained in:
parent
08e62347e1
commit
fb8ba5e137
|
@ -4,6 +4,7 @@ class TopicEmbed < ActiveRecord::Base
|
||||||
belongs_to :topic
|
belongs_to :topic
|
||||||
belongs_to :post
|
belongs_to :post
|
||||||
validates_presence_of :embed_url
|
validates_presence_of :embed_url
|
||||||
|
validates_uniqueness_of :embed_url
|
||||||
|
|
||||||
def self.normalize_url(url)
|
def self.normalize_url(url)
|
||||||
url.downcase.sub(/\/$/, '').sub(/\-+/, '-').strip
|
url.downcase.sub(/\/$/, '').sub(/\-+/, '-').strip
|
||||||
|
|
|
@ -193,7 +193,8 @@ class PostCreator
|
||||||
# discourse post.
|
# discourse post.
|
||||||
def create_embedded_topic
|
def create_embedded_topic
|
||||||
return unless @opts[:embed_url].present?
|
return unless @opts[:embed_url].present?
|
||||||
TopicEmbed.create!(topic_id: @post.topic_id, post_id: @post.id, embed_url: @opts[:embed_url])
|
embed = TopicEmbed.new(topic_id: @post.topic_id, post_id: @post.id, embed_url: @opts[:embed_url])
|
||||||
|
rollback_from_errors!(embed) unless embed.save
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_spam
|
def handle_spam
|
||||||
|
|
|
@ -559,7 +559,17 @@ describe PostCreator do
|
||||||
title: 'Reviews of Science Ovens',
|
title: 'Reviews of Science Ovens',
|
||||||
raw: 'Did you know that you can use microwaves to cook your dinner? Science!')
|
raw: 'Did you know that you can use microwaves to cook your dinner? Science!')
|
||||||
creator.create
|
creator.create
|
||||||
|
expect(creator.errors).to be_blank
|
||||||
expect(TopicEmbed.where(embed_url: embed_url).exists?).to eq(true)
|
expect(TopicEmbed.where(embed_url: embed_url).exists?).to eq(true)
|
||||||
|
|
||||||
|
# If we try to create another topic with the embed url, should fail
|
||||||
|
creator = PostCreator.new(user,
|
||||||
|
embed_url: embed_url,
|
||||||
|
title: 'More Reviews of Science Ovens',
|
||||||
|
raw: 'As if anyone ever wanted to learn more about them!')
|
||||||
|
result = creator.create
|
||||||
|
expect(result).to be_present
|
||||||
|
expect(creator.errors).to be_present
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user