Skip to content
Permalink
Browse files

FEATURE: adjust autobump system

- We spread out bumping through the day, if you are bumping
 4 topics then a topic will be bumped every 6 hours

- We add a small, bumping action at the bottom of the post to
 denote a topic got bumped
  • Loading branch information
SamSaffron committed Jul 18, 2018
1 parent f146f94 commit 02628883d289168882b8a17231d21ae9e6ffc8cb
Showing with 31 additions and 12 deletions.
  1. +11 −7 app/models/category.rb
  2. +8 −2 app/models/topic.rb
  3. +1 −0 config/locales/client.en.yml
  4. +11 −3 spec/models/category_spec.rb
@@ -378,16 +378,20 @@ def num_auto_bump_daily=(v)
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 @@ def auto_bump_topic!
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 @@ def auto_bump_topic!
.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
@@ -698,10 +698,16 @@ def changed_to_category(new_category)
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)
@@ -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}'
@@ -695,8 +695,11 @@
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 @@

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

1 comment on commit 0262888

@discoursebot

This comment has been minimized.

Copy link

discoursebot commented on 0262888 Jul 18, 2018

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/automatically-bumping-old-topics-on-a-category/92518/11

Please sign in to comment.
You can’t perform that action at this time.