From 18dad4cfbd8b26182561516c52f8b3bf233b3dcf Mon Sep 17 00:00:00 2001 From: Blake Erickson Date: Mon, 3 Aug 2020 19:38:04 -0600 Subject: [PATCH] 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 --- .../config/locales/server.en.yml | 2 +- .../discourse_narrative_bot/new_user_narrative.rb | 9 ++++++++- plugins/discourse-narrative-bot/plugin.rb | 4 ++-- .../new_user_narrative_spec.rb | 15 ++++++++++++++- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/plugins/discourse-narrative-bot/config/locales/server.en.yml b/plugins/discourse-narrative-bot/config/locales/server.en.yml index 76efd5aa6d0..33bd9b72ea7 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** ? Don’t forget to use the show more button 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** ? Don’t forget to use the show more button 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 f9d1b639daf..8a9ed39cb49 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 61047c0e8b2..55e765522fc 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 9c3b5b73b8f..89d644c0335 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