A platform for community discussion. Free, open, simple.
FIX: New hashtag support for narrative bot advanced narrative (#19875)
Martin Brennan14983c5b8ed1
diff --git a/plugins/discourse-narrative-bot/config/locales/server.en.yml b/plugins/discourse-narrative-bot/config/locales/server.en.ymlindex 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?+```textI 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.rbindex 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 DiscourseNarrativeBotslug = "#{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}"),+ )+ endend,recover: {action: :reply_to_recover,@@ -288,7 +298,9 @@ module DiscourseNarrativeBottopic_id = @post.topic_idreturn 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 > 0raw = <<~MD#{I18n.t("#{I18N_KEY}.category_hashtag.reply", i18n_post_args)}@@ -300,7 +312,14 @@ module DiscourseNarrativeBotelsefake_delayunless @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))+ endendenqueue_timeout_job(@user)falsediff --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.rbindex 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 docontext "when reply contains the skip trigger" doit "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 docontext "when user recovers a post in the right topic" doit "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 dotopic_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 = falseendcontext "when post is not in the right topic" do@@ -494,9 +503,6 @@ RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative doendit "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+ endendcontext "with topic notification level tutorial" do