FIX: Invalid setex expire time in `DiscourseNarrativeBot::Actions#reset_rate_limits`.

This commit is contained in:
Guo Xiang Tan 2018-05-25 16:01:37 +08:00
parent 12e4dd59b7
commit 9f873fa66c
2 changed files with 26 additions and 9 deletions

View File

@ -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

View File

@ -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}")