A platform for community discussion. Free, open, simple.
FIX: Handle blank-slug topics in full app embed mode (#38668)
Rafael dos Santos Silva70ddac53ba3c
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rbindex f57e1495134..c9f1ac06a28 100644--- a/app/controllers/topics_controller.rb+++ b/app/controllers/topics_controller.rb@@ -167,7 +167,8 @@ class TopicsController < ApplicationControllerdiscourse_expires_in 1.minute- if slugs_do_not_match || (!request.format.json? && params[:slug].nil?)+ if slugs_do_not_match ||+ (!request.format.json? && params[:slug].nil? && @topic_view.topic.slug.present?)redirect_to_correct_topic(@topic_view.topic, opts[:post_number])returnenddiff --git a/app/models/topic.rb b/app/models/topic.rbindex 8b1b8f19703..36e6ddf6551 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -1503,7 +1503,9 @@ class Topic < ActiveRecord::Baseenddef self.url(id, slug, post_number = nil)- url = +"#{Discourse.base_url}/t/#{slug}/#{id}"+ url = +"#{Discourse.base_url}/t/"+ url << "#{slug}/" if slug.present?+ url << id.to_surl << "/#{post_number}" if post_number.to_i > 1urlenddiff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rbindex 7d1aa6bcde8..1270133ab31 100644--- a/app/models/topic_embed.rb+++ b/app/models/topic_embed.rb@@ -73,7 +73,7 @@ class TopicEmbed < ActiveRecord::Baseendcreate_args = {- title: title,+ title: title.presence || url,raw: absolutize_urls(url, contents),skip_validations: true,cook_method: cook_method,diff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rbindex 999b2aca9bd..ed194f5903c 100644--- a/spec/models/topic_embed_spec.rb+++ b/spec/models/topic_embed_spec.rb@@ -165,6 +165,21 @@ RSpec.describe TopicEmbed doexpect(post.cooked).to match(/#{cased_url}/)end+ it "falls back to the url when the title is blank" do+ blank_title_url = "http://eviltrout.com/blank-title"+ imported_post = TopicEmbed.import(user, blank_title_url, "", contents)++ expect(imported_post.topic.title).to eq(blank_title_url)+ end++ it "preserves an existing title when a later import has a blank title" do+ imported_post = TopicEmbed.import(user, url, title, contents)++ TopicEmbed.import(user, url, "", "<p>updated content</p>")++ expect(imported_post.topic.reload.title).to eq(title)+ end+shared_examples "topic is unlisted" doit "unlists the topic until someone replies" doJobs.run_immediately!diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rbindex cdd74d5d045..b6519fc3a06 100644--- a/spec/models/topic_spec.rb+++ b/spec/models/topic_spec.rb@@ -342,6 +342,17 @@ RSpec.describe Topic doendend+ describe "url" do+ fab!(:topic)++ it "omits blank slugs" do+ topic.update_columns(title: "", slug: nil)+ topic.reload++ expect(topic.url).to eq("#{Discourse.base_url}/t/#{topic.id}")+ end+ end+describe "updating a title to be shorter" dolet!(:topic) { Fabricate(:topic) }diff --git a/spec/requests/embed_controller_spec.rb b/spec/requests/embed_controller_spec.rbindex 66bbd5bc999..195f89b8971 100644--- a/spec/requests/embed_controller_spec.rb+++ b/spec/requests/embed_controller_spec.rb@@ -219,6 +219,23 @@ RSpec.describe EmbedController doexpect(response).to redirect_to("#{topic.url}?embed_mode=true")end+ it "redirects blank-slug topics to a slugless URL" do+ topic_embed = Fabricate(:topic_embed, embed_url: embed_url)+ topic_embed.topic.update_columns(title: "", slug: nil)+ topic_embed.topic.reload++ get "/embed/comments",+ params: {+ embed_url: embed_url,+ full_app: "true",+ },+ headers: {+ "REFERER" => embed_url,+ }++ expect(response).to redirect_to("#{topic_embed.topic.url}?embed_mode=true")+ end+it "does not redirect when embed_full_app is disabled" doSiteSetting.embed_full_app = falsetopic_embed = Fabricate(:topic_embed, embed_url: embed_url)diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 296067ae376..039a356737d 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -2831,6 +2831,15 @@ RSpec.describe TopicsController doexpect(response.status).to eq(200)end+ it "shows a blank-slug topic without redirecting" do+ topic.update_columns(title: "", slug: nil)+ topic.reload++ get "/t/#{topic.id}"++ expect(response.status).to eq(200)+ end+it "return 404 for an invalid page" doget "/t/#{topic.slug}/#{topic.id}.json", params: { page: 2 }expect(response.status).to eq(404)