FIX: hide welcome topic banner as soon as the welcome topic is edited (#18125)

* FIX: hide welcome topic banner as soon as the welcome topic is edited

This commit adds a message bus listener on client to hide the welcome
topic banner as soon as the welcome topic is edited.

* update test

* only subscribe when show_welcome_topic_banner is true

* Do not lookup for messageBus service if it's not required

* Remove unneeded code

* Cache result for Site.show_welcome_topic_banner

* Update tests per latest changes

* Changes per PR review
This commit is contained in:
Arpit Jalan 2022-09-01 09:44:21 +05:30 committed by GitHub
parent 8fa9f0cf92
commit 3aaf4dcfd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 70 additions and 6 deletions

View File

@ -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);
});
}
},
};

View File

@ -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
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

View File

@ -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

View File

@ -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

View File

@ -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