diff --git a/app/models/topic.rb b/app/models/topic.rb index e397aba3975..3e79e2c3399 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -182,15 +182,13 @@ class Topic < ActiveRecord::Base # Additional rate limits on topics: per day and private messages per day def limit_topics_per_day - RateLimiter.new(user, "topics-per-day:#{Date.today.to_s}", SiteSetting.max_topics_per_day, 1.day.to_i) - if user.created_at > 1.day.ago - RateLimiter.new(user, "first-day-topics-per-day:#{Date.today.to_s}", SiteSetting.max_topics_in_first_day, 1.day.to_i) - end + apply_per_day_rate_limit_for("topics", :max_topics_per_day) + limit_first_day_topics_per_day if user.added_a_day_ago? end def limit_private_messages_per_day return unless private_message? - RateLimiter.new(user, "pms-per-day:#{Date.today.to_s}", SiteSetting.max_private_messages_per_day, 1.day.to_i) + apply_per_day_rate_limit_for("pms", :max_private_messages_per_day) end def fancy_title @@ -325,9 +323,7 @@ class Topic < ActiveRecord::Base end def changed_to_category(cat) - - return true if cat.blank? - return true if Category.where(topic_id: id).first.present? + return true if cat.blank? || Category.where(topic_id: id).first.present? Topic.transaction do old_category = category @@ -615,11 +611,19 @@ class Topic < ActiveRecord::Base private - def update_category_topic_count_by(num) - if category_id.present? - Category.where(['id = ?', category_id]).update_all("topic_count = topic_count " + (num > 0 ? '+' : '') + "#{num}") - end + def update_category_topic_count_by(num) + if category_id.present? + Category.where(['id = ?', category_id]).update_all("topic_count = topic_count " + (num > 0 ? '+' : '') + "#{num}") end + end + + def limit_first_day_topics_per_day + apply_per_day_rate_limit_for("first-day-topics", :max_topics_in_first_day) + end + + def apply_per_day_rate_limit_for(key, method_name) + RateLimiter.new(user, "#{key}-per-day:#{Date.today.to_s}", SiteSetting.send(method_name), 1.day.to_i) + end end diff --git a/app/models/user.rb b/app/models/user.rb index 5a32548dfb7..3576bebaa9c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -467,6 +467,10 @@ class User < ActiveRecord::Base uploaded_avatar.present? end + def added_a_day_ago? + created_at > 1.day.ago + end + protected def cook diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index c9b751f87ef..253d51b4948 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -822,4 +822,23 @@ describe User do end end 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) } + + it "returns false" do + expect(user).to_not be_added_a_day_ago + end + 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 + end + + end end