forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo_not_disturb_controller.rb
58 lines (48 loc) · 1.31 KB
/
do_not_disturb_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# frozen_string_literal: true
class DoNotDisturbController < ApplicationController
requires_login
def create
raise Discourse::InvalidParameters.new(:duration) if params[:duration].blank?
duration_minutes =
(
begin
Integer(params[:duration])
rescue StandardError
false
end
)
ends_at =
(
if duration_minutes
ends_at_from_minutes(duration_minutes)
else
ends_at_from_string(params[:duration])
end
)
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)
current_user.shelved_notifications.each(&:process)
current_user.shelved_notifications.destroy_all
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