FIX: match discobot triggers on cooked version

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’

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 d151e6d..91b3479 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 56afacf..f69ed88 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 bd6bb02..53e4d39 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 9898ba0..f9d1b63 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 c370934..c5c1a58 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("<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]

[... diff too long, it was truncated ...]

GitHub sha: 3c31884b

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/discobot-french-translation-problem/152248/12

Nice change! :+1: :+1::+1::+1:

1 Like

Is it possible to cache or momoize this? I think cooking text is relatively expensive and this is a code path that we have to run for every single post.

1 Like

I think we should add a spec to ensure that this behavior doesn’t regress as well.

1 Like

https://review.discourse.org/t/spec-add-spec-to-ensure-discobot-works-in-french/13003/6