diff --git a/app/models/site.rb b/app/models/site.rb index 426299414fe..e9594d6e250 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -207,6 +207,15 @@ class Site "show_welcome_topic_banner:#{user_id}" end + def self.welcome_topic_exists_and_is_not_edited? + Post.joins(:topic) + .where( + "topics.id = :topic_id AND topics.deleted_at IS NULL AND posts.post_number = 1 AND posts.version = 1 AND posts.created_at > :created_at", + topic_id: SiteSetting.welcome_topic_id, + created_at: 1.month.ago + ).exists? + end + def self.show_welcome_topic_banner?(guardian) return false if !guardian.is_admin? user_id = guardian.user.id @@ -215,12 +224,7 @@ class Site return show_welcome_topic_banner unless show_welcome_topic_banner.nil? show_welcome_topic_banner = if (user_id == User.first_login_admin_id) - Post.joins(:topic) - .find_by( - "topics.id = :topic_id AND topics.deleted_at IS NULL AND posts.post_number = 1 AND posts.version = 1 AND posts.created_at > :created_at", - topic_id: SiteSetting.welcome_topic_id, - created_at: 1.month.ago - ).present? + welcome_topic_exists_and_is_not_edited? else false end diff --git a/lib/topic_query_params.rb b/lib/topic_query_params.rb index 0676334bf6b..7081513c3f1 100644 --- a/lib/topic_query_params.rb +++ b/lib/topic_query_params.rb @@ -18,6 +18,19 @@ module TopicQueryParams options[:topic_ids] = param_to_integer_list(:topic_ids) options[:no_subcategories] = options[:no_subcategories] == 'true' if options[:no_subcategories].present? + if hide_welcome_topic? + options[:except_topic_ids] ||= [] + options[:except_topic_ids] << SiteSetting.welcome_topic_id + end + options end + + private + + def hide_welcome_topic? + return false if !SiteSetting.bootstrap_mode_enabled + return false if @guardian.is_admin? + Site.welcome_topic_exists_and_is_not_edited? + end end diff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rb index f105b1e8b08..46a80983c1d 100644 --- a/spec/requests/list_controller_spec.rb +++ b/spec/requests/list_controller_spec.rb @@ -961,4 +961,56 @@ RSpec.describe ListController do expect(response.body).to have_tag "body", with: { class: "category-myparentslug-mychildslug" } end end + + describe "welcome topic" do + fab!(:welcome_topic) { Fabricate(:topic) } + fab!(:post) { Fabricate(:post, topic: welcome_topic) } + + before do + SiteSetting.welcome_topic_id = welcome_topic.id + SiteSetting.editing_grace_period = 1.minute.to_i + SiteSetting.bootstrap_mode_enabled = true + end + + it "is hidden for non-admins" do + + get "/latest.json" + expect(response.status).to eq(200) + parsed = response.parsed_body + expect(parsed["topic_list"]["topics"].length).to eq(1) + expect(parsed["topic_list"]["topics"].first["id"]).not_to eq(welcome_topic.id) + end + + it "is shown to non-admins when there is an edit" do + post.revise(post.user, { raw: "#{post.raw}2" }, revised_at: post.updated_at + 2.minutes) + post.reload + expect(post.version).to eq(2) + + get "/latest.json" + expect(response.status).to eq(200) + parsed = response.parsed_body + expect(parsed["topic_list"]["topics"].length).to eq(2) + expect(parsed["topic_list"]["topics"].first["id"]).to eq(welcome_topic.id) + end + + it "is shown to admins" do + sign_in(admin) + + get "/latest.json" + expect(response.status).to eq(200) + parsed = response.parsed_body + expect(parsed["topic_list"]["topics"].length).to eq(2) + expect(parsed["topic_list"]["topics"].first["id"]).to eq(welcome_topic.id) + end + + it "is shown to users when bootstrap mode is disabled" do + SiteSetting.bootstrap_mode_enabled = false + + get "/latest.json" + expect(response.status).to eq(200) + parsed = response.parsed_body + expect(parsed["topic_list"]["topics"].length).to eq(2) + expect(parsed["topic_list"]["topics"].first["id"]).to eq(welcome_topic.id) + end + end end