Skip to content

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

FIX: Slow mode bypass via auto_track parameter (#37868)

Sam2f529a589917
diff --git a/lib/post_creator.rb b/lib/post_creator.rb
index 7925fcdfa90..253013d2b98 100644
--- a/lib/post_creator.rb
+++ b/lib/post_creator.rb
@@ -151,10 +151,11 @@ class PostCreator
end
if guardian.affected_by_slow_mode?(@topic)
- tu = TopicUser.find_by(user: @user, topic: @topic)
+ last_posted_at =
+ @topic.posts.where(user_id: @user.id).order(created_at: :desc).pick(:created_at)
- if tu&.last_posted_at
- threshold = tu.last_posted_at + @topic.slow_mode_seconds.seconds
+ if last_posted_at
+ threshold = last_posted_at + @topic.slow_mode_seconds.seconds
if DateTime.now < threshold
errors.add(:base, I18n.t(:slow_mode_enabled))
diff --git a/spec/lib/post_creator_spec.rb b/spec/lib/post_creator_spec.rb
index a6226db46dc..35a17eabb75 100644
--- a/spec/lib/post_creator_spec.rb
+++ b/spec/lib/post_creator_spec.rb
@@ -1007,7 +1007,7 @@ RSpec.describe PostCreator do
end
it "fails if the user recently posted in this topic" do
- TopicUser.create!(user: user, topic: topic, last_posted_at: 10.minutes.ago)
+ Fabricate(:post, topic: topic, user: user, created_at: 10.minutes.ago)
post = creator.create
@@ -1017,7 +1017,7 @@ RSpec.describe PostCreator do
end
it "creates the topic if the user last post is older than the slow mode interval" do
- TopicUser.create!(user: user, topic: topic, last_posted_at: 5.days.ago)
+ Fabricate(:post, topic: topic, user: user, created_at: 5.days.ago)
post = creator.create
@@ -1028,7 +1028,7 @@ RSpec.describe PostCreator do
it "creates the topic if the user is a staff member" do
post_creator =
PostCreator.new(admin, raw: "test reply", topic_id: topic.id, reply_to_post_number: 4)
- TopicUser.create!(user: admin, topic: topic, last_posted_at: 10.minutes.ago)
+ Fabricate(:post, topic: topic, user: admin, created_at: 10.minutes.ago)
post = post_creator.create
diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb
index baf4fdae8ed..4158bb2230b 100644
--- a/spec/requests/posts_controller_spec.rb
+++ b/spec/requests/posts_controller_spec.rb
@@ -1942,6 +1942,43 @@ RSpec.describe PostsController do
end
end
+ context "when topic is in slow mode" do
+ fab!(:slow_topic) { Fabricate(:topic, slow_mode_seconds: 86_400) }
+
+ it "enforces slow mode even when auto_track is false" do
+ post "/posts.json",
+ params: {
+ raw: "this is the first reply in slow mode",
+ topic_id: slow_topic.id,
+ auto_track: false,
+ }
+
+ expect(response.status).to eq(200)
+
+ post "/posts.json",
+ params: {
+ raw: "this is the second reply in slow mode bypassing",
+ topic_id: slow_topic.id,
+ auto_track: false,
+ }
+
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"]).to include(I18n.t(:slow_mode_enabled))
+ end
+
+ it "does not create a TopicUser record when auto_track is false" do
+ post "/posts.json",
+ params: {
+ raw: "this is a reply with auto_track false",
+ topic_id: slow_topic.id,
+ auto_track: false,
+ }
+
+ expect(response.status).to eq(200)
+ expect(TopicUser.find_by(user: user, topic: slow_topic)).to be_nil
+ end
+ end
+
context "when `enable_user_status` site setting is enabled" do
fab!(:user_to_mention, :user)