Class: Slk::Api::CustomStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/slk/api/custom_status.rb

Overview

Wrapper for Slack's internal users.customStatus.* endpoints, which back the "Scheduled" section of the status picker.

Two undocumented quirks these methods paper over:

- Only form-encoded bodies are accepted. A JSON body is ignored and the
call fails with invalid_arguments naming every field as missing.
- list omits scheduled_statuses entirely unless
statuses_count_per_section is passed.

Constant Summary collapse

DEFAULT_SECTION_COUNT =
20

Instance Method Summary collapse

Constructor Details

#initialize(api_client, workspace) ⇒ CustomStatus

Returns a new instance of CustomStatus.



16
17
18
19
# File 'lib/slk/api/custom_status.rb', line 16

def initialize(api_client, workspace)
  @api = api_client
  @workspace = workspace
end

Instance Method Details

#delete_scheduled(custom_status_id) ⇒ Array(Symbol, String, nil)

Slack answers a delete with ok: true whether or not anything changed — including for an id that never existed — so the response alone is not evidence. Re-reading the list at least catches an accepted delete that did not apply. It cannot distinguish "cancelled" from "was never there": both leave the id absent. unschedule covers that case ahead of time by finding the workspace that owns the id.

Returns:

  • (Array(Symbol, String, nil))

    [:cancelled, nil], or [:unconfirmed, reason] when the delete was accepted but the following read failed. Those are different things: only the second read failed, and reporting it as a failed cancel would send the user back to re-cancel something already gone.

Raises:

  • (ApiError)

    only when the status is demonstrably still there



70
71
72
73
74
# File 'lib/slk/api/custom_status.rb', line 70

def delete_scheduled(custom_status_id)
  @api.post_form(@workspace, 'users.customStatus.deleteScheduled',
                 { custom_status_id: custom_status_id })
  confirm_deleted(custom_status_id)
end

#list(count_per_section: DEFAULT_SECTION_COUNT) ⇒ Hash

Returns raw response; 'statuses' (recent) and 'scheduled_statuses' (pending) are each absent when Slack omits the section.

Returns:

  • (Hash)

    raw response; 'statuses' (recent) and 'scheduled_statuses' (pending) are each absent when Slack omits the section



23
24
25
26
# File 'lib/slk/api/custom_status.rb', line 23

def list(count_per_section: DEFAULT_SECTION_COUNT)
  @api.post_form(@workspace, 'users.customStatus.list',
                 { statuses_count_per_section: count_per_section.to_s })
end

#schedule(text:, emoji:, date_scheduled:, date_expire: nil, dnd: false) ⇒ Models::ScheduledStatus

Parameters:

  • date_scheduled (Integer)

    Unix timestamp the status turns on

  • date_expire (Integer, nil) (defaults to: nil)

    Unix timestamp it clears

  • dnd (Boolean) (defaults to: false)

    also pause notifications while active

Returns:



48
49
50
51
52
53
54
55
# File 'lib/slk/api/custom_status.rb', line 48

def schedule(text:, emoji:, date_scheduled:, date_expire: nil, dnd: false)
  params = { text: text, emoji: emoji, date_scheduled: date_scheduled.to_i.to_s }
  params[:date_expire] = date_expire.to_i.to_s if date_expire
  params[:is_dnd] = 'true' if dnd

  response = @api.post_form(@workspace, 'users.customStatus.schedule', params)
  Models::ScheduledStatus.from_api(confirmed_schedule(response))
end

#scheduled(count_per_section: DEFAULT_SECTION_COUNT) ⇒ Array<Models::ScheduledStatus>

Returns pending statuses only.

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/slk/api/custom_status.rb', line 29

def scheduled(count_per_section: DEFAULT_SECTION_COUNT)
  response = list(count_per_section: count_per_section)
  section = response['scheduled_statuses']
  # An absent section is a protocol change, not an empty list. Reporting
  # it as "none scheduled" would invite the user to re-create statuses
  # that still exist.
  unless section.is_a?(Array)
    raise ApiError.new('Slack returned no scheduled_statuses section; this internal endpoint may have changed. ' \
                       'Check the Slack status picker before re-scheduling.',
                       code: :missing_scheduled_section)
  end

  section.map { |item| Models::ScheduledStatus.from_api(item) }
end