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

153 lines
4.4 KiB
Go
Raw Normal View History

2016-09-05 16:34:37 +02:00
package slack
import (
2017-07-16 14:29:46 +02:00
"context"
2016-09-05 16:34:37 +02:00
"errors"
"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
}
2018-08-10 00:38:19 +02:00
func dndRequest(ctx context.Context, client HTTPRequester, path string, values url.Values, debug bool) (*dndResponseFull, error) {
2016-09-05 16:34:37 +02:00
response := &dndResponseFull{}
2018-08-10 00:38:19 +02:00
err := postSlackMethod(ctx, client, path, values, response, debug)
2016-09-05 16:34:37 +02:00
if err != nil {
return nil, err
}
if !response.Ok {
return nil, errors.New(response.Error)
}
return response, nil
}
// EndDND ends the user's scheduled Do Not Disturb session
func (api *Client) EndDND() error {
2017-07-16 14:29:46 +02:00
return api.EndDNDContext(context.Background())
}
// EndDNDContext ends the user's scheduled Do Not Disturb session with a custom context
func (api *Client) EndDNDContext(ctx context.Context) error {
2016-09-05 16:34:37 +02:00
values := url.Values{
2018-08-10 00:38:19 +02:00
"token": {api.token},
2016-09-05 16:34:37 +02:00
}
response := &SlackResponse{}
2018-08-10 00:38:19 +02:00
if err := postSlackMethod(ctx, api.httpclient, "dnd.endDnd", values, response, api.debug); err != nil {
2016-09-05 16:34:37 +02:00
return err
}
2018-08-10 00:38:19 +02:00
return response.Err()
2016-09-05 16:34:37 +02:00
}
// EndSnooze ends the current user's snooze mode
func (api *Client) EndSnooze() (*DNDStatus, error) {
2017-07-16 14:29:46 +02:00
return api.EndSnoozeContext(context.Background())
}
// EndSnoozeContext ends the current user's snooze mode with a custom context
func (api *Client) EndSnoozeContext(ctx context.Context) (*DNDStatus, error) {
2016-09-05 16:34:37 +02:00
values := url.Values{
2018-08-10 00:38:19 +02:00
"token": {api.token},
2016-09-05 16:34:37 +02:00
}
2018-08-10 00:38:19 +02:00
response, err := dndRequest(ctx, api.httpclient, "dnd.endSnooze", values, api.debug)
2016-09-05 16:34:37 +02:00
if err != nil {
return nil, err
}
return &response.DNDStatus, nil
}
// GetDNDInfo provides information about a user's current Do Not Disturb settings.
func (api *Client) GetDNDInfo(user *string) (*DNDStatus, error) {
2017-07-16 14:29:46 +02:00
return api.GetDNDInfoContext(context.Background(), user)
}
// GetDNDInfoContext provides information about a user's current Do Not Disturb settings with a custom context.
func (api *Client) GetDNDInfoContext(ctx context.Context, user *string) (*DNDStatus, error) {
2016-09-05 16:34:37 +02:00
values := url.Values{
2018-08-10 00:38:19 +02:00
"token": {api.token},
2016-09-05 16:34:37 +02:00
}
if user != nil {
values.Set("user", *user)
}
2018-08-10 00:38:19 +02:00
response, err := dndRequest(ctx, api.httpclient, "dnd.info", values, api.debug)
2016-09-05 16:34:37 +02:00
if err != nil {
return nil, err
}
return &response.DNDStatus, nil
}
// GetDNDTeamInfo provides information about a user's current Do Not Disturb settings.
func (api *Client) GetDNDTeamInfo(users []string) (map[string]DNDStatus, error) {
2017-07-16 14:29:46 +02:00
return api.GetDNDTeamInfoContext(context.Background(), users)
}
// GetDNDTeamInfoContext provides information about a user's current Do Not Disturb settings with a custom context.
func (api *Client) GetDNDTeamInfoContext(ctx context.Context, users []string) (map[string]DNDStatus, error) {
2016-09-05 16:34:37 +02:00
values := url.Values{
2018-08-10 00:38:19 +02:00
"token": {api.token},
2016-09-05 16:34:37 +02:00
"users": {strings.Join(users, ",")},
}
response := &dndTeamInfoResponse{}
2018-08-10 00:38:19 +02:00
if err := postSlackMethod(ctx, api.httpclient, "dnd.teamInfo", values, response, api.debug); err != nil {
2016-09-05 16:34:37 +02:00
return nil, err
}
if !response.Ok {
return nil, errors.New(response.Error)
}
return response.Users, nil
}
// SetSnooze 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.
func (api *Client) SetSnooze(minutes int) (*DNDStatus, error) {
2017-07-16 14:29:46 +02:00
return api.SetSnoozeContext(context.Background(), minutes)
}
// SetSnooze adjusts the snooze duration for a user's Do Not Disturb settings with a custom context.
// For more information see the SetSnooze docs
func (api *Client) SetSnoozeContext(ctx context.Context, minutes int) (*DNDStatus, error) {
2016-09-05 16:34:37 +02:00
values := url.Values{
2018-08-10 00:38:19 +02:00
"token": {api.token},
2016-09-05 16:34:37 +02:00
"num_minutes": {strconv.Itoa(minutes)},
}
2018-08-10 00:38:19 +02:00
response, err := dndRequest(ctx, api.httpclient, "dnd.setSnooze", values, api.debug)
2016-09-05 16:34:37 +02:00
if err != nil {
return nil, err
}
return &response.DNDStatus, nil
}