Class: Slk::Models::DndState

Inherits:
Data
  • Object
show all
Defined in:
lib/slk/models/dnd_state.rb

Overview

Current Do Not Disturb state for one workspace, from dnd.info.

Slack reports two independent things through that one endpoint: a manual snooze ("pause notifications for 2 hours") and the configured DND schedule ("quiet from 8pm to 8am"). From the outside they are the same thing — messages do not notify — so active? covers both and source says which one is responsible.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#next_endObject (readonly)

Returns the value of attribute next_end

Returns:

  • (Object)

    the current value of next_end



14
15
16
# File 'lib/slk/models/dnd_state.rb', line 14

def next_end
  @next_end
end

#next_startObject (readonly)

Returns the value of attribute next_start

Returns:

  • (Object)

    the current value of next_start



14
15
16
# File 'lib/slk/models/dnd_state.rb', line 14

def next_start
  @next_start
end

#scheduledObject (readonly)

Returns the value of attribute scheduled

Returns:

  • (Object)

    the current value of scheduled



14
15
16
# File 'lib/slk/models/dnd_state.rb', line 14

def scheduled
  @scheduled
end

#snooze_endtimeObject (readonly)

Returns the value of attribute snooze_endtime

Returns:

  • (Object)

    the current value of snooze_endtime



14
15
16
# File 'lib/slk/models/dnd_state.rb', line 14

def snooze_endtime
  @snooze_endtime
end

#snoozingObject (readonly)

Returns the value of attribute snoozing

Returns:

  • (Object)

    the current value of snoozing



14
15
16
# File 'lib/slk/models/dnd_state.rb', line 14

def snoozing
  @snoozing
end

Class Method Details

.from_api(data) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/slk/models/dnd_state.rb', line 15

def self.from_api(data)
  data = {} unless data.is_a?(Hash)

  new(
    snoozing: data['snooze_enabled'] == true,
    snooze_endtime: data['snooze_endtime'].to_i,
    scheduled: data['dnd_enabled'] == true,
    next_start: data['next_dnd_start_ts'].to_i,
    next_end: data['next_dnd_end_ts'].to_i
  )
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


27
# File 'lib/slk/models/dnd_state.rb', line 27

def active? = snoozing || in_scheduled_hours?

#in_scheduled_hours?Boolean

next_dnd_* names the next window while DND hours are off and the current one while they are running, so "now falls inside it" is the only reading that means notifications are being held right now.

Slack reports 1 for both schedule timestamps when no schedule is configured, so a bare positive? would read that as a window that opened in 1970 and never closed.

Returns:

  • (Boolean)


36
37
38
39
40
41
# File 'lib/slk/models/dnd_state.rb', line 36

def in_scheduled_hours?
  return false unless scheduled && next_start > 1 && next_end > next_start

  now = Time.now.to_i
  next_start <= now && now < next_end
end

#sourceObject



43
44
45
46
47
48
49
# File 'lib/slk/models/dnd_state.rb', line 43

def source
  return :both if snoozing && in_scheduled_hours?
  return :snooze if snoozing
  return :schedule if in_scheduled_hours?

  nil
end

#to_sObject

Empty when notifications are flowing: there is nothing to say, and a "[dnd off]" on every line would bury the workspace where it is on.



71
72
73
74
75
76
77
78
# File 'lib/slk/models/dnd_state.rb', line 71

def to_s
  return '' unless active?

  label = until_label
  # A snooze with no end time is a state Slack can report; saying "[dnd]"
  # is honest about not knowing when it lifts.
  label ? "[dnd until #{label}]" : '[dnd]'
end

#until_labelObject

Same-day times need no date; an overnight schedule ending tomorrow morning would otherwise read as "until 8:00am" of a day already gone.



64
65
66
67
# File 'lib/slk/models/dnd_state.rb', line 64

def until_label
  finish = until_time or return nil
  finish.to_date == Date.today ? finish.strftime('%-l:%M%P') : finish.strftime('%a %-l:%M%P')
end

#until_timeObject

The later of the two when both apply: notifications stay off until every reason for holding them has expired, not the first.



53
54
55
56
57
58
59
60
# File 'lib/slk/models/dnd_state.rb', line 53

def until_time
  finishes = []
  finishes << snooze_endtime if snoozing && snooze_endtime.positive?
  finishes << next_end if in_scheduled_hours?

  finish = finishes.max
  finish ? Time.at(finish) : nil
end