FIX: do not create duplicate topics

https://meta.discourse.org/t/duplicate-http-https-topics-are-randomly-created/77190
This commit is contained in:
Arpit Jalan 2018-01-04 23:43:17 +05:30
parent 6cab53abd4
commit 9030d3ef63
3 changed files with 18 additions and 3 deletions

View File

@ -182,8 +182,8 @@ class TopicEmbed < ActiveRecord::Base
end end
def self.topic_id_for_embed(embed_url) def self.topic_id_for_embed(embed_url)
embed_url = normalize_url(embed_url) embed_url = normalize_url(embed_url).sub(/^https?\:\/\//, '')
TopicEmbed.where("lower(embed_url) = ?", embed_url).pluck(:topic_id).first TopicEmbed.where("embed_url ~* '^https?://#{embed_url}$'").pluck(:topic_id).first
end end
def self.first_paragraph_from(html) def self.first_paragraph_from(html)

View File

@ -60,6 +60,20 @@ describe TopicEmbed do
end end
context '.topic_id_for_embed' do
it "returns correct topic id irrespective of url protocol" do
topic_embed = Fabricate(:topic_embed, embed_url: "http://example.com/post/248")
expect(TopicEmbed.topic_id_for_embed('http://exAMPle.com/post/248')).to eq(topic_embed.topic_id)
expect(TopicEmbed.topic_id_for_embed('https://example.com/post/248/')).to eq(topic_embed.topic_id)
expect(TopicEmbed.topic_id_for_embed('http://example.com/post/248/2')).to eq(nil)
expect(TopicEmbed.topic_id_for_embed('http://examples.com/post/248')).to eq(nil)
expect(TopicEmbed.topic_id_for_embed('http://example.com/post/24')).to eq(nil)
expect(TopicEmbed.topic_id_for_embed('http://example.com/post')).to eq(nil)
end
end
describe '.find_remote' do describe '.find_remote' do
context ".title_scrub" do context ".title_scrub" do

View File

@ -4,6 +4,7 @@ describe EmbedController do
let(:host) { "eviltrout.com" } let(:host) { "eviltrout.com" }
let(:embed_url) { "http://eviltrout.com/2013/02/10/why-discourse-uses-emberjs.html" } let(:embed_url) { "http://eviltrout.com/2013/02/10/why-discourse-uses-emberjs.html" }
let(:embed_url_secure) { "https://eviltrout.com/2013/02/10/why-discourse-uses-emberjs.html" }
let(:discourse_username) { "eviltrout" } let(:discourse_username) { "eviltrout" }
it "is 404 without an embed_url" do it "is 404 without an embed_url" do
@ -95,7 +96,7 @@ describe EmbedController do
it "displays the right view" do it "displays the right view" do
topic_embed = Fabricate(:topic_embed, embed_url: embed_url) topic_embed = Fabricate(:topic_embed, embed_url: embed_url)
get '/embed/comments', params: { embed_url: embed_url }, headers: headers get '/embed/comments', params: { embed_url: embed_url_secure }, headers: headers
expect(response.body).to match(I18n.t('embed.start_discussion')) expect(response.body).to match(I18n.t('embed.start_discussion'))
end end