FEATURE: Topic slow mode. (#10904)

Adds a new slow mode for topics that are heating up. Users will have to wait for a period of time before being able to post again.

We store this interval inside the topics table and track the last time a user posted using the last_posted_at datetime in the TopicUser relation.
This commit is contained in:
Roman Rizzi
2020-10-16 16:24:38 -03:00
committed by GitHub
parent 4669e60ce5
commit 21c53ed249
27 changed files with 460 additions and 6 deletions

View File

@ -159,6 +159,19 @@ class PostCreator
return false
end
if @topic&.slow_mode_seconds.to_i > 0
tu = TopicUser.find_by(user: @user, topic: @topic)
if tu&.last_posted_at
threshold = tu.last_posted_at + @topic.slow_mode_seconds.seconds
if DateTime.now < threshold
errors.add(:base, I18n.t(:slow_mode_enabled))
return false
end
end
end
unless @topic.present? && (@opts[:skip_guardian] || guardian.can_create?(Post, @topic))
errors.add(:base, I18n.t(:topic_not_found))
return false
@ -622,7 +635,8 @@ class PostCreator
@topic.id,
posted: true,
last_read_post_number: @post.post_number,
highest_seen_post_number: @post.post_number)
highest_seen_post_number: @post.post_number,
last_posted_at: Time.zone.now)
# assume it took us 5 seconds of reading time to make a post
PostTiming.record_timing(topic_id: @post.topic_id,

View File

@ -130,6 +130,7 @@ module SvgSprite
"heading",
"heart",
"home",
"hourglass-end",
"id-card",
"info-circle",
"italic",