Skip to content

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

Raise error when timestamp is invalid when creating topic timers.

Guo Xiang Tan3d18cd1d9da2
diff --git a/app/models/topic.rb b/app/models/topic.rb
index 55f016c4b44..a9169ad515a 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -1101,19 +1101,17 @@ SQL
end
else
utc = Time.find_zone("UTC")
- is_timestamp = time.is_a?(String)
- now = utc.now
+ is_float = (Float(time) rescue nil)
- if is_timestamp && time.include?("-") && timestamp = utc.parse(time)
+ if is_float
+ num_hours = time.to_f
+ topic_timer.execute_at = num_hours.hours.from_now if num_hours > 0
+ else
+ timestamp = utc.parse(time)
+ raise Discourse::InvalidParameters unless timestamp
# a timestamp in client's time zone, like "2015-5-27 12:00"
topic_timer.execute_at = timestamp
- topic_timer.errors.add(:execute_at, :invalid) if timestamp < now
- else
- num_hours = time.to_f
-
- if num_hours > 0
- topic_timer.execute_at = num_hours.hours.from_now
- end
+ topic_timer.errors.add(:execute_at, :invalid) if timestamp < utc.now
end
end
diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb
index a1e60727b0b..a88441bb755 100644
--- a/spec/models/topic_spec.rb
+++ b/spec/models/topic_spec.rb
@@ -1392,6 +1392,18 @@ describe Topic do
expect(topic.topic_timers.first.errors[:execute_at]).to be_present
end
+ it "sets a validation error when give a timestamp of an invalid format" do
+ freeze_time now
+
+ expect do
+ topic.set_or_create_timer(
+ TopicTimer.types[:close],
+ '۲۰۱۸-۰۳-۲۶ ۱۸:۰۰+۰۸:۰۰',
+ by_user: admin
+ )
+ end.to raise_error(Discourse::InvalidParameters)
+ end
+
it "can take a timestamp with timezone" do
freeze_time now
topic.set_or_create_timer(TopicTimer.types[:close], '2013-11-25T01:35:00-08:00', by_user: admin)