From 5aa1272f05fc0de0b08577707d3370d10fafdd51 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 26 Mar 2015 16:48:36 +1100 Subject: [PATCH] clean up first day user definition and object model --- app/models/post.rb | 2 +- app/models/topic.rb | 2 +- app/models/user.rb | 10 ++++++---- spec/models/user_spec.rb | 20 ++++++++------------ 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index b4bf023d566..9e07e94f935 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -88,7 +88,7 @@ class Post < ActiveRecord::Base end def limit_posts_per_day - if user.created_at > 1.day.ago && post_number > 1 + if user.first_day_user? && post_number > 1 RateLimiter.new(user, "first-day-replies-per-day", SiteSetting.max_replies_in_first_day, 1.day.to_i) end end diff --git a/app/models/topic.rb b/app/models/topic.rb index 03868e13b82..eada4a11ff4 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -250,7 +250,7 @@ class Topic < ActiveRecord::Base # Additional rate limits on topics: per day and private messages per day def limit_topics_per_day apply_per_day_rate_limit_for("topics", :max_topics_per_day) - limit_first_day_topics_per_day if user.added_a_day_ago? + limit_first_day_topics_per_day if user.first_day_user? end def limit_private_messages_per_day diff --git a/app/models/user.rb b/app/models/user.rb index f66c6e02c42..d93301150a0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -311,6 +311,12 @@ class User < ActiveRecord::Base self.password_hash == hash_password(password, salt) end + def first_day_user? + !staff? && + trust_level < TrustLevel[2] && + created_at >= 24.hours.ago + end + def new_user? (created_at >= 24.hours.ago || trust_level == TrustLevel[0]) && trust_level < TrustLevel[2] && @@ -562,10 +568,6 @@ class User < ActiveRecord::Base uploaded_avatar.present? end - def added_a_day_ago? - created_at > 1.day.ago - end - def generate_api_key(created_by) if api_key.present? api_key.regenerate!(created_by) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index df3a47e6711..5c58b5a7144 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -722,21 +722,17 @@ describe User do end - describe "#added_a_day_ago?" do - context "when user is more than a day old" do - subject(:user) { Fabricate(:user, created_at: Date.today - 2.days) } + describe "#first_day_user?" do - it "returns false" do - expect(user).to_not be_added_a_day_ago - end + def test_user?(opts={}) + Fabricate.build(:user, {created_at: Time.now}.merge(opts)).first_day_user? end - context "is less than a day old" do - subject(:user) { Fabricate(:user) } - - it "returns true" do - expect(user).to be_added_a_day_ago - end + it "works" do + expect(test_user?).to eq(true) + expect(test_user?(moderator: true)).to eq(false) + expect(test_user?(trust_level: TrustLevel[2])).to eq(false) + expect(test_user?(created_at: 2.days.ago)).to eq(false) end end