[User] Remove memoization of throttles

This interfered with tests and doesn't seem to give much of an advantage
This commit is contained in:
Earlopain 2021-05-15 12:36:29 +02:00
parent 9df05e7ba8
commit 8d8a0802a5
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
3 changed files with 10 additions and 8 deletions

View File

@ -90,8 +90,6 @@ class VoteManager
begin
raise CommentVote::Error.new("Invalid vote") unless [1, -1].include?(score)
raise CommentVote::Error.new("You do not have permission to vote") unless user.is_voter?
reason = user.can_comment_vote_with_reason
raise CommentVote::Error.new("You #{User.throttle_reason(reason)}") unless reason == true
target_isolation = !Rails.env.test? ? {isolation: :serializable} : {}
CommentVote.transaction(**target_isolation) do
CommentVote.uncached do

View File

@ -481,9 +481,7 @@ class User < ApplicationRecord
def self.create_user_throttle(name, limiter, checker, newbie_duration)
define_method("#{name}_limit".to_sym, limiter)
unless Rails.env.test?
memoize "#{name}_limit".to_sym
end
define_method("can_#{name}_with_reason".to_sym) do
return true if Danbooru.config.disable_throttles
return send(checker) if checker && send(checker)

View File

@ -69,15 +69,21 @@ class UserTest < ActiveSupport::TestCase
end
should "limit comment votes" do
Danbooru.config.stubs(:member_comment_limit).returns(10)
# allow creating one more comment than votes so creating a vote can fail later on
Danbooru.config.stubs(:member_comment_limit).returns(Danbooru.config.comment_vote_limit + 1)
assert_equal(@user.can_comment_vote_with_reason, :REJ_NEWBIE)
@user.update_column(:created_at, 1.year.ago)
10.times do
Danbooru.config.comment_vote_limit.times do
comment = FactoryBot.create(:comment)
FactoryBot.create(:comment_vote, :comment_id => comment.id, :score => -1)
VoteManager.comment_vote!(comment: comment, user: @user, score: -1)
end
assert_equal(@user.can_comment_vote_with_reason, :REJ_LIMITED)
comment = FactoryBot.create(:comment)
assert_raises ActiveRecord::RecordInvalid do
VoteManager.comment_vote!(comment: comment, user: @user, score: -1)
end
CommentVote.update_all("created_at = '1990-01-01'")
assert_equal(@user.can_comment_vote_with_reason, true)
end