FIX: discobot inappropriate flag section

FIX: discobot inappropriate flag section

Ensure that the inappropriate flag is used and not some other flag type. If some other flag is used a message will be posted letting the user know they used the wrong flag and the original flag will be removed so that they can try again.

Bug reported on meta: https://meta.discourse.org/t/-/157075

diff --git a/plugins/discourse-narrative-bot/config/locales/server.en.yml b/plugins/discourse-narrative-bot/config/locales/server.en.yml
index 76efd5a..33bd9b7 100644
--- a/plugins/discourse-narrative-bot/config/locales/server.en.yml
+++ b/plugins/discourse-narrative-bot/config/locales/server.en.yml
@@ -291,7 +291,7 @@ en:
         reply: |-
           [Our staff](%{base_uri}/groups/staff) will be privately notified about your flag. If enough community members flag a post, it will also be automatically hidden as a precaution. (Since I didn’t actually write a nasty post :angel:, I’ve gone ahead and removed the flag for now.)
         not_found: |-
-          Oh no, my nasty post hasn’t been flagged yet. :worried: Can you flag it as inappropriate using the **flag** <img src="%{base_uri}/plugins/discourse-narrative-bot/images/font-awesome-flag.png" width="16" height="16">? Don’t forget to use the show more button <img src="%{base_uri}/plugins/discourse-narrative-bot/images/font-awesome-ellipsis.png" width="16" height="16"> to reveal more actions for each post.
+          Oh no, my nasty post hasn’t been flagged as inappropriate yet. :worried: Can you flag it as inappropriate using the **flag** <img src="%{base_uri}/plugins/discourse-narrative-bot/images/font-awesome-flag.png" width="16" height="16">? Don’t forget to use the show more button <img src="%{base_uri}/plugins/discourse-narrative-bot/images/font-awesome-ellipsis.png" width="16" height="16"> to reveal more actions for each post.
 
       search:
         instructions: |-
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 f9d1b63..8a9ed39 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
@@ -474,7 +474,14 @@ module DiscourseNarrativeBot
 
     def missing_flag
       return unless valid_topic?(@post.topic_id)
-      return if @post.user_id == -2
+
+      # Remove any incorrect flags so that they can try again
+      if @post.user_id == -2
+        @post.post_actions
+          .where(user_id: @user.id)
+          .where("post_action_type_id IN (?)", (PostActionType.flag_types.values - [PostActionType.types[:inappropriate]]))
+          .destroy_all
+      end
 
       fake_delay
       reply_to(@post, I18n.t("#{I18N_KEY}.flag.not_found", i18n_post_args)) unless @data[:attempted]
diff --git a/plugins/discourse-narrative-bot/plugin.rb b/plugins/discourse-narrative-bot/plugin.rb
index 61047c0..55e7655 100644
--- a/plugins/discourse-narrative-bot/plugin.rb
+++ b/plugins/discourse-narrative-bot/plugin.rb
@@ -223,8 +223,8 @@ after_initialize do
     if self.post && self.user.enqueue_narrative_bot_job?
       input =
         case self.post_action_type_id
-        when *PostActionType.flag_types_without_custom.values
-          :flag
+        when *PostActionType.flag_types.values
+          self.post_action_type_id == PostActionType.types[:inappropriate] ? :flag : :reply
         when PostActionType.types[:like]
           :like
         when PostActionType.types[:bookmark]
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 9c3b5b7..89d644c 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
@@ -840,7 +840,9 @@ describe DiscourseNarrativeBot::NewUserNarrative do
 
     describe 'flag tutorial' do
       let(:post) { Fabricate(:post, user: discobot_user, topic: topic) }
-      let(:flag) { Fabricate(:flag, post: post, user: user) }
+      let(:another_post) { Fabricate(:post, user: discobot_user, topic: topic) }
+      let(:flag) { Fabricate(:flag, post: post, user: user, post_action_type_id: PostActionType.types[:inappropriate]) }
+      let(:other_flag) { Fabricate(:flag, post: another_post, user: user, post_action_type_id: PostActionType.types[:spam]) }
       let(:other_post) { Fabricate(:post, user: user, topic: topic) }
 
       before do
@@ -858,6 +860,17 @@ describe DiscourseNarrativeBot::NewUserNarrative do
         end
       end
 
+      describe 'when post is flagged incorrectly' do
+        it 'should create the right reply and stay on the same step' do
+          narrative.expects(:enqueue_timeout_job).with(user).never
+          other_flag.update!(post: another_post)
+          new_post = Post.last
+
+          expect(new_post.raw).to eq(I18n.t('discourse_narrative_bot.new_user_narrative.flag.not_found', base_uri: ''))
+          expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_flag)
+        end
+      end
+
       describe 'when post being flagged does not belong to discobot ' do
         it 'should not do anything' do
           narrative.expects(:enqueue_timeout_job).with(user).never

GitHub sha: 18dad4cf

1 Like

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

https://meta.discourse.org/t/discobot-tutorial-gets-stuck-if-something-else-flag-type-is-selected/157075/13