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

155 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
"net/url"
"strconv"
)
type imChannel struct {
ID string `json:"id"`
}
type imResponseFull struct {
NoOp bool `json:"no_op"`
AlreadyClosed bool `json:"already_closed"`
AlreadyOpen bool `json:"already_open"`
Channel imChannel `json:"channel"`
IMs []IM `json:"ims"`
History
SlackResponse
}
// IM contains information related to the Direct Message channel
type IM struct {
2019-09-07 22:46:58 +02:00
Conversation
IsUserDeleted bool `json:"is_user_deleted"`
2016-09-05 16:34:37 +02:00
}
2019-09-07 22:46:58 +02:00
func (api *Client) imRequest(ctx context.Context, path string, values url.Values) (*imResponseFull, error) {
2016-09-05 16:34:37 +02:00
response := &imResponseFull{}
2019-09-07 22:46:58 +02:00
err := api.postMethod(ctx, path, values, response)
2016-09-05 16:34:37 +02:00
if err != nil {
return nil, err
}
2018-12-01 19:55:35 +01:00
return response, response.Err()
2016-09-05 16:34:37 +02:00
}
// CloseIMChannel closes the direct message channel
func (api *Client) CloseIMChannel(channel string) (bool, bool, error) {
2017-07-16 14:29:46 +02:00
return api.CloseIMChannelContext(context.Background(), channel)
}
// CloseIMChannelContext closes the direct message channel with a custom context
func (api *Client) CloseIMChannelContext(ctx context.Context, channel string) (bool, bool, 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
"channel": {channel},
}
2018-08-10 00:38:19 +02:00
2019-09-07 22:46:58 +02:00
response, err := api.imRequest(ctx, "im.close", values)
2016-09-05 16:34:37 +02:00
if err != nil {
return false, false, err
}
return response.NoOp, response.AlreadyClosed, nil
}
// OpenIMChannel opens a direct message channel to the user provided as argument
// Returns some status and the channel ID
func (api *Client) OpenIMChannel(user string) (bool, bool, string, error) {
2017-07-16 14:29:46 +02:00
return api.OpenIMChannelContext(context.Background(), user)
}
// OpenIMChannelContext opens a direct message channel to the user provided as argument with a custom context
// Returns some status and the channel ID
func (api *Client) OpenIMChannelContext(ctx context.Context, user string) (bool, bool, string, 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
"user": {user},
}
2018-08-10 00:38:19 +02:00
2019-09-07 22:46:58 +02:00
response, err := api.imRequest(ctx, "im.open", values)
2016-09-05 16:34:37 +02:00
if err != nil {
return false, false, "", err
}
return response.NoOp, response.AlreadyOpen, response.Channel.ID, nil
}
// MarkIMChannel sets the read mark of a direct message channel to a specific point
func (api *Client) MarkIMChannel(channel, ts string) (err error) {
2017-07-16 14:29:46 +02:00
return api.MarkIMChannelContext(context.Background(), channel, ts)
}
// MarkIMChannelContext sets the read mark of a direct message channel to a specific point with a custom context
2018-08-10 00:38:19 +02:00
func (api *Client) MarkIMChannelContext(ctx context.Context, channel, ts string) 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
"channel": {channel},
"ts": {ts},
}
2018-08-10 00:38:19 +02:00
2019-09-07 22:46:58 +02:00
_, err := api.imRequest(ctx, "im.mark", values)
2018-08-10 00:38:19 +02:00
return err
2016-09-05 16:34:37 +02:00
}
// GetIMHistory retrieves the direct message channel history
func (api *Client) GetIMHistory(channel string, params HistoryParameters) (*History, error) {
2017-07-16 14:29:46 +02:00
return api.GetIMHistoryContext(context.Background(), channel, params)
}
// GetIMHistoryContext retrieves the direct message channel history with a custom context
func (api *Client) GetIMHistoryContext(ctx context.Context, channel string, params HistoryParameters) (*History, 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
"channel": {channel},
}
if params.Latest != DEFAULT_HISTORY_LATEST {
values.Add("latest", params.Latest)
}
if params.Oldest != DEFAULT_HISTORY_OLDEST {
values.Add("oldest", params.Oldest)
}
if params.Count != DEFAULT_HISTORY_COUNT {
values.Add("count", strconv.Itoa(params.Count))
}
if params.Inclusive != DEFAULT_HISTORY_INCLUSIVE {
if params.Inclusive {
values.Add("inclusive", "1")
} else {
values.Add("inclusive", "0")
}
}
if params.Unreads != DEFAULT_HISTORY_UNREADS {
if params.Unreads {
values.Add("unreads", "1")
} else {
values.Add("unreads", "0")
}
}
2018-08-10 00:38:19 +02:00
2019-09-07 22:46:58 +02:00
response, err := api.imRequest(ctx, "im.history", values)
2016-09-05 16:34:37 +02:00
if err != nil {
return nil, err
}
return &response.History, nil
}
// GetIMChannels returns the list of direct message channels
func (api *Client) GetIMChannels() ([]IM, error) {
2017-07-16 14:29:46 +02:00
return api.GetIMChannelsContext(context.Background())
}
// GetIMChannelsContext returns the list of direct message channels with a custom context
func (api *Client) GetIMChannelsContext(ctx context.Context) ([]IM, 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
2019-09-07 22:46:58 +02:00
response, err := api.imRequest(ctx, "im.list", values)
2016-09-05 16:34:37 +02:00
if err != nil {
return nil, err
}
return response.IMs, nil
}