FIX: match discobot triggers on cooked version

In French, the help trigger has a raw content of "afficher l'aider" which is then cooked into "afficher l’aide" (note the different quote character).
Since we were checking the raw content of the trigger against the cooked version of the post, this trigger never worked in French.

This changes so that we cook the trigger before checking in against the cooked version of the post.

DEV: new 'discobot_username' method that is used everywhere instead of 'discobot_user.username' / 'discobot_user.username_lower'
This commit is contained in:
Régis Hanol 2020-06-25 13:29:55 +02:00 committed by Robin Ward
parent 17182edab2
commit d156b7749d
9 changed files with 47 additions and 46 deletions

View File

@ -7,7 +7,7 @@ module Jobs
return if user.nil?
raw = I18n.t("discourse_narrative_bot.tl2_promotion_message.text_body_template",
discobot_username: ::DiscourseNarrativeBot::Base.new.discobot_user.username,
discobot_username: ::DiscourseNarrativeBot::Base.new.discobot_username,
reset_trigger: "#{::DiscourseNarrativeBot::TrackSelector.reset_trigger} #{::DiscourseNarrativeBot::AdvancedUserNarrative.reset_trigger}")
PostCreator.create!(

View File

@ -8,6 +8,10 @@ module DiscourseNarrativeBot
@discobot ||= User.find(-2)
end
def discobot_username
self.discobot_user.username_lower
end
private
def reply_to(post, raw, opts = {}, post_alert_options = {})
@ -73,7 +77,7 @@ module DiscourseNarrativeBot
valid = false
doc.css(".mention").each do |mention|
if User.normalize_username(mention.text) == "@#{self.discobot_user.username_lower}"
if User.normalize_username(mention.text) == "@#{self.discobot_username}"
valid = true
break
end

View File

@ -123,13 +123,13 @@ module DiscourseNarrativeBot
fake_delay
post = PostCreator.create!(@user, raw: I18n.t(
"#{I18N_KEY}.edit.bot_created_post_raw",
i18n_post_args(discobot_username: self.discobot_user.username)
),
topic_id: data[:topic_id],
skip_bot: true,
skip_validations: true)
post = PostCreator.create!(
@user,
raw: I18n.t("#{I18N_KEY}.edit.bot_created_post_raw", i18n_post_args(discobot_username: self.discobot_username)),
topic_id: data[:topic_id],
skip_bot: true,
skip_validations: true
)
set_state_data(:post_id, post.id)
post
@ -138,11 +138,9 @@ module DiscourseNarrativeBot
def init_tutorial_recover
data = get_data(@user)
post = PostCreator.create!(@user,
raw: I18n.t(
"#{I18N_KEY}.recover.deleted_post_raw",
i18n_post_args(discobot_username: self.discobot_user.username)
),
post = PostCreator.create!(
@user,
raw: I18n.t("#{I18N_KEY}.recover.deleted_post_raw", i18n_post_args(discobot_username: self.discobot_username)),
topic_id: data[:topic_id],
skip_bot: true,
skip_validations: true

View File

@ -44,7 +44,7 @@ module DiscourseNarrativeBot
next_state: :tutorial_mention,
next_instructions: Proc.new {
I18n.t("#{I18N_KEY}.mention.instructions",
discobot_username: self.discobot_user.username,
discobot_username: self.discobot_username,
base_uri: Discourse.base_uri)
},
reply: {
@ -462,7 +462,7 @@ module DiscourseNarrativeBot
"#{I18N_KEY}.mention.not_found",
i18n_post_args(
username: @user.username,
discobot_username: self.discobot_user.username
discobot_username: self.discobot_username
)
))
end
@ -526,7 +526,7 @@ module DiscourseNarrativeBot
username: @user.username,
base_url: Discourse.base_url,
certificate: certificate,
discobot_username: self.discobot_user.username,
discobot_username: self.discobot_username,
advanced_trigger: AdvancedUserNarrative.reset_trigger
)
),

View File

@ -115,11 +115,10 @@ module DiscourseNarrativeBot
end
def selected_track(klass)
post_raw = @post.raw
trigger = "#{self.class.reset_trigger} #{klass.reset_trigger}"
if post_raw.length < RESET_TRIGGER_EXACT_MATCH_LENGTH && @is_pm_to_bot
post_raw.match(Regexp.new("\\b\\W\?#{trigger}\\W\?\\b", 'i'))
if @post.raw.length < RESET_TRIGGER_EXACT_MATCH_LENGTH && @is_pm_to_bot
@post.raw.match(Regexp.new("\\b\\W\?#{trigger}\\W\?\\b", 'i'))
else
match_trigger?(trigger)
end
@ -137,7 +136,7 @@ module DiscourseNarrativeBot
help_message
elsif hint
message = I18n.t(self.class.i18n_key('random_mention.reply'),
discobot_username: self.discobot_user.username,
discobot_username: self.discobot_username,
help_trigger: self.class.help_trigger
)
@ -165,17 +164,15 @@ module DiscourseNarrativeBot
end
def help_message
discobot_username = self.discobot_user.username
message = I18n.t(
self.class.i18n_key('random_mention.tracks'),
discobot_username: discobot_username,
discobot_username: self.discobot_username,
reset_trigger: self.class.reset_trigger,
tracks: [NewUserNarrative.reset_trigger, AdvancedUserNarrative.reset_trigger].join(', ')
)
message << "\n\n#{I18n.t(self.class.i18n_key('random_mention.bot_actions'),
discobot_username: discobot_username,
discobot_username: self.discobot_username,
dice_trigger: self.class.dice_trigger,
quote_trigger: self.class.quote_trigger,
quote_sample: DiscourseNarrativeBot::QuoteGenerator.generate(@user),
@ -219,16 +216,16 @@ module DiscourseNarrativeBot
def skip_track?
if @is_pm_to_bot
post_raw = @post.raw
post_raw.match(/((^@#{self.discobot_user.username} #{self.class.skip_trigger})|(^#{self.class.skip_trigger}$))/i)
@post.raw.match(/((^@#{self.discobot_username} #{self.class.skip_trigger})|(^#{self.class.skip_trigger}$))/i)
else
false
end
end
def match_trigger?(trigger)
discobot_username = self.discobot_user.username
regexp = Regexp.new("<a class=\"mention\".*>@#{discobot_username}</a> #{trigger}</p>", 'i')
# we remove the leading <p> to allow for trigger to be at the end of a paragraph
cooked_trigger = PrettyText.cook("@#{self.discobot_username} #{trigger}")[3..-1]
regexp = Regexp.new(cooked_trigger, 'i')
match = @post.cooked.match(regexp)
if @is_pm_to_bot
@ -245,9 +242,7 @@ module DiscourseNarrativeBot
end
def bot_mentioned?
@bot_mentioned ||= PostAnalyzer.new(@post.raw, @post.topic_id).raw_mentions.include?(
self.discobot_user.username_lower
)
@bot_mentioned ||= PostAnalyzer.new(@post.raw, @post.topic_id).raw_mentions.include?(self.discobot_username)
end
def public_reply?

View File

@ -3,7 +3,9 @@
require 'rails_helper'
RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative do
let(:discobot_user) { ::DiscourseNarrativeBot::Base.new.discobot_user }
let(:narrative_bot) { ::DiscourseNarrativeBot::Base.new }
let(:discobot_user) { narrative_bot.discobot_user }
let(:discobot_username) { narrative_bot.discobot_username }
let(:first_post) { Fabricate(:post, user: discobot_user) }
let(:user) { Fabricate(:user) }
@ -175,7 +177,7 @@ RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative do
describe 'when reply contains the skip trigger' do
it 'should create the right reply' do
post.update!(raw: "@#{discobot_user.username} #{skip_trigger.upcase}")
post.update!(raw: "@#{discobot_username} #{skip_trigger.upcase}")
described_class.any_instance.expects(:enqueue_timeout_job).with(user)
DiscourseNarrativeBot::TrackSelector.new(:reply, user, post_id: post.id).select

View File

@ -4,7 +4,9 @@ require 'rails_helper'
describe DiscourseNarrativeBot::NewUserNarrative do
let!(:welcome_topic) { Fabricate(:topic, title: 'Welcome to Discourse') }
let(:discobot_user) { ::DiscourseNarrativeBot::Base.new.discobot_user }
let(:narrative_bot) { ::DiscourseNarrativeBot::Base.new }
let(:discobot_user) { narrative_bot.discobot_user }
let(:discobot_username) { narrative_bot.discobot_username }
let(:first_post) { Fabricate(:post, user: discobot_user) }
let(:user) { Fabricate(:user) }
@ -233,7 +235,7 @@ describe DiscourseNarrativeBot::NewUserNarrative do
describe 'when reply contains the skip trigger' do
it 'should create the right reply' do
post.update!(raw: "@#{discobot_user.username} #{skip_trigger.upcase}")
post.update!(raw: "@#{discobot_username} #{skip_trigger.upcase}")
described_class.any_instance.expects(:enqueue_timeout_job).with(user)
DiscourseNarrativeBot::TrackSelector.new(:reply, user, post_id: post.id).select
@ -335,7 +337,7 @@ describe DiscourseNarrativeBot::NewUserNarrative do
#{I18n.t('discourse_narrative_bot.new_user_narrative.onebox.reply', base_uri: '')}
#{I18n.t('discourse_narrative_bot.new_user_narrative.mention.instructions',
discobot_username: discobot_user.username, base_uri: ''
discobot_username: discobot_username, base_uri: ''
)}
RAW
@ -708,7 +710,7 @@ describe DiscourseNarrativeBot::NewUserNarrative do
expect(new_post.raw).to eq(I18n.t(
'discourse_narrative_bot.new_user_narrative.mention.instructions',
discobot_username: discobot_user.username, base_uri: ''
discobot_username: discobot_username, base_uri: ''
))
expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_mention)
@ -746,7 +748,7 @@ describe DiscourseNarrativeBot::NewUserNarrative do
#{I18n.t('discourse_narrative_bot.new_user_narrative.emoji.reply', base_uri: '')}
#{I18n.t('discourse_narrative_bot.new_user_narrative.mention.instructions',
discobot_username: discobot_user.username, base_uri: ''
discobot_username: discobot_username, base_uri: ''
)}
RAW
@ -778,7 +780,7 @@ describe DiscourseNarrativeBot::NewUserNarrative do
expect(Post.last.raw).to eq(I18n.t(
'discourse_narrative_bot.new_user_narrative.mention.not_found',
username: user.username,
discobot_username: discobot_user.username,
discobot_username: discobot_username,
base_uri: ''
))
@ -797,7 +799,7 @@ describe DiscourseNarrativeBot::NewUserNarrative do
expect(new_post.raw).to eq(I18n.t(
'discourse_narrative_bot.new_user_narrative.formatting.instructions',
discobot_username: discobot_user.username, base_uri: ''
discobot_username: discobot_username, base_uri: ''
))
expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_formatting)

View File

@ -4,12 +4,14 @@ require 'rails_helper'
describe DiscourseNarrativeBot::TrackSelector do
let(:user) { Fabricate(:user) }
let(:discobot_user) { ::DiscourseNarrativeBot::Base.new.discobot_user }
let(:narrative_bot) { ::DiscourseNarrativeBot::Base.new }
let(:discobot_user) { narrative_bot.discobot_user }
let(:discobot_username) { narrative_bot.discobot_username }
let(:narrative) { DiscourseNarrativeBot::NewUserNarrative.new }
let(:random_mention_reply) do
I18n.t('discourse_narrative_bot.track_selector.random_mention.reply',
discobot_username: discobot_user.username,
discobot_username: discobot_username,
help_trigger: described_class.help_trigger
)
end
@ -20,8 +22,6 @@ describe DiscourseNarrativeBot::TrackSelector do
end
let(:help_message) do
discobot_username = discobot_user.username
end_message = <<~RAW
#{I18n.t(
'discourse_narrative_bot.track_selector.random_mention.tracks',

View File

@ -23,7 +23,7 @@ RSpec.describe Jobs::SendAdvancedTutorialMessage do
)
expect(topic.first_post.raw).to eq(I18n.t(
'discourse_narrative_bot.tl2_promotion_message.text_body_template',
discobot_username: ::DiscourseNarrativeBot::Base.new.discobot_user.username,
discobot_username: ::DiscourseNarrativeBot::Base.new.discobot_username,
reset_trigger: "#{::DiscourseNarrativeBot::TrackSelector.reset_trigger} #{::DiscourseNarrativeBot::AdvancedUserNarrative.reset_trigger}"
).chomp)
end