Skip to content

A platform for community discussion. Free, open, simple.

correct auto bump topic logic

Sam91266cdabb88
diff --git a/app/jobs/scheduled/periodical_updates.rb b/app/jobs/scheduled/periodical_updates.rb
index 5025f999711..73f480494ab 100644
--- a/app/jobs/scheduled/periodical_updates.rb
+++ b/app/jobs/scheduled/periodical_updates.rb
@@ -49,13 +49,7 @@ module Jobs
SiteSetting.min_new_topics_time = last_new_topic.created_at.to_i
end
- auto_bumps = CategoryCustomField.where(name: Category::NUM_AUTO_BUMP_DAILY).pluck(:id)
-
- if (auto_bumps.length > 0)
- auto_bumps.shuffle.each do |category_id|
- break if Category.find_by(id: category_id)&.auto_bump_topic!
- end
- end
+ Category.auto_bump_topic!
nil
end
diff --git a/app/models/category.rb b/app/models/category.rb
index e3fc9bc7ded..f8ebb396dca 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -385,6 +385,19 @@ class Category < ActiveRecord::Base
auto_bump_limiter.clear!
end
+ def self.auto_bump_topic!
+ bumped = false
+ auto_bumps = CategoryCustomField.where(name: Category::NUM_AUTO_BUMP_DAILY).pluck(:category_id)
+
+ if (auto_bumps.length > 0)
+ auto_bumps.shuffle.each do |category_id|
+ bumped = Category.find_by(id: category_id)&.auto_bump_topic!
+ break if bumped
+ end
+ end
+ bumped
+ end
+
# will automatically bump a single topic
# if number of automatically bumped topics is smaller than threshold
def auto_bump_topic!
diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb
index 54828be487a..18880170cfb 100644
--- a/spec/models/category_spec.rb
+++ b/spec/models/category_spec.rb
@@ -686,10 +686,6 @@ describe Category do
end
describe 'auto bump' do
- before do
- RateLimiter.enable
- end
-
after do
RateLimiter.disable
end
@@ -703,6 +699,9 @@ describe Category do
_post2 = create_post(category: category)
_post3 = create_post(category: category)
+ # no limits on post creation or category creation please
+ RateLimiter.enable
+
time = 1.month.from_now
freeze_time time
@@ -721,6 +720,12 @@ describe Category do
expect(category.auto_bump_topic!).to eq(false)
expect(Topic.where(bumped_at: time).count).to eq(2)
+ time = 1.month.from_now
+ freeze_time time
+
+ category.auto_bump_limiter.clear!
+ expect(Category.auto_bump_topic!).to eq(true)
+ expect(Topic.where(bumped_at: time).count).to eq(1)
end
end