matterbridge/vendor/github.com/slack-go/slack/dnd.go

161 lines
4.7 KiB
Go
Raw Normal View History

2016-09-05 07:34:37 -07:00
package slack
import (
2017-07-16 05:29:46 -07:00
"context"
2016-09-05 07:34:37 -07:00
"net/url"
"strconv"
"strings"
)
type SnoozeDebug struct {
SnoozeEndDate string `json:"snooze_end_date"`
}
type SnoozeInfo struct {
SnoozeEnabled bool `json:"snooze_enabled,omitempty"`
SnoozeEndTime int `json:"snooze_endtime,omitempty"`
SnoozeRemaining int `json:"snooze_remaining,omitempty"`
SnoozeDebug SnoozeDebug `json:"snooze_debug,omitempty"`
}
type DNDStatus struct {
Enabled bool `json:"dnd_enabled"`
NextStartTimestamp int `json:"next_dnd_start_ts"`
NextEndTimestamp int `json:"next_dnd_end_ts"`
SnoozeInfo
}
type dndResponseFull struct {
DNDStatus
SlackResponse
}
type dndTeamInfoResponse struct {
Users map[string]DNDStatus `json:"users"`
SlackResponse
}
2019-09-07 13:46:58 -07:00
func (api *Client) dndRequest(ctx context.Context, path string, values url.Values) (*dndResponseFull, error) {
2016-09-05 07:34:37 -07:00
response := &dndResponseFull{}
2019-09-07 13:46:58 -07:00
err := api.postMethod(ctx, path, values, response)
2016-09-05 07:34:37 -07:00
if err != nil {
return nil, err
}
2019-09-07 13:46:58 -07:00
return response, response.Err()
2016-09-05 07:34:37 -07:00
}
// EndDND ends the user's scheduled Do Not Disturb session.
// For more information see the EndDNDContext documentation.
2016-09-05 07:34:37 -07:00
func (api *Client) EndDND() error {
2017-07-16 05:29:46 -07:00
return api.EndDNDContext(context.Background())
}
// EndDNDContext ends the user's scheduled Do Not Disturb session with a custom context.
// Slack API docs: https://api.slack.com/methods/dnd.endDnd
2017-07-16 05:29:46 -07:00
func (api *Client) EndDNDContext(ctx context.Context) error {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2016-09-05 07:34:37 -07:00
}
response := &SlackResponse{}
2018-08-09 15:38:19 -07:00
2019-09-07 13:46:58 -07:00
if err := api.postMethod(ctx, "dnd.endDnd", values, response); err != nil {
2016-09-05 07:34:37 -07:00
return err
}
2018-08-09 15:38:19 -07:00
return response.Err()
2016-09-05 07:34:37 -07:00
}
// EndSnooze ends the current user's snooze mode.
// For more information see the EndSnoozeContext documentation.
2016-09-05 07:34:37 -07:00
func (api *Client) EndSnooze() (*DNDStatus, error) {
2017-07-16 05:29:46 -07:00
return api.EndSnoozeContext(context.Background())
}
// EndSnoozeContext ends the current user's snooze mode with a custom context.
// Slack API docs: https://api.slack.com/methods/dnd.endSnooze
2017-07-16 05:29:46 -07:00
func (api *Client) EndSnoozeContext(ctx context.Context) (*DNDStatus, error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2016-09-05 07:34:37 -07:00
}
2019-09-07 13:46:58 -07:00
response, err := api.dndRequest(ctx, "dnd.endSnooze", values)
2016-09-05 07:34:37 -07:00
if err != nil {
return nil, err
}
return &response.DNDStatus, nil
}
// GetDNDInfo provides information about a user's current Do Not Disturb settings.
// For more information see the GetDNDInfoContext documentation.
2016-09-05 07:34:37 -07:00
func (api *Client) GetDNDInfo(user *string) (*DNDStatus, error) {
2017-07-16 05:29:46 -07:00
return api.GetDNDInfoContext(context.Background(), user)
}
// GetDNDInfoContext provides information about a user's current Do Not Disturb settings with a custom context.
// Slack API docs: https://api.slack.com/methods/dnd.info
2017-07-16 05:29:46 -07:00
func (api *Client) GetDNDInfoContext(ctx context.Context, user *string) (*DNDStatus, error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2016-09-05 07:34:37 -07:00
}
if user != nil {
values.Set("user", *user)
}
2018-08-09 15:38:19 -07:00
2019-09-07 13:46:58 -07:00
response, err := api.dndRequest(ctx, "dnd.info", values)
2016-09-05 07:34:37 -07:00
if err != nil {
return nil, err
}
return &response.DNDStatus, nil
}
// GetDNDTeamInfo provides information about a user's current Do Not Disturb settings.
// For more information see the GetDNDTeamInfoContext documentation.
2016-09-05 07:34:37 -07:00
func (api *Client) GetDNDTeamInfo(users []string) (map[string]DNDStatus, error) {
2017-07-16 05:29:46 -07:00
return api.GetDNDTeamInfoContext(context.Background(), users)
}
// GetDNDTeamInfoContext provides information about a user's current Do Not Disturb settings with a custom context.
// Slack API docs: https://api.slack.com/methods/dnd.teamInfo
2017-07-16 05:29:46 -07:00
func (api *Client) GetDNDTeamInfoContext(ctx context.Context, users []string) (map[string]DNDStatus, error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2016-09-05 07:34:37 -07:00
"users": {strings.Join(users, ",")},
}
response := &dndTeamInfoResponse{}
2018-08-09 15:38:19 -07:00
2019-09-07 13:46:58 -07:00
if err := api.postMethod(ctx, "dnd.teamInfo", values, response); err != nil {
2016-09-05 07:34:37 -07:00
return nil, err
}
2018-12-01 10:55:35 -08:00
if response.Err() != nil {
return nil, response.Err()
2016-09-05 07:34:37 -07:00
}
2019-09-07 13:46:58 -07:00
2016-09-05 07:34:37 -07:00
return response.Users, nil
}
// SetSnooze adjusts the snooze duration for a user's Do Not Disturb settings.
// For more information see the SetSnoozeContext documentation.
2016-09-05 07:34:37 -07:00
func (api *Client) SetSnooze(minutes int) (*DNDStatus, error) {
2017-07-16 05:29:46 -07:00
return api.SetSnoozeContext(context.Background(), minutes)
}
// SetSnoozeContext adjusts the snooze duration for a user's Do Not Disturb settings.
// If a snooze session is not already active for the user, invoking this method will
// begin one for the specified duration.
// Slack API docs: https://api.slack.com/methods/dnd.setSnooze
2017-07-16 05:29:46 -07:00
func (api *Client) SetSnoozeContext(ctx context.Context, minutes int) (*DNDStatus, error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2016-09-05 07:34:37 -07:00
"num_minutes": {strconv.Itoa(minutes)},
}
2018-08-09 15:38:19 -07:00
2019-09-07 13:46:58 -07:00
response, err := api.dndRequest(ctx, "dnd.setSnooze", values)
2016-09-05 07:34:37 -07:00
if err != nil {
return nil, err
}
return &response.DNDStatus, nil
}