From 3c31884b7989ea1f6079f432b94c78a60dd4f831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Thu, 25 Jun 2020 13:29:55 +0200 Subject: [PATCH] FIX: match discobot triggers on cooked version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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' --- .../jobs/send_advanced_tutorial_message.rb | 2 +- .../lib/discourse_narrative_bot/actions.rb | 6 ++++- .../advanced_user_narrative.rb | 22 ++++++++-------- .../new_user_narrative.rb | 6 ++--- .../discourse_narrative_bot/track_selector.rb | 25 ++++++++----------- .../advanced_user_narrative_spec.rb | 6 +++-- .../new_user_narrative_spec.rb | 16 ++++++------ .../track_selector_spec.rb | 8 +++--- .../send_advanced_tutorial_message_spec.rb | 2 +- 9 files changed, 47 insertions(+), 46 deletions(-) diff --git a/plugins/discourse-narrative-bot/autoload/jobs/send_advanced_tutorial_message.rb b/plugins/discourse-narrative-bot/autoload/jobs/send_advanced_tutorial_message.rb index d151e6d76a8..91b3479d2e1 100644 --- a/plugins/discourse-narrative-bot/autoload/jobs/send_advanced_tutorial_message.rb +++ b/plugins/discourse-narrative-bot/autoload/jobs/send_advanced_tutorial_message.rb @@ -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!( diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/actions.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/actions.rb index 56afacf2af7..f69ed8834c3 100644 --- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/actions.rb +++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/actions.rb @@ -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 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 bd6bb02e492..53e4d39f602 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 @@ -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 diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/new_user_narrative.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/new_user_narrative.rb index 9898ba085e8..f9d1b639daf 100644 --- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/new_user_narrative.rb +++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/new_user_narrative.rb @@ -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 ) ), diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/track_selector.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/track_selector.rb index c370934965a..c5c1a58c60f 100644 --- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/track_selector.rb +++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/track_selector.rb @@ -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("@#{discobot_username} #{trigger}

", 'i') + # we remove the leading

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? 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 da97a7df58a..a6d5956a8bd 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 @@ -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 diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb index ce65b15463b..cfb4f00b018 100644 --- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb +++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb @@ -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) diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb index 6914dff1d2f..f60aad254d5 100644 --- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb +++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb @@ -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', diff --git a/plugins/discourse-narrative-bot/spec/jobs/send_advanced_tutorial_message_spec.rb b/plugins/discourse-narrative-bot/spec/jobs/send_advanced_tutorial_message_spec.rb index 874e40cfe81..06233a03311 100644 --- a/plugins/discourse-narrative-bot/spec/jobs/send_advanced_tutorial_message_spec.rb +++ b/plugins/discourse-narrative-bot/spec/jobs/send_advanced_tutorial_message_spec.rb @@ -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