FEATURE: max topics/replies per day for new users now starts counting from the first post, not signup date
This commit is contained in:
parent
e9a293beeb
commit
487c20959c
|
@ -109,7 +109,7 @@ class Post < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def limit_posts_per_day
|
def limit_posts_per_day
|
||||||
if user && user.first_day_user? && post_number && post_number > 1
|
if user && user.new_user_posting_on_first_day? && post_number && post_number > 1
|
||||||
RateLimiter.new(user, "first-day-replies-per-day", SiteSetting.max_replies_in_first_day, 1.day.to_i)
|
RateLimiter.new(user, "first-day-replies-per-day", SiteSetting.max_replies_in_first_day, 1.day.to_i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -268,7 +268,7 @@ class Topic < ActiveRecord::Base
|
||||||
|
|
||||||
# Additional rate limits on topics: per day and private messages per day
|
# Additional rate limits on topics: per day and private messages per day
|
||||||
def limit_topics_per_day
|
def limit_topics_per_day
|
||||||
if user && user.first_day_user?
|
if user && user.new_user_posting_on_first_day?
|
||||||
limit_first_day_topics_per_day
|
limit_first_day_topics_per_day
|
||||||
else
|
else
|
||||||
apply_per_day_rate_limit_for("topics", :max_topics_per_day)
|
apply_per_day_rate_limit_for("topics", :max_topics_per_day)
|
||||||
|
|
|
@ -410,10 +410,10 @@ class User < ActiveRecord::Base
|
||||||
self.password_hash == hash_password(password, salt)
|
self.password_hash == hash_password(password, salt)
|
||||||
end
|
end
|
||||||
|
|
||||||
def first_day_user?
|
def new_user_posting_on_first_day?
|
||||||
!staff? &&
|
!staff? &&
|
||||||
trust_level < TrustLevel[2] &&
|
trust_level < TrustLevel[2] &&
|
||||||
created_at >= 24.hours.ago
|
(self.first_post_created_at.nil? || self.first_post_created_at >= 24.hours.ago)
|
||||||
end
|
end
|
||||||
|
|
||||||
def new_user?
|
def new_user?
|
||||||
|
|
|
@ -1499,34 +1499,53 @@ describe Topic do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "limits new users to max_topics_in_first_day and max_posts_in_first_day" do
|
context "new user limits" do
|
||||||
SiteSetting.stubs(:max_topics_in_first_day).returns(1)
|
before do
|
||||||
SiteSetting.stubs(:max_replies_in_first_day).returns(1)
|
SiteSetting.max_topics_in_first_day = 1
|
||||||
SiteSetting.stubs(:client_settings_json).returns(SiteSetting.client_settings_json_uncached)
|
SiteSetting.max_replies_in_first_day = 1
|
||||||
RateLimiter.stubs(:rate_limit_create_topic).returns(100)
|
SiteSetting.stubs(:client_settings_json).returns(SiteSetting.client_settings_json_uncached)
|
||||||
RateLimiter.stubs(:disabled?).returns(false)
|
RateLimiter.stubs(:rate_limit_create_topic).returns(100)
|
||||||
RateLimiter.clear_all!
|
RateLimiter.stubs(:disabled?).returns(false)
|
||||||
|
RateLimiter.clear_all!
|
||||||
|
end
|
||||||
|
|
||||||
start = Time.now.tomorrow.beginning_of_day
|
it "limits new users to max_topics_in_first_day and max_posts_in_first_day" do
|
||||||
|
start = Time.now.tomorrow.beginning_of_day
|
||||||
|
|
||||||
freeze_time(start)
|
freeze_time(start)
|
||||||
|
|
||||||
user = Fabricate(:user)
|
user = Fabricate(:user)
|
||||||
topic_id = create_post(user: user).topic_id
|
topic_id = create_post(user: user).topic_id
|
||||||
|
|
||||||
freeze_time(start + 10.minutes)
|
freeze_time(start + 10.minutes)
|
||||||
expect {
|
expect { create_post(user: user) }.to raise_error(RateLimiter::LimitExceeded)
|
||||||
create_post(user: user)
|
|
||||||
}.to raise_error(RateLimiter::LimitExceeded)
|
|
||||||
|
|
||||||
freeze_time(start + 20.minutes)
|
freeze_time(start + 20.minutes)
|
||||||
create_post(user: user, topic_id: topic_id)
|
|
||||||
|
|
||||||
freeze_time(start + 30.minutes)
|
|
||||||
|
|
||||||
expect {
|
|
||||||
create_post(user: user, topic_id: topic_id)
|
create_post(user: user, topic_id: topic_id)
|
||||||
}.to raise_error(RateLimiter::LimitExceeded)
|
|
||||||
|
freeze_time(start + 30.minutes)
|
||||||
|
expect { create_post(user: user, topic_id: topic_id) }.to raise_error(RateLimiter::LimitExceeded)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "starts counting when they make their first post/topic" do
|
||||||
|
start = Time.now.tomorrow.beginning_of_day
|
||||||
|
|
||||||
|
freeze_time(start)
|
||||||
|
|
||||||
|
user = Fabricate(:user)
|
||||||
|
|
||||||
|
freeze_time(start + 25.hours)
|
||||||
|
topic_id = create_post(user: user).topic_id
|
||||||
|
|
||||||
|
freeze_time(start + 26.hours)
|
||||||
|
expect { create_post(user: user) }.to raise_error(RateLimiter::LimitExceeded)
|
||||||
|
|
||||||
|
freeze_time(start + 27.hours)
|
||||||
|
create_post(user: user, topic_id: topic_id)
|
||||||
|
|
||||||
|
freeze_time(start + 28.hours)
|
||||||
|
expect { create_post(user: user, topic_id: topic_id) }.to raise_error(RateLimiter::LimitExceeded)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".count_exceeds_minimun?" do
|
describe ".count_exceeds_minimun?" do
|
||||||
|
|
|
@ -806,17 +806,29 @@ describe User do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#first_day_user?" do
|
describe "#new_user_posting_on_first_day?" do
|
||||||
|
|
||||||
def test_user?(opts={})
|
def test_user?(opts={})
|
||||||
Fabricate.build(:user, {created_at: Time.now}.merge(opts)).first_day_user?
|
Fabricate.build(:user, {created_at: Time.zone.now}.merge(opts)).new_user_posting_on_first_day?
|
||||||
end
|
end
|
||||||
|
|
||||||
it "works" do
|
it "handles when user has never posted" do
|
||||||
expect(test_user?).to eq(true)
|
expect(test_user?).to eq(true)
|
||||||
expect(test_user?(moderator: true)).to eq(false)
|
expect(test_user?(moderator: true)).to eq(false)
|
||||||
expect(test_user?(trust_level: TrustLevel[2])).to eq(false)
|
expect(test_user?(trust_level: TrustLevel[2])).to eq(false)
|
||||||
expect(test_user?(created_at: 2.days.ago)).to eq(false)
|
expect(test_user?(created_at: 2.days.ago)).to eq(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is true for a user who posted less than 24 hours ago but was created over 1 day ago" do
|
||||||
|
u = Fabricate(:user, created_at: 28.hours.ago)
|
||||||
|
u.user_stat.first_post_created_at = 1.hour.ago
|
||||||
|
expect(u.new_user_posting_on_first_day?).to eq(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is false if first post was more than 24 hours ago" do
|
||||||
|
u = Fabricate(:user, created_at: 28.hours.ago)
|
||||||
|
u.user_stat.first_post_created_at = 25.hours.ago
|
||||||
|
expect(u.new_user_posting_on_first_day?).to eq(false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue