diff --git a/app/models/category.rb b/app/models/category.rb index 6c3e279b07f..610406bd0ea 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -378,16 +378,20 @@ class Category < ActiveRecord::Base end def auto_bump_limiter - RateLimiter.new(nil, "auto_bump_limit_#{self.id}", num_auto_bump_daily.to_i, 86400) + return nil if num_auto_bump_daily.to_i == 0 + RateLimiter.new(nil, "auto_bump_limit_#{self.id}", 1, 86400 / num_auto_bump_daily.to_i) end def clear_auto_bump_cache! - auto_bump_limiter.clear! + 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) + auto_bumps = CategoryCustomField + .where(name: Category::NUM_AUTO_BUMP_DAILY) + .where('value::int > 0') + .pluck(:category_id) if (auto_bumps.length > 0) auto_bumps.shuffle.each do |category_id| @@ -406,7 +410,7 @@ class Category < ActiveRecord::Base limiter = auto_bump_limiter return false if !limiter.can_perform? - id = Topic + topic = Topic .visible .listable_topics .where(category_id: self.id) @@ -415,10 +419,10 @@ class Category < ActiveRecord::Base .where('pinned_at IS NULL AND NOT closed AND NOT archived') .order('bumped_at ASC') .limit(1) - .pluck(:id).first + .first - if id - Topic.where(id: id).update_all(bumped_at: Time.zone.now) + if topic + topic.add_small_action(Discourse.system_user, "autobumped", nil, bump: true) limiter.performed! true else diff --git a/app/models/topic.rb b/app/models/topic.rb index 4b2256aa7dd..975825a0350 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -698,10 +698,16 @@ class Topic < ActiveRecord::Base true end - def add_small_action(user, action_code, who = nil) + def add_small_action(user, action_code, who = nil, opts = {}) custom_fields = {} custom_fields["action_code_who"] = who if who.present? - add_moderator_post(user, nil, post_type: Post.types[:small_action], action_code: action_code, custom_fields: custom_fields) + opts = opts.merge( + post_type: Post.types[:small_action], + action_code: action_code, + custom_fields: custom_fields + ) + + add_moderator_post(user, nil, opts) end def add_moderator_post(user, text, opts = nil) diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index e14586abc51..37121640b2c 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -152,6 +152,7 @@ en: user_left: "%{who} removed themselves from this message %{when}" removed_user: "removed %{who} %{when}" removed_group: "removed %{who} %{when}" + autobumped: "automatically bumped topic" autoclosed: enabled: 'closed %{when}' disabled: 'opened %{when}' diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index 18880170cfb..d116df07a63 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -695,8 +695,11 @@ describe Category do category = Fabricate(:category) category.clear_auto_bump_cache! - _post1 = create_post(category: category) + freeze_time 1.second.from_now + post1 = create_post(category: category) + freeze_time 1.second.from_now _post2 = create_post(category: category) + freeze_time 1.second.from_now _post3 = create_post(category: category) # no limits on post creation or category creation please @@ -713,12 +716,17 @@ describe Category do expect(category.auto_bump_topic!).to eq(true) expect(Topic.where(bumped_at: time).count).to eq(1) + # our extra bump message + expect(post1.topic.reload.posts_count).to eq(2) + + time = time + 13.hours + freeze_time time expect(category.auto_bump_topic!).to eq(true) - expect(Topic.where(bumped_at: time).count).to eq(2) + expect(Topic.where(bumped_at: time).count).to eq(1) expect(category.auto_bump_topic!).to eq(false) - expect(Topic.where(bumped_at: time).count).to eq(2) + expect(Topic.where(bumped_at: time).count).to eq(1) time = 1.month.from_now freeze_time time