diff --git a/app/assets/javascripts/discourse/app/initializers/welcome-topic-banner.js b/app/assets/javascripts/discourse/app/initializers/welcome-topic-banner.js new file mode 100644 index 00000000000..cbf9cfb8fdb --- /dev/null +++ b/app/assets/javascripts/discourse/app/initializers/welcome-topic-banner.js @@ -0,0 +1,15 @@ +export default { + name: "welcome-topic-banner", + after: "message-bus", + + initialize(container) { + const site = container.lookup("service:site"); + + if (site.show_welcome_topic_banner) { + const messageBus = container.lookup("service:message-bus"); + messageBus.subscribe("/site/welcome-topic-banner", function (disabled) { + site.set("show_welcome_topic_banner", disabled); + }); + } + }, +}; diff --git a/app/models/site.rb b/app/models/site.rb index 8a23bf1e044..b76a0fa2a0b 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -203,10 +203,24 @@ class Site MessageBus.publish(SITE_JSON_CHANNEL, '') end - def self.show_welcome_topic_banner?(guardian) - return false unless guardian.is_admin? - return false unless guardian.user.id == User.first_login_admin_id + def self.welcome_topic_banner_cache_key(user_id) + "show_welcome_topic_banner:#{user_id}" + end - Post.find_by("topic_id = :topic_id AND post_number = 1 AND version = 1 AND created_at > :created_at", topic_id: SiteSetting.welcome_topic_id, created_at: 1.month.ago).present? + def self.show_welcome_topic_banner?(guardian) + return false if !guardian.is_admin? + user_id = guardian.user.id + + show_welcome_topic_banner = Discourse.cache.read(welcome_topic_banner_cache_key(user_id)) + return show_welcome_topic_banner unless show_welcome_topic_banner.nil? + + show_welcome_topic_banner = if (user_id == User.first_login_admin_id) + Post.find_by("topic_id = :topic_id AND post_number = 1 AND version = 1 AND created_at > :created_at", topic_id: SiteSetting.welcome_topic_id, created_at: 1.month.ago).present? + else + false + end + + Discourse.cache.write(welcome_topic_banner_cache_key(user_id), show_welcome_topic_banner) + show_welcome_topic_banner end end diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb index dfa5904d78c..d38cfa3df6b 100644 --- a/lib/post_revisor.rb +++ b/lib/post_revisor.rb @@ -630,6 +630,7 @@ class PostRevisor update_topic_excerpt update_category_description + hide_welcome_topic_banner end def update_topic_excerpt @@ -651,6 +652,15 @@ class PostRevisor end end + def hide_welcome_topic_banner + return unless guardian.is_admin? + return unless @topic.id == SiteSetting.welcome_topic_id + return unless Discourse.cache.read(Site.welcome_topic_banner_cache_key(@editor.id)) + + Discourse.cache.write(Site.welcome_topic_banner_cache_key(@editor.id), false) + MessageBus.publish("/site/welcome-topic-banner", false) + end + def advance_draft_sequence @post.advance_draft_sequence end diff --git a/spec/lib/post_revisor_spec.rb b/spec/lib/post_revisor_spec.rb index f1d0918398c..87523993888 100644 --- a/spec/lib/post_revisor_spec.rb +++ b/spec/lib/post_revisor_spec.rb @@ -696,7 +696,7 @@ RSpec.describe PostRevisor do describe "topic excerpt" do it "topic excerpt is updated only if first post is revised" do revisor = PostRevisor.new(post) - first_post = topic.posts.by_post_number.first + first_post = topic.first_post expect { revisor.revise!(first_post.user, { raw: 'Edit the first post' }, revised_at: first_post.updated_at + 10.seconds) topic.reload @@ -709,6 +709,26 @@ RSpec.describe PostRevisor do end end + describe "welcome topic" do + before do + SiteSetting.welcome_topic_id = topic.id + end + + it "should publish welcome topic edit message" do + revisor = PostRevisor.new(post) + first_post = topic.first_post + UserAuthToken.generate!(user_id: admin.id) + Discourse.cache.write(Site.welcome_topic_banner_cache_key(admin.id), true) + + messages = MessageBus.track_publish("/site/welcome-topic-banner") do + revisor.revise!(admin, { raw: 'updated welcome topic body' }) + end + welcome_topic_banner_message = messages.find { |message| message.channel == "/site/welcome-topic-banner" } + expect(welcome_topic_banner_message).to be_present + expect(welcome_topic_banner_message.data).to eq(false) + end + end + it "doesn't strip starting whitespaces" do subject.revise!(post.user, raw: " <-- whitespaces --> ") post.reload diff --git a/spec/models/site_spec.rb b/spec/models/site_spec.rb index 0b264ec78e5..c6db2283781 100644 --- a/spec/models/site_spec.rb +++ b/spec/models/site_spec.rb @@ -192,10 +192,12 @@ RSpec.describe Site do end it "returns false when the user is not first admin who logs in" do + admin = Fabricate(:admin) first_post = Fabricate(:post, created_at: 25.days.ago) SiteSetting.welcome_topic_id = first_post.topic.id - expect(Site.show_welcome_topic_banner?(Guardian.new(Fabricate(:admin)))).to eq(false) + expect(Site.show_welcome_topic_banner?(Guardian.new(admin))).to eq(false) + expect(Discourse.cache.read(Site.welcome_topic_banner_cache_key(admin.id))).to eq(false) end it "returns true when welcome topic is less than month old" do @@ -206,6 +208,7 @@ RSpec.describe Site do SiteSetting.welcome_topic_id = first_post.topic.id expect(Site.show_welcome_topic_banner?(Guardian.new(admin))).to eq(true) + expect(Discourse.cache.read(Site.welcome_topic_banner_cache_key(admin.id))).to eq(true) end it "returns false when welcome topic is more than month old" do @@ -216,6 +219,7 @@ RSpec.describe Site do SiteSetting.welcome_topic_id = first_post.topic.id expect(Site.show_welcome_topic_banner?(Guardian.new(admin))).to eq(false) + expect(Discourse.cache.read(Site.welcome_topic_banner_cache_key(admin.id))).to eq(false) end it "returns false when welcome topic has been edited" do @@ -226,6 +230,7 @@ RSpec.describe Site do SiteSetting.welcome_topic_id = first_post.topic.id expect(Site.show_welcome_topic_banner?(Guardian.new(admin))).to eq(false) + expect(Discourse.cache.read(Site.welcome_topic_banner_cache_key(admin.id))).to eq(false) end end