From c049e18203136e9c13e77c8cfb287cf8841640bb Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 13 Jul 2017 15:12:45 +0900 Subject: [PATCH] FEATURE: Allow tutorials to declare prerequisite before it starts. --- .../advanced_user_narrative.rb | 2 +- .../lib/discourse_narrative_bot/base.rb | 15 ++++ .../new_user_narrative.rb | 1 + .../new_user_narrative_spec.rb | 76 ++++++++++++------- 4 files changed, 67 insertions(+), 27 deletions(-) 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 00b9e4366f0..fb6b4e2e14a 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 @@ -74,7 +74,7 @@ module DiscourseNarrativeBot action: :reply_to_topic_notification_level_changed }, reply: { - next_state: :tutorial_notification_level, + next_state: :tutorial_change_topic_notification_level, action: :missing_topic_notification_level_change } }, diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/base.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/base.rb index 3552d1663f3..e00cf5eb2f0 100644 --- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/base.rb +++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/base.rb @@ -19,6 +19,21 @@ module DiscourseNarrativeBot begin opts = transition + + loop do + next_state = opts[:next_state] + + break if next_state == :end + + next_opts = self.class::TRANSITION_TABLE.fetch(next_state) + prerequisite = next_opts[:prerequisite] + + break if !prerequisite || instance_eval(&prerequisite) + + [:next_state, :next_instructions].each do |key| + opts[key] = next_opts[key] + end + end rescue InvalidTransitionError # For given input, no transition for current state return 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 a0ba45f2046..76e5b2e6fe0 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 @@ -38,6 +38,7 @@ module DiscourseNarrativeBot }, tutorial_emoji: { + prerequisite: Proc.new { SiteSetting.enable_emoji }, next_state: :tutorial_mention, next_instructions: Proc.new { I18n.t("#{I18N_KEY}.mention.instructions", 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 836a715099c..3c712876b86 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 @@ -302,23 +302,47 @@ describe DiscourseNarrativeBot::NewUserNarrative do expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_emoji) end end - end - it 'should create the right reply' do - post.update!(raw: 'https://en.wikipedia.org/wiki/ROT13') + describe 'when emoji is disabled' do + before do + SiteSetting.enable_emoji = false + end - narrative.expects(:enqueue_timeout_job).with(user) - narrative.input(:reply, user, post: post) - new_post = Post.last + it 'should create the right reply' do + post.update!(raw: 'https://en.wikipedia.org/wiki/ROT13') - expected_raw = <<~RAW - #{I18n.t('discourse_narrative_bot.new_user_narrative.onebox.reply', base_uri: '')} + narrative.input(:reply, user, post: post) + new_post = Post.last - #{I18n.t('discourse_narrative_bot.new_user_narrative.emoji.instructions', base_uri: '')} - RAW + expected_raw = <<~RAW + #{I18n.t('discourse_narrative_bot.new_user_narrative.onebox.reply', base_uri: '')} - expect(new_post.raw).to eq(expected_raw.chomp) - expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_emoji) + #{I18n.t('discourse_narrative_bot.new_user_narrative.mention.instructions', + discobot_username: discobot_user.username, base_uri: '' + )} + RAW + + expect(new_post.raw).to eq(expected_raw.chomp) + expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_mention) + end + end + + it 'should create the right reply' do + post.update!(raw: 'https://en.wikipedia.org/wiki/ROT13') + + narrative.expects(:enqueue_timeout_job).with(user) + narrative.input(:reply, user, post: post) + new_post = Post.last + + expected_raw = <<~RAW + #{I18n.t('discourse_narrative_bot.new_user_narrative.onebox.reply', base_uri: '')} + + #{I18n.t('discourse_narrative_bot.new_user_narrative.emoji.instructions', base_uri: '')} + RAW + + expect(new_post.raw).to eq(expected_raw.chomp) + expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_emoji) + end end end @@ -585,25 +609,25 @@ describe DiscourseNarrativeBot::NewUserNarrative do expect(new_post.raw).to include("/forum/images") end end - end - it 'should create the right reply' do - post.update!( - raw: '[quote="#{post.user}, post:#{post.post_number}, topic:#{topic.id}"]\n:monkey: :fries:\n[/quote]' - ) + it 'should create the right reply' do + post.update!( + raw: '[quote="#{post.user}, post:#{post.post_number}, topic:#{topic.id}"]\n:monkey: :fries:\n[/quote]' + ) - narrative.expects(:enqueue_timeout_job).with(user) - narrative.input(:reply, user, post: post) - new_post = Post.last + narrative.expects(:enqueue_timeout_job).with(user) + narrative.input(:reply, user, post: post) + new_post = Post.last - expected_raw = <<~RAW - #{I18n.t('discourse_narrative_bot.new_user_narrative.quoting.reply', base_uri: '')} + expected_raw = <<~RAW + #{I18n.t('discourse_narrative_bot.new_user_narrative.quoting.reply', base_uri: '')} - #{I18n.t('discourse_narrative_bot.new_user_narrative.images.instructions', base_uri: '')} - RAW + #{I18n.t('discourse_narrative_bot.new_user_narrative.images.instructions', base_uri: '')} + RAW - expect(new_post.raw).to eq(expected_raw.chomp) - expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_images) + expect(new_post.raw).to eq(expected_raw.chomp) + expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_images) + end end end