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
This commit is contained in:
Martin Brennan 2023-01-17 10:16:28 +10:00 committed by GitHub
parent d4e262d5dc
commit 14983c5b8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 9 deletions

View File

@ -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 dont 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 dont see a category in there anywhere. Can you copy this in your next reply?
```text
I can create a category link via #
```

View File

@ -46,10 +46,20 @@ module DiscourseNarrativeBot
slug = "#{parent_category.slug}#{CategoryHashtag::SEPARATOR}#{slug}"
end
# 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,8 +312,15 @@ module DiscourseNarrativeBot
else
fake_delay
unless @data[:attempted]
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
end

View File

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