From 9f873fa66cda23c4905e142beb6b3d771df1f2ba Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 25 May 2018 16:01:37 +0800 Subject: [PATCH] FIX: Invalid setex expire time in `DiscourseNarrativeBot::Actions#reset_rate_limits`. --- .../lib/discourse_narrative_bot/actions.rb | 19 ++++++++++--------- .../new_user_narrative_spec.rb | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 9 deletions(-) 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 d8b6c47b1aa..349aedd5237 100644 --- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/actions.rb +++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/actions.rb @@ -32,22 +32,23 @@ module DiscourseNarrativeBot def reset_rate_limits(post) user = post.user - data = DiscourseNarrativeBot::Store.get(user.id.to_s) + duration = + if user && user.new_user? + SiteSetting.rate_limit_new_user_create_post + else + SiteSetting.rate_limit_create_post + end + + return unless duration > 0 + + data = DiscourseNarrativeBot::Store.get(user.id.to_s) return unless data key = "#{DiscourseNarrativeBot::PLUGIN_NAME}:reset-rate-limit:#{post.topic_id}:#{data['state']}" if !(count = $redis.get(key)) count = 0 - - duration = - if user && user.new_user? - SiteSetting.rate_limit_new_user_create_post - else - SiteSetting.rate_limit_create_post - end - $redis.setex(key, duration, count) end 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 1fe809178bc..03d550f18f2 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 @@ -211,6 +211,22 @@ describe DiscourseNarrativeBot::NewUserNarrative do expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_bookmark) end + describe 'when rate_limit_new_user_create_post site setting is disabled' do + before do + SiteSetting.rate_limit_new_user_create_post = 0 + end + + it 'should create the right reply' do + narrative.input(:reply, user, post: post) + new_post = Post.last + + expect(new_post.raw).to eq(I18n.t( + 'discourse_narrative_bot.new_user_narrative.bookmark.not_found', + base_uri: '' + )) + end + end + describe 'when reply contains the skip trigger' do it 'should create the right reply' do post.update!(raw: "@#{discobot_user.username} #{skip_trigger.upcase}")