FIX: Wrong discobot tutorial started for certain locales.

If a locale has triggers that start with the same word, our regexp will
always end up matching the first trigger. For example,

`start tutorial` and `start tutorial advanced`

To support the change, we have to make the match on triggers more
restrictive. `@discobot quote here` will no longer work like `@discobot
quote`.
This commit is contained in:
Guo Xiang Tan 2019-08-08 10:47:39 +08:00
parent b574276e6e
commit 636b6c3a5a
2 changed files with 34 additions and 10 deletions

View File

@ -227,7 +227,7 @@ module DiscourseNarrativeBot
def match_trigger?(trigger) def match_trigger?(trigger)
discobot_username = self.discobot_user.username discobot_username = self.discobot_user.username
regexp = Regexp.new("<a class=\"mention\".*>@#{discobot_username}</a> #{trigger}", 'i') regexp = Regexp.new("<a class=\"mention\".*>@#{discobot_username}</a> #{trigger}</p>", 'i')
match = @post.cooked.match(regexp) match = @post.cooked.match(regexp)
if @is_pm_to_bot if @is_pm_to_bot

View File

@ -580,16 +580,14 @@ describe DiscourseNarrativeBot::TrackSelector do
stub_request(:get, "http://api.forismatic.com/api/1.0/?format=json&lang=en&method=getQuote"). stub_request(:get, "http://api.forismatic.com/api/1.0/?format=json&lang=en&method=getQuote").
to_return(status: 200, body: "{\"quoteText\":\"Be Like Water\",\"quoteAuthor\":\"Bruce Lee\"}") to_return(status: 200, body: "{\"quoteText\":\"Be Like Water\",\"quoteAuthor\":\"Bruce Lee\"}")
['@discobot quote', 'hello @discobot quote there'].each do |raw| post.update!(raw: "@discobot quote")
post.update!(raw: raw) described_class.new(:reply, user, post_id: post.id).select
described_class.new(:reply, user, post_id: post.id).select new_post = Post.last
new_post = Post.last
expect(new_post.raw).to eq( expect(new_post.raw).to eq(
I18n.t("discourse_narrative_bot.quote.results", I18n.t("discourse_narrative_bot.quote.results",
quote: "Be Like Water", author: "Bruce Lee" quote: "Be Like Water", author: "Bruce Lee"
)) ))
end
end end
describe 'when quote is requested incorrectly' do describe 'when quote is requested incorrectly' do
@ -661,6 +659,32 @@ describe DiscourseNarrativeBot::TrackSelector do
end.to_not change { Post.count } end.to_not change { Post.count }
end end
end end
describe "when new and advanced user triggers overlap" do
before do
@overrides = []
@overrides << TranslationOverride.upsert!(
I18n.locale, 'discourse_narrative_bot.new_user_narrative.reset_trigger', 'tutorial'
)
@overrides << TranslationOverride.upsert!(
I18n.locale, 'discourse_narrative_bot.advanced_user_narrative.reset_trigger', 'tutorial advanced'
)
end
after do
@overrides.each(&:destroy!)
end
it "should start the right track" do
post.update!(raw: "@discobot #{I18n.t('discourse_narrative_bot.track_selector.reset_trigger')} #{DiscourseNarrativeBot::AdvancedUserNarrative.reset_trigger}")
expect do
described_class.new(:reply, user, post_id: post.id).select
end.to change { Post.count }.by(2)
end
end
end end
end end
end end