From 14983c5b8ed160ac6d0887f397982d0cf6597510 Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Tue, 17 Jan 2023 10:16:28 +1000 Subject: [PATCH] FIX: New hashtag support for narrative bot advanced narrative (#19875) The discobot advanced tutorial was failing when the new hashtags were enabled with enable_experimental_hashtag_autocomplete set to true, since the CSS selector is different. This commit fixes the issue and also changes the instructions if this is enabled since we no longer require the hashtag to not be at the start of the line. c.f. https://meta.discourse.org/t/it-is-impossible-to-complete-the-hashtag-section-of-the-discobot-advanced-tutorial/251494 --- .../config/locales/server.en.yml | 10 ++++++ .../advanced_user_narrative.rb | 31 +++++++++++++---- .../advanced_user_narrative_spec.rb | 34 +++++++++++++++++-- 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/plugins/discourse-narrative-bot/config/locales/server.en.yml b/plugins/discourse-narrative-bot/config/locales/server.en.yml index cf69370c2a3..d1114d78f43 100644 --- a/plugins/discourse-narrative-bot/config/locales/server.en.yml +++ b/plugins/discourse-narrative-bot/config/locales/server.en.yml @@ -401,9 +401,19 @@ en: Did you know that you can refer to categories and tags in your post? For example, have you seen the %{category} category? Type `#` in the middle of a sentence and select any category or tag. + instructions_experimental: |- + Did you know that you can refer to categories and tags in your post? For example, have you seen the %{category} category? + + Type `#` anywhere in a sentence and select any category or tag. not_found: |- Hmm, I don’t see a category in there anywhere. Note that `#` can't be the first character. Can you copy this in your next reply? + ```text + I can create a category link via # + ``` + not_found_experimental: |- + Hmm, I don’t see a category in there anywhere. Can you copy this in your next reply? + ```text I can create a category link via # ``` diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/advanced_user_narrative.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/advanced_user_narrative.rb index 1fea4f13d56..8b93776603b 100644 --- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/advanced_user_narrative.rb +++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/advanced_user_narrative.rb @@ -46,10 +46,20 @@ module DiscourseNarrativeBot slug = "#{parent_category.slug}#{CategoryHashtag::SEPARATOR}#{slug}" end - I18n.t( - "#{I18N_KEY}.category_hashtag.instructions", - i18n_post_args(category: "##{slug}"), - ) + # TODO (martin) When enable_experimental_hashtag_autocomplete is the only option + # update the instructions and remove instructions_experimental, as well as the + # not_found translation + if SiteSetting.enable_experimental_hashtag_autocomplete + I18n.t( + "#{I18N_KEY}.category_hashtag.instructions_experimental", + i18n_post_args(category: "##{slug}"), + ) + else + I18n.t( + "#{I18N_KEY}.category_hashtag.instructions", + i18n_post_args(category: "##{slug}"), + ) + end end, recover: { action: :reply_to_recover, @@ -288,7 +298,9 @@ module DiscourseNarrativeBot topic_id = @post.topic_id return unless valid_topic?(topic_id) - if Nokogiri::HTML5.fragment(@post.cooked).css(".hashtag").size > 0 + hashtag_css_class = + SiteSetting.enable_experimental_hashtag_autocomplete ? ".hashtag-cooked" : ".hashtag" + if Nokogiri::HTML5.fragment(@post.cooked).css(hashtag_css_class).size > 0 raw = <<~MD #{I18n.t("#{I18N_KEY}.category_hashtag.reply", i18n_post_args)} @@ -300,7 +312,14 @@ module DiscourseNarrativeBot else fake_delay unless @data[:attempted] - reply_to(@post, I18n.t("#{I18N_KEY}.category_hashtag.not_found", i18n_post_args)) + if SiteSetting.enable_experimental_hashtag_autocomplete + reply_to( + @post, + I18n.t("#{I18N_KEY}.category_hashtag.not_found_experimental", i18n_post_args), + ) + else + reply_to(@post, I18n.t("#{I18N_KEY}.category_hashtag.not_found", i18n_post_args)) + end end enqueue_timeout_job(@user) false diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb index 14561dceed8..529ad5c67a1 100644 --- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb +++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb @@ -377,6 +377,9 @@ RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative do context "when reply contains the skip trigger" do it "should create the right reply" do + # TODO (martin) Remove when enable_experimental_hashtag_autocomplete is default for all sites + SiteSetting.enable_experimental_hashtag_autocomplete = false + parent_category = Fabricate(:category, name: "a") _category = Fabricate(:category, parent_category: parent_category, name: "b") @@ -414,6 +417,9 @@ RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative do context "when user recovers a post in the right topic" do it "should create the right reply" do + # TODO (martin) Remove when enable_experimental_hashtag_autocomplete is default for all sites + SiteSetting.enable_experimental_hashtag_autocomplete = false + parent_category = Fabricate(:category, name: "a") _category = Fabricate(:category, parent_category: parent_category, name: "b") post @@ -442,6 +448,9 @@ RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative do topic_id: topic.id, track: described_class.to_s, ) + + # TODO (martin) Remove when enable_experimental_hashtag_autocomplete is default for all sites + SiteSetting.enable_experimental_hashtag_autocomplete = false end context "when post is not in the right topic" do @@ -494,9 +503,6 @@ RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative do end it "should create the right reply" do - # TODO (martin) Remove when enable_experimental_hashtag_autocomplete is default for all sites - SiteSetting.enable_experimental_hashtag_autocomplete = false - category = Fabricate(:category) post.update!(raw: "Check out this ##{category.slug}") @@ -513,6 +519,28 @@ RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative do :tutorial_change_topic_notification_level, ) end + + context "when enable_experimental_hashtag_autocomplete is true" do + before { SiteSetting.enable_experimental_hashtag_autocomplete = true } + + it "should create the right reply" do + category = Fabricate(:category) + + post.update!(raw: "Check out this ##{category.slug}") + narrative.input(:reply, user, post: post) + + expected_raw = <<~RAW + #{I18n.t("discourse_narrative_bot.advanced_user_narrative.category_hashtag.reply", base_uri: "")} + + #{I18n.t("discourse_narrative_bot.advanced_user_narrative.change_topic_notification_level.instructions", base_uri: "")} + RAW + + expect(Post.last.raw).to eq(expected_raw.chomp) + expect(narrative.get_data(user)[:state].to_sym).to eq( + :tutorial_change_topic_notification_level, + ) + end + end end context "with topic notification level tutorial" do