Skip to content

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

FIX: skip topic bump when date is in the past (#36784)

Régis Hanolfb910ca078f4
diff --git a/plugins/discourse-calendar/jobs/regular/discourse_post_event/bump_topic.rb b/plugins/discourse-calendar/jobs/regular/discourse_post_event/bump_topic.rb
index 4f0b36d32a0..d74981df2f2 100644
--- a/plugins/discourse-calendar/jobs/regular/discourse_post_event/bump_topic.rb
+++ b/plugins/discourse-calendar/jobs/regular/discourse_post_event/bump_topic.rb
@@ -6,12 +6,20 @@ module Jobs
def execute(args)
return unless topic = Topic.find_by(id: args[:topic_id].to_i)
+ return unless by_user = User.find_by(id: topic.user_id)
return if args[:date].blank?
- event_user = User.find_by(id: topic.user_id)
- timer = TopicTimer.find_by(timerable_id: args[:topic_id].to_i)
+ date = args[:date]
- topic.set_or_create_timer(TopicTimer.types[:bump], args[:date], by_user: event_user)
+ begin
+ date = Time.parse(date) if date.is_a?(String)
+ rescue ArgumentError
+ return
+ end
+
+ return if date <= Time.now.utc
+
+ topic.set_or_create_timer(TopicTimer.types[:bump], args[:date], by_user:)
end
end
end
diff --git a/plugins/discourse-calendar/spec/jobs/regular/discourse_post_event/bump_topic_spec.rb b/plugins/discourse-calendar/spec/jobs/regular/discourse_post_event/bump_topic_spec.rb
index a3959e7ff5c..837a378767e 100644
--- a/plugins/discourse-calendar/spec/jobs/regular/discourse_post_event/bump_topic_spec.rb
+++ b/plugins/discourse-calendar/spec/jobs/regular/discourse_post_event/bump_topic_spec.rb
@@ -23,6 +23,13 @@ describe Jobs::DiscoursePostEventBumpTopic do
expect(timer.status_type).to eq(TopicTimer.types[:bump])
expect(timer.execute_at).to eq_time(Time.zone.local(2019, 12, 10, 5, 0))
end
+
+ it "does not create a timer if the date is in the past" do
+ freeze_time
+ Jobs::DiscoursePostEventBumpTopic.new.execute(topic_id: topic_1.id, date: "2019-10-10 5:00")
+
+ expect(TopicTimer.find_by(topic: topic_1)).to be_nil
+ end
end
context "when the topic_id param is missing" do