FIX: Track should only continue if user is alone with bot in a PM.

This commit is contained in:
Guo Xiang Tan
2017-06-06 09:46:28 +09:00
parent ba578f2aa9
commit c1dc6f6cd7
2 changed files with 21 additions and 8 deletions

View File

@ -30,11 +30,11 @@ module DiscourseNarrativeBot
if @post && @post.post_type == Post.types[:regular] && !is_topic_action? if @post && @post.post_type == Post.types[:regular] && !is_topic_action?
is_reply = @input == :reply is_reply = @input == :reply
@is_pm_to_bot = pm_to_bot?(@post)
return if is_reply && reset_track return if is_reply && reset_track
topic_id = @post.topic_id if data && (data[:topic_id] == @post.topic_id) && @is_pm_to_bot
if (data && data[:topic_id] == topic_id)
state = data[:state] state = data[:state]
klass = (data[:track] || NewUserNarrative.to_s).constantize klass = (data[:track] || NewUserNarrative.to_s).constantize
@ -58,7 +58,7 @@ module DiscourseNarrativeBot
else else
klass.new.input(@input, @user, post: @post, skip: skip_track?) klass.new.input(@input, @user, post: @post, skip: skip_track?)
end end
elsif is_reply && (pm_to_bot?(@post) || public_reply?) elsif is_reply && (@is_pm_to_bot || public_reply?)
like_user_post like_user_post
bot_commands bot_commands
end end
@ -116,7 +116,7 @@ module DiscourseNarrativeBot
post_raw = @post.raw post_raw = @post.raw
trigger = "#{self.class.reset_trigger} #{klass.reset_trigger}" trigger = "#{self.class.reset_trigger} #{klass.reset_trigger}"
if post_raw.length < RESET_TRIGGER_EXACT_MATCH_LENGTH && pm_to_bot?(@post) if post_raw.length < RESET_TRIGGER_EXACT_MATCH_LENGTH && @is_pm_to_bot
post_raw.match(Regexp.new("\\b\\W\?#{trigger}\\W\?\\b", 'i')) post_raw.match(Regexp.new("\\b\\W\?#{trigger}\\W\?\\b", 'i'))
else else
match_trigger?(trigger) match_trigger?(trigger)
@ -215,7 +215,7 @@ module DiscourseNarrativeBot
end end
def skip_track? def skip_track?
if pm_to_bot?(@post) if @is_pm_to_bot
post_raw = @post.raw post_raw = @post.raw
post_raw.match(/^@#{self.discobot_user.username} #{self.class.skip_trigger}/i) || post_raw.match(/^@#{self.discobot_user.username} #{self.class.skip_trigger}/i) ||
@ -230,7 +230,7 @@ module DiscourseNarrativeBot
regexp = Regexp.new("<a class=\"mention\".*>@#{discobot_username}</a> #{trigger}", 'i') regexp = Regexp.new("<a class=\"mention\".*>@#{discobot_username}</a> #{trigger}", 'i')
match = @post.cooked.match(regexp) match = @post.cooked.match(regexp)
if pm_to_bot?(@post) if @is_pm_to_bot
match || @post.raw.strip.match(Regexp.new("^#{trigger}$", 'i')) match || @post.raw.strip.match(Regexp.new("^#{trigger}$", 'i'))
else else
match match
@ -255,7 +255,7 @@ module DiscourseNarrativeBot
end end
def terminate_track(data) def terminate_track(data)
Store.set(@user.id, data.merge!(state: nil, topic_id: nil)) Store.set(@user.id, data.merge!(track: nil, state: nil, topic_id: nil))
cancel_timeout_job(@user) cancel_timeout_job(@user)
end end
end end

View File

@ -188,6 +188,19 @@ describe DiscourseNarrativeBot::TrackSelector do
end end
end end
end end
context 'when a new user is added into the topic' do
before do
topic.allowed_users << Fabricate(:user)
end
it 'should stop the new user track' do
post
expect { described_class.new(:reply, user, post_id: post.id).select }
.to_not change { Post.count }
end
end
end end
context 'at the end of a tutorial track' do context 'at the end of a tutorial track' do