| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/url" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | type SnoozeDebug struct { |
| 11 | SnoozeEndDate string `json:"snooze_end_date"` |
| 12 | } |
| 13 | |
| 14 | type SnoozeInfo struct { |
| 15 | SnoozeEnabled bool `json:"snooze_enabled,omitempty"` |
| 16 | SnoozeEndTime int `json:"snooze_endtime,omitempty"` |
| 17 | SnoozeRemaining int `json:"snooze_remaining,omitempty"` |
| 18 | SnoozeDebug SnoozeDebug `json:"snooze_debug,omitempty"` |
| 19 | } |
| 20 | |
| 21 | type DNDStatus struct { |
| 22 | Enabled bool `json:"dnd_enabled"` |
| 23 | NextStartTimestamp int `json:"next_dnd_start_ts"` |
| 24 | NextEndTimestamp int `json:"next_dnd_end_ts"` |
| 25 | SnoozeInfo |
| 26 | } |
| 27 | |
| 28 | type dndResponseFull struct { |
| 29 | DNDStatus |
| 30 | SlackResponse |
| 31 | } |
| 32 | |
| 33 | type dndTeamInfoResponse struct { |
| 34 | Users map[string]DNDStatus `json:"users"` |
| 35 | SlackResponse |
| 36 | } |
| 37 | |
| 38 | func (api *Client) dndRequest(ctx context.Context, path string, values url.Values) (*dndResponseFull, error) { |
| 39 | response := &dndResponseFull{} |
| 40 | err := api.postMethod(ctx, path, values, response) |
| 41 | if err != nil { |
| 42 | return nil, err |
| 43 | } |
| 44 | |
| 45 | return response, response.Err() |
| 46 | } |
| 47 | |
| 48 | // EndDND ends the user's scheduled Do Not Disturb session |
| 49 | func (api *Client) EndDND() error { |
| 50 | return api.EndDNDContext(context.Background()) |
| 51 | } |
| 52 | |
| 53 | // EndDNDContext ends the user's scheduled Do Not Disturb session with a custom context |
| 54 | func (api *Client) EndDNDContext(ctx context.Context) error { |
| 55 | values := url.Values{ |
| 56 | "token": {api.token}, |
| 57 | } |
| 58 | |
| 59 | response := &SlackResponse{} |
| 60 | |
| 61 | if err := api.postMethod(ctx, "dnd.endDnd", values, response); err != nil { |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | return response.Err() |
| 66 | } |
| 67 | |
| 68 | // EndSnooze ends the current user's snooze mode |
| 69 | func (api *Client) EndSnooze() (*DNDStatus, error) { |
| 70 | return api.EndSnoozeContext(context.Background()) |
| 71 | } |
| 72 | |
| 73 | // EndSnoozeContext ends the current user's snooze mode with a custom context |
| 74 | func (api *Client) EndSnoozeContext(ctx context.Context) (*DNDStatus, error) { |
| 75 | values := url.Values{ |
| 76 | "token": {api.token}, |
| 77 | } |
| 78 | |
| 79 | response, err := api.dndRequest(ctx, "dnd.endSnooze", values) |
| 80 | if err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | return &response.DNDStatus, nil |
| 84 | } |
| 85 | |
| 86 | // GetDNDInfo provides information about a user's current Do Not Disturb settings. |
| 87 | func (api *Client) GetDNDInfo(user *string) (*DNDStatus, error) { |
| 88 | return api.GetDNDInfoContext(context.Background(), user) |
| 89 | } |
| 90 | |
| 91 | // GetDNDInfoContext provides information about a user's current Do Not Disturb settings with a custom context. |
| 92 | func (api *Client) GetDNDInfoContext(ctx context.Context, user *string) (*DNDStatus, error) { |
| 93 | values := url.Values{ |
| 94 | "token": {api.token}, |
| 95 | } |
| 96 | if user != nil { |
| 97 | values.Set("user", *user) |
| 98 | } |
| 99 | |
| 100 | response, err := api.dndRequest(ctx, "dnd.info", values) |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | return &response.DNDStatus, nil |
| 105 | } |
| 106 | |
| 107 | // GetDNDTeamInfo provides information about a user's current Do Not Disturb settings. |
| 108 | func (api *Client) GetDNDTeamInfo(users []string) (map[string]DNDStatus, error) { |
| 109 | return api.GetDNDTeamInfoContext(context.Background(), users) |
| 110 | } |
| 111 | |
| 112 | // GetDNDTeamInfoContext provides information about a user's current Do Not Disturb settings with a custom context. |
| 113 | func (api *Client) GetDNDTeamInfoContext(ctx context.Context, users []string) (map[string]DNDStatus, error) { |
| 114 | values := url.Values{ |
| 115 | "token": {api.token}, |
| 116 | "users": {strings.Join(users, ",")}, |
| 117 | } |
| 118 | response := &dndTeamInfoResponse{} |
| 119 | |
| 120 | if err := api.postMethod(ctx, "dnd.teamInfo", values, response); err != nil { |
| 121 | return nil, err |
| 122 | } |
| 123 | |
| 124 | if response.Err() != nil { |
| 125 | return nil, response.Err() |
| 126 | } |
| 127 | |
| 128 | return response.Users, nil |
| 129 | } |
| 130 | |
| 131 | // SetSnooze adjusts the snooze duration for a user's Do Not Disturb |
| 132 | // settings. If a snooze session is not already active for the user, invoking |
| 133 | // this method will begin one for the specified duration. |
| 134 | func (api *Client) SetSnooze(minutes int) (*DNDStatus, error) { |
| 135 | return api.SetSnoozeContext(context.Background(), minutes) |
| 136 | } |
| 137 | |
| 138 | // SetSnoozeContext adjusts the snooze duration for a user's Do Not Disturb settings with a custom context. |
| 139 | // For more information see the SetSnooze docs |
| 140 | func (api *Client) SetSnoozeContext(ctx context.Context, minutes int) (*DNDStatus, error) { |
| 141 | values := url.Values{ |
| 142 | "token": {api.token}, |
| 143 | "num_minutes": {strconv.Itoa(minutes)}, |
| 144 | } |
| 145 | |
| 146 | response, err := api.dndRequest(ctx, "dnd.setSnooze", values) |
| 147 | if err != nil { |
| 148 | return nil, err |
| 149 | } |
| 150 | return &response.DNDStatus, nil |
| 151 | } |