Raise error when timestamp is invalid when creating topic timers.

https://meta.discourse.org/t/topic-timer-doesnt-work-for-fa-ir-locale/83702
This commit is contained in:
Guo Xiang Tan 2018-03-26 11:32:52 +08:00
parent 252e58dfe9
commit 3d18cd1d9d
2 changed files with 20 additions and 10 deletions

View File

@ -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

View File

@ -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)