2020-12-18 10:03:51 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class DoNotDisturbController < ApplicationController
|
|
|
|
requires_login
|
|
|
|
|
|
|
|
def create
|
|
|
|
raise Discourse::InvalidParameters.new(:duration) if params[:duration].blank?
|
|
|
|
|
|
|
|
duration_minutes =
|
|
|
|
(
|
2023-01-09 07:20:10 -05:00
|
|
|
begin
|
2020-12-18 10:03:51 -05:00
|
|
|
Integer(params[:duration])
|
|
|
|
rescue StandardError
|
|
|
|
false
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2020-12-18 10:03:51 -05:00
|
|
|
)
|
2023-01-09 07:20:10 -05:00
|
|
|
|
2020-12-18 10:03:51 -05:00
|
|
|
ends_at =
|
2023-01-09 07:20:10 -05:00
|
|
|
(
|
2020-12-18 10:03:51 -05:00
|
|
|
if duration_minutes
|
|
|
|
ends_at_from_minutes(duration_minutes)
|
2023-01-09 07:20:10 -05:00
|
|
|
else
|
2020-12-18 10:03:51 -05:00
|
|
|
ends_at_from_string(params[:duration])
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
|
|
|
)
|
2020-12-18 10:03:51 -05:00
|
|
|
|
|
|
|
new_timing = current_user.do_not_disturb_timings.new(starts_at: Time.zone.now, ends_at: ends_at)
|
|
|
|
|
|
|
|
if new_timing.save
|
|
|
|
current_user.publish_do_not_disturb(ends_at: ends_at)
|
|
|
|
render json: { ends_at: ends_at }
|
|
|
|
else
|
|
|
|
render_json_error(new_timing)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
current_user.active_do_not_disturb_timings.destroy_all
|
|
|
|
current_user.publish_do_not_disturb(ends_at: nil)
|
2021-01-27 11:29:24 -05:00
|
|
|
current_user.shelved_notifications.each(&:process)
|
|
|
|
current_user.shelved_notifications.destroy_all
|
2020-12-18 10:03:51 -05:00
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def ends_at_from_minutes(duration)
|
|
|
|
duration.minutes.from_now
|
|
|
|
end
|
|
|
|
|
|
|
|
def ends_at_from_string(string)
|
|
|
|
if string == "tomorrow"
|
|
|
|
Time.now.end_of_day.utc
|
|
|
|
else
|
|
|
|
raise Discourse::InvalidParameters.new(:duration)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|