FIX: Wizard tries harder to find existing Welcome Topic

FIX: Wizard tries harder to find existing Welcome Topic

The wizard searches for:

  • a topic that with the “is_welcome_topic” custom field
  • a topic with the correct slug for the current default locale
  • a topic with the correct slug for the English locale
  • the oldest globally pinned topic

It gives up if it didn’t find any of the above.

From 43cfdb1cb9be56e24ef239b4ec3a711d64099333 Mon Sep 17 00:00:00 2001
From: Gerhard Schlager <mail@gerhard-schlager.at>
Date: Wed, 5 Dec 2018 18:05:37 +0100
Subject: [PATCH] FIX: Wizard tries harder to find existing Welcome Topic

The wizard searches for:

* a topic that with the "is_welcome_topic" custom field
* a topic with the correct slug for the current default locale
* a topic with the correct slug for the English locale
* the oldest globally pinned topic

It gives up if it didn't find any of the above.

diff --git a/app/serializers/wizard_step_serializer.rb b/app/serializers/wizard_step_serializer.rb
index 458bee2..bb792a2 100644
--- a/app/serializers/wizard_step_serializer.rb
+++ b/app/serializers/wizard_step_serializer.rb
@@ -39,9 +39,8 @@ class WizardStepSerializer < ApplicationSerializer
   end
 
   def description
-    return translate("disabled") if object.disabled
-
-    translate("description", base_path: Discourse.base_path)
+    key = object.disabled ? "disabled" : "description"
+    translate(key, object.description_vars)
   end
 
   def include_description?
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index c8548b7..6dda02e 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -4049,10 +4049,10 @@ en:
 
       introduction:
         title: "Introduction"
-        disabled: "<p>We couldn’t find any topic with the title “Welcome to Discourse”.</p>
+        disabled: "<p>We couldn’t find any topic with the title “%{topic_title}”.</p>
           <ul>
           <li>If you have changed the title, edit that topic to modify your site’s introductory text.</li>
-          <li>If you have deleted this topic, create another topic with “Welcome to Discourse” as the title. The content of the first post is your site’s introductory text.</li>
+          <li>If you have deleted this topic, create another topic with “%{topic_title}” as the title. The content of the first post is your site’s introductory text.</li>
           </ul>"
 
         fields:
diff --git a/lib/introduction_updater.rb b/lib/introduction_updater.rb
index 002b52f..4f46ef1 100644
--- a/lib/introduction_updater.rb
+++ b/lib/introduction_updater.rb
@@ -22,25 +22,47 @@ class IntroductionUpdater
     end
   end
 
-protected
+  protected
 
   def summary_from_post(post)
     return post ? post.raw.split("\n").first : nil
   end
 
   def find_welcome_post
-    topic_id = TopicCustomField.where(name: "is_welcome_topic").where(value: "true").pluck(:topic_id)
-    unless topic_id.present?
-      topic_id = Topic.listable_topics.where(slug: 'welcome-to-discourse').pluck(:id)
+    topic_id = TopicCustomField
+      .where(name: "is_welcome_topic", value: "true")
+      .pluck(:topic_id)
+
+    if topic_id.blank?
+      title = I18n.t("discourse_welcome_topic.title")
+      topic_id = find_topic_id(title)
     end
 
-    welcome_topic = Topic.where(id: topic_id).first
-    return nil unless welcome_topic.present?
+    if topic_id.blank?
+      title = I18n.t("discourse_welcome_topic.title", locale: :en)
+      topic_id = find_topic_id(title)
+    end
+
+    if topic_id.blank?
+      topic_id = Topic.listable_topics
+        .where(pinned_globally: true)
+        .order(:created_at)
+        .limit(1)
+        .pluck(:id)
+    end
 
-    post = welcome_topic.posts.where(post_number: 1).first
-    return nil unless post.present?
+    welcome_topic = Topic.where(id: topic_id).first
+    return nil if welcome_topic.blank?
 
-    post
+    welcome_topic.first_post
   end
 
+  def find_topic_id(topic_title)
+    slug = Slug.for(topic_title, nil)
+    return nil if slug.blank?
+
+    Topic.listable_topics
+      .where(slug: slug)
+      .pluck(:id)
+  end
 end
diff --git a/lib/wizard/builder.rb b/lib/wizard/builder.rb
index ba6b615..92c6aa9 100644
--- a/lib/wizard/builder.rb
+++ b/lib/wizard/builder.rb
@@ -48,6 +48,7 @@ class Wizard
 
         if @wizard.completed_steps?('introduction') && !introduction.get_summary
           step.disabled = true
+          step.description_vars = { topic_title: I18n.t("discourse_welcome_topic.title") }
         else
           step.add_field(id: 'welcome', type: 'textarea', required: true, value: introduction.get_summary)
 
@@ -102,6 +103,7 @@ class Wizard
       end
 
       @wizard.append_step('corporate') do |step|
+        step.description_vars = { base_path: Discourse.base_path }
         step.add_field(id: 'company_name', type: 'text', value: SiteSetting.company_name)
         step.add_field(id: 'governing_law', type: 'text', value: SiteSetting.governing_law)
         step.add_field(id: 'city_for_disputes', type: 'text', value: SiteSetting.city_for_disputes)
@@ -263,6 +265,7 @@ class Wizard
 
       @wizard.append_step('finished') do |step|
         step.banner = "finished.png"
+        step.description_vars = { base_path: Discourse.base_path }
       end
       @wizard
     end
diff --git a/lib/wizard/step.rb b/lib/wizard/step.rb
index f059bd4..aea48f9 100644
--- a/lib/wizard/step.rb
+++ b/lib/wizard/step.rb
@@ -1,7 +1,7 @@
 class Wizard
   class Step
     attr_reader :id, :updater
-    attr_accessor :index, :fields, :next, :previous, :banner, :disabled
+    attr_accessor :index, :fields, :next, :previous, :banner, :disabled, :description_vars
 
     def initialize(id)
       @id = id
diff --git a/spec/lib/introduction_updater_spec.rb b/spec/lib/introduction_updater_spec.rb
new file mode 100644
index 0000000..025e116
--- /dev/null
+++ b/spec/lib/introduction_updater_spec.rb
@@ -0,0 +1,56 @@
+require 'rails_helper'
+require 'introduction_updater'
+
+describe IntroductionUpdater do
+  describe "#get_summary" do
+    subject { IntroductionUpdater.new(Fabricate(:admin)) }
+
+    let(:welcome_post_raw) { "lorem ipsum" }
+    let(:welcome_topic) do
+      topic = Fabricate(:topic)
+      Fabricate(:post, topic: topic, raw: welcome_post_raw, post_number: 1)
+      topic
+    end
+
+    it "finds the welcome topic by custom field" do
+      TopicCustomField.create(topic_id: welcome_topic.id, name: "is_welcome_topic", value: "true")
+      expect(subject.get_summary).to eq(welcome_post_raw)
+    end
+
+    context "without custom field" do
+      it "finds the welcome topic by slug using the default locale" do
+        I18n.locale = :de
+        welcome_topic.title = I18n.t("discourse_welcome_topic.title")
+        welcome_topic.save!
+
+        expect(subject.get_summary).to eq(welcome_post_raw)
+      end
+
+      it "finds the welcome topic by slug using the English locale" do
+        welcome_topic.title = I18n.t("discourse_welcome_topic.title", locale: :en)
+        welcome_topic.save!
+        I18n.locale = :de
+
+        expect(subject.get_summary).to eq(welcome_post_raw)
+      end
+
+      it "doesn't find the topic when slug_generation_method is set to 'none'" do
+        SiteSetting.slug_generation_method = :none
+        welcome_topic.title = I18n.t("discourse_welcome_topic.title")
+        welcome_topic.save!
+
+        expect(subject.get_summary).to be_blank
+      end
+
+      it "finds the oldest globally pinned topic" do
+        welcome_topic.update_columns(pinned_at: Time.zone.now, pinned_globally: true)
+
+        expect(subject.get_summary).to eq(welcome_post_raw)
+      end
+
+      it "doesn't find the topic when there is no globally pinned topic or a topic with the correct slug" do
+        expect(subject.get_summary).to be_blank
+      end
+    end
+  end
+end

GitHub

1 Like