diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/actions.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/actions.rb index 6315bb9c90c..321362c765a 100644 --- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/actions.rb +++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/actions.rb @@ -77,5 +77,20 @@ module DiscourseNarrativeBot topic.pm_with_non_human_user? && topic.topic_allowed_users.where(user_id: -2).exists? end + + def cancel_timeout_job(user) + Jobs.cancel_scheduled_job(:narrative_timeout, user_id: user.id, klass: self.class.to_s) + end + + def enqueue_timeout_job(user) + return if Rails.env.test? + + cancel_timeout_job(user) + + Jobs.enqueue_in(TIMEOUT_DURATION, :narrative_timeout, + user_id: user.id, + klass: self.class.to_s + ) + end end end 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 c3af709b200..ee4205cebe0 100644 --- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/base.rb +++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/base.rb @@ -163,21 +163,6 @@ module DiscourseNarrativeBot topic_id == @data[:topic_id] end - def cancel_timeout_job(user) - Jobs.cancel_scheduled_job(:narrative_timeout, user_id: user.id, klass: self.class.to_s) - end - - def enqueue_timeout_job(user) - return if Rails.env.test? - - cancel_timeout_job(user) - - Jobs.enqueue_in(TIMEOUT_DURATION, :narrative_timeout, - user_id: user.id, - klass: self.class.to_s - ) - end - def not_implemented raise 'Not implemented.' end diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/track_selector.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/track_selector.rb index 33c1f0fa9aa..bd3d10b7456 100644 --- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/track_selector.rb +++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/track_selector.rb @@ -39,7 +39,7 @@ module DiscourseNarrativeBot klass = (data[:track] || NewUserNarrative.to_s).constantize if is_reply && like_user_post - Store.set(@user.id, data.merge!(state: nil, topic_id: nil)) + terminate_track(data) elsif state&.to_sym == :end && is_reply bot_commands(bot_mentioned?) || generic_replies(klass.reset_trigger) elsif is_reply @@ -253,5 +253,10 @@ module DiscourseNarrativeBot !SiteSetting.discourse_narrative_bot_disable_public_replies && (bot_mentioned? || reply_to_bot_post?(@post)) end + + def terminate_track(data) + Store.set(@user.id, data.merge!(state: nil, topic_id: nil)) + cancel_timeout_job(@user) + end end end diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb index 39de599ac6a..9e721e0b47d 100644 --- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb +++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb @@ -127,9 +127,6 @@ describe DiscourseNarrativeBot::TrackSelector do expect(post_action.post).to eq(post) expect(post_action.post_action_type_id).to eq(PostActionType.types[:like]) - - post = Post.last - expect(Post.last).to eq(post) expect(DiscourseNarrativeBot::NewUserNarrative.new.get_data(user)['state'])