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,14 +32,6 @@ module DiscourseNarrativeBot
def reset_rate_limits(post) def reset_rate_limits(post)
user = post.user user = post.user
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 = duration =
if user && user.new_user? if user && user.new_user?
@ -48,6 +40,15 @@ module DiscourseNarrativeBot
SiteSetting.rate_limit_create_post SiteSetting.rate_limit_create_post
end 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
$redis.setex(key, duration, count) $redis.setex(key, duration, count)
end end

View File

@ -211,6 +211,22 @@ describe DiscourseNarrativeBot::NewUserNarrative do
expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_bookmark) expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_bookmark)
end 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 describe 'when reply contains the skip trigger' do
it 'should create the right reply' do it 'should create the right reply' do
post.update!(raw: "@#{discobot_user.username} #{skip_trigger.upcase}") post.update!(raw: "@#{discobot_user.username} #{skip_trigger.upcase}")