mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 22:43:33 +08:00
FEATURE: Allow tutorials to declare prerequisite before it starts.
This commit is contained in:
@ -74,7 +74,7 @@ module DiscourseNarrativeBot
|
|||||||
action: :reply_to_topic_notification_level_changed
|
action: :reply_to_topic_notification_level_changed
|
||||||
},
|
},
|
||||||
reply: {
|
reply: {
|
||||||
next_state: :tutorial_notification_level,
|
next_state: :tutorial_change_topic_notification_level,
|
||||||
action: :missing_topic_notification_level_change
|
action: :missing_topic_notification_level_change
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -19,6 +19,21 @@ module DiscourseNarrativeBot
|
|||||||
|
|
||||||
begin
|
begin
|
||||||
opts = transition
|
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
|
rescue InvalidTransitionError
|
||||||
# For given input, no transition for current state
|
# For given input, no transition for current state
|
||||||
return
|
return
|
||||||
|
@ -38,6 +38,7 @@ module DiscourseNarrativeBot
|
|||||||
},
|
},
|
||||||
|
|
||||||
tutorial_emoji: {
|
tutorial_emoji: {
|
||||||
|
prerequisite: Proc.new { SiteSetting.enable_emoji },
|
||||||
next_state: :tutorial_mention,
|
next_state: :tutorial_mention,
|
||||||
next_instructions: Proc.new {
|
next_instructions: Proc.new {
|
||||||
I18n.t("#{I18N_KEY}.mention.instructions",
|
I18n.t("#{I18N_KEY}.mention.instructions",
|
||||||
|
@ -302,6 +302,29 @@ describe DiscourseNarrativeBot::NewUserNarrative do
|
|||||||
expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_emoji)
|
expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_emoji)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'when emoji is disabled' do
|
||||||
|
before do
|
||||||
|
SiteSetting.enable_emoji = false
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should create the right reply' do
|
||||||
|
post.update!(raw: 'https://en.wikipedia.org/wiki/ROT13')
|
||||||
|
|
||||||
|
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.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
|
end
|
||||||
|
|
||||||
it 'should create the right reply' do
|
it 'should create the right reply' do
|
||||||
@ -321,6 +344,7 @@ describe DiscourseNarrativeBot::NewUserNarrative do
|
|||||||
expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_emoji)
|
expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_emoji)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe 'images tutorial' do
|
describe 'images tutorial' do
|
||||||
let(:post_2) { Fabricate(:post, topic: topic) }
|
let(:post_2) { Fabricate(:post, topic: topic) }
|
||||||
@ -585,7 +609,6 @@ describe DiscourseNarrativeBot::NewUserNarrative do
|
|||||||
expect(new_post.raw).to include("/forum/images")
|
expect(new_post.raw).to include("/forum/images")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
it 'should create the right reply' do
|
it 'should create the right reply' do
|
||||||
post.update!(
|
post.update!(
|
||||||
@ -606,6 +629,7 @@ describe DiscourseNarrativeBot::NewUserNarrative do
|
|||||||
expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_images)
|
expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_images)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe 'emoji tutorial' do
|
describe 'emoji tutorial' do
|
||||||
before do
|
before do
|
||||||
|
Reference in New Issue
Block a user