A platform for community discussion. Free, open, simple.
DEV: Apply different first-day limits for TL0 and TL1 (#41433)
Penar Musarajcf9a37c78842
diff --git a/app/models/post.rb b/app/models/post.rbindex cce9c321df1..2e0e47d5de5 100644--- a/app/models/post.rb+++ b/app/models/post.rb@@ -210,12 +210,7 @@ class Post < ActiveRecord::Basedef limit_posts_per_dayif 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", user.first_day_replies_limit, 1.day.to_i)endenddiff --git a/app/models/topic.rb b/app/models/topic.rbindex cab6a7820ca..2fab9ee7264 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -2293,7 +2293,7 @@ class Topic < ActiveRecord::Baseenddef limit_first_day_topics_per_day- apply_per_day_rate_limit_for("first-day-topics", :max_topics_in_first_day)+ RateLimiter.new(user, "first-day-topics-per-day", user.first_day_topics_limit, 1.day.to_i)enddef apply_per_day_rate_limit_for(key, method_name)diff --git a/app/models/user.rb b/app/models/user.rbindex 2c0c2c923de..b9681050ce1 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -1047,10 +1047,26 @@ class User < ActiveRecord::Basedef new_user_posting_on_first_day?return false if staff?return false if trust_level >= TrustLevel[2]- return false if first_post_created_at.present? && first_post_created_at <= 24.hours.ago+ return false if created_at <= 24.hours.agotrueend+ def first_day_topics_limit+ if trust_level == TrustLevel[1]+ SiteSetting.tl1_max_topics_in_first_day+ else+ SiteSetting.max_topics_in_first_day+ end+ end++ def first_day_replies_limit+ if trust_level == TrustLevel[1]+ SiteSetting.tl1_max_replies_in_first_day+ else+ SiteSetting.max_replies_in_first_day+ end+ end+def new_user?(created_at >= 24.hours.ago || trust_level == TrustLevel[0]) && trust_level < TrustLevel[2] &&!staff?diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 6f7813e24ef..e07934f54dc 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -2000,8 +2000,10 @@ en:cooldown_minutes_after_hiding_posts: "Number of minutes a user must wait before they can edit a post hidden via community flagging"- max_topics_in_first_day: "The maximum number of topics a user is allowed to create in the 24 hour period after creating their first post"- max_replies_in_first_day: "The maximum number of replies a user is allowed to create in the 24 hour period after creating their first post"+ max_topics_in_first_day: "The maximum number of topics a trust level 0 user is allowed to create in the 24 hour period after account creation"+ max_replies_in_first_day: "The maximum number of replies a trust level 0 user is allowed to create in the 24 hour period after account creation"+ tl1_max_topics_in_first_day: "The maximum number of topics a trust level 1 user is allowed to create in the 24 hour period after account creation"+ tl1_max_replies_in_first_day: "The maximum number of replies a trust level 1 user is allowed to create in the 24 hour period after account creation"tl2_additional_likes_per_day_multiplier: "Increase limit of likes per day for tl2 (member) by multiplying with this number"tl3_additional_likes_per_day_multiplier: "Increase limit of likes per day for tl3 (regular) by multiplying with this number"diff --git a/config/site_settings.yml b/config/site_settings.ymlindex 2a385a89a87..991d0b33a52 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -3256,6 +3256,8 @@ rate_limits:max_topic_invitations_per_minute: 5max_topics_in_first_day: 3max_replies_in_first_day: 10+ tl1_max_topics_in_first_day: 6+ tl1_max_replies_in_first_day: 30tl2_additional_likes_per_day_multiplier: 1.5tl3_additional_likes_per_day_multiplier: 2tl4_additional_likes_per_day_multiplier: 3diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rbindex 1a11d2d38ea..1cc7b8ac41c 100644--- a/spec/models/topic_spec.rb+++ b/spec/models/topic_spec.rb@@ -2980,17 +2980,20 @@ RSpec.describe Topic dobefore doSiteSetting.max_topics_in_first_day = 1SiteSetting.max_replies_in_first_day = 1+ SiteSetting.tl1_max_topics_in_first_day = 2+ SiteSetting.tl1_max_replies_in_first_day = 2SiteSetting.stubs(:client_settings_json).returns(SiteSetting.client_settings_json_uncached)RateLimiter.stubs(:rate_limit_create_topic).returns(100)RateLimiter.enableend- it "limits new users to max_topics_in_first_day and max_posts_in_first_day" do+ it "limits TL0 users to max_topics_in_first_day and max_replies_in_first_day" dostart = Time.now.tomorrow.beginning_of_dayfreeze_time(start)- user = Fabricate(:user, refresh_auto_groups: true)+ user =+ Fabricate(:user, created_at: start, trust_level: TrustLevel[0], refresh_auto_groups: true)topic_id = create_post(user: user).topic_idfreeze_time(start + 10.minutes)@@ -3005,26 +3008,52 @@ RSpec.describe Topic do)end- it "starts counting when they make their first post/topic" do+ it "limits TL1 users to TL1 first day limits" dostart = Time.now.tomorrow.beginning_of_dayfreeze_time(start)- user = Fabricate(:user, refresh_auto_groups: true)+ user =+ Fabricate(:user, created_at: start, trust_level: TrustLevel[1], refresh_auto_groups: true)+ topic_id = create_post(user: user).topic_id++ freeze_time(start + 10.minutes)+ create_post(user: user)++ freeze_time(start + 20.minutes)+ expect { create_post(user: user) }.to raise_error(RateLimiter::LimitExceeded)++ freeze_time(start + 30.minutes)+ create_post(user: user, topic_id: topic_id)++ freeze_time(start + 40.minutes)+ create_post(user: user, topic_id: topic_id)++ freeze_time(start + 50.minutes)+ expect { create_post(user: user, topic_id: topic_id) }.to raise_error(+ RateLimiter::LimitExceeded,+ )+ end++ it "starts counting when the account is created" do+ start = Time.now.tomorrow.beginning_of_day++ freeze_time(start)++ user =+ Fabricate(:user, created_at: start, trust_level: TrustLevel[0], refresh_auto_groups: true)freeze_time(start + 25.hours)topic_id = create_post(user: user).topic_idfreeze_time(start + 26.hours)- expect { create_post(user: user) }.to raise_error(RateLimiter::LimitExceeded)+ create_post(user: user)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,- )+ create_post(user: user, topic_id: topic_id)endenddiff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rbindex f3d458f48a9..12b71e706ab 100644--- a/spec/models/user_spec.rb+++ b/spec/models/user_spec.rb@@ -1782,19 +1782,36 @@ RSpec.describe User do)end- it "is true for a user who posted less than 24 hours ago but was created over 1 day ago" do- u = create_test_user(created_at: 28.hours.ago)- u.user_stat.update!(first_post_created_at: 1.hour.ago)- expect(u.new_user_posting_on_first_day?).to eq(true)+ it "is true for a TL1 user created less than 24 hours ago" do+ expect(create_test_user(trust_level: TrustLevel[1]).new_user_posting_on_first_day?).to eq(+ true,+ )end- it "is false if first post was more than 24 hours ago" do- u = create_test_user(created_at: 28.hours.ago)- u.user_stat.update!(first_post_created_at: 25.hours.ago)+ it "is false if account was created more than 24 hours ago" do+ u = create_test_user(created_at: 25.hours.ago)expect(u.new_user_posting_on_first_day?).to eq(false)endend+ describe "#first_day_topics_limit" do+ it "returns the TL1 limit for TL1 users" do+ SiteSetting.max_topics_in_first_day = 3+ SiteSetting.tl1_max_topics_in_first_day = 6++ expect(Fabricate(:user, trust_level: TrustLevel[1]).first_day_topics_limit).to eq(6)+ end+ end++ describe "#first_day_replies_limit" do+ it "returns the TL1 limit for TL1 users" do+ SiteSetting.max_replies_in_first_day = 10+ SiteSetting.tl1_max_replies_in_first_day = 30++ expect(Fabricate(:user, trust_level: TrustLevel[1]).first_day_replies_limit).to eq(30)+ end+ end+describe "posted too much in topic" dolet!(:user) { Fabricate(:user, trust_level: TrustLevel[0]) }let!(:topic) { Fabricate(:post).topic }