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

482 lines
16 KiB
Go
Raw Permalink 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"
2020-03-28 15:50:47 -07:00
"time"
2016-09-05 07:34:37 -07:00
)
type channelResponseFull struct {
Channel Channel `json:"channel"`
Channels []Channel `json:"channels"`
Purpose string `json:"purpose"`
Topic string `json:"topic"`
NotInChannel bool `json:"not_in_channel"`
History
SlackResponse
2020-03-28 15:50:47 -07:00
Metadata ResponseMetadata `json:"response_metadata"`
2016-09-05 07:34:37 -07:00
}
// Channel contains information about the channel
type Channel struct {
2019-09-07 13:46:58 -07:00
GroupConversation
2018-08-09 15:38:19 -07:00
IsChannel bool `json:"is_channel"`
IsGeneral bool `json:"is_general"`
IsMember bool `json:"is_member"`
Locale string `json:"locale"`
2016-09-05 07:34:37 -07:00
}
2019-09-07 13:46:58 -07:00
func (api *Client) channelRequest(ctx context.Context, path string, values url.Values) (*channelResponseFull, error) {
2016-09-05 07:34:37 -07:00
response := &channelResponseFull{}
2019-09-07 13:46:58 -07:00
err := postForm(ctx, api.httpclient, api.endpoint+path, values, response, api)
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
}
// GetChannelsOption option provided when getting channels.
2020-03-28 15:50:47 -07:00
type GetChannelsOption func(*ChannelPagination) error
// GetChannelsOptionExcludeMembers excludes the members collection from each channel.
func GetChannelsOptionExcludeMembers() GetChannelsOption {
2020-03-28 15:50:47 -07:00
return func(p *ChannelPagination) error {
p.excludeMembers = true
return nil
}
}
// GetChannelsOptionExcludeArchived excludes archived channels from results.
func GetChannelsOptionExcludeArchived() GetChannelsOption {
2020-03-28 15:50:47 -07:00
return func(p *ChannelPagination) error {
p.excludeArchived = true
return nil
}
}
2016-09-05 07:34:37 -07:00
// ArchiveChannel archives the given channel
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.archive
func (api *Client) ArchiveChannel(channelID string) error {
return api.ArchiveChannelContext(context.Background(), channelID)
2017-07-16 05:29:46 -07:00
}
// ArchiveChannelContext archives the given channel with a custom context
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.archive
2018-08-09 15:38:19 -07:00
func (api *Client) ArchiveChannelContext(ctx context.Context, channelID string) (err error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2018-01-08 13:41:38 -08:00
"channel": {channelID},
2016-09-05 07:34:37 -07:00
}
2018-08-09 15:38:19 -07:00
2019-09-07 13:46:58 -07:00
_, err = api.channelRequest(ctx, "channels.archive", values)
2018-01-08 13:41:38 -08:00
return err
2016-09-05 07:34:37 -07:00
}
// UnarchiveChannel unarchives the given channel
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.unarchive
func (api *Client) UnarchiveChannel(channelID string) error {
return api.UnarchiveChannelContext(context.Background(), channelID)
2017-07-16 05:29:46 -07:00
}
// UnarchiveChannelContext unarchives the given channel with a custom context
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.unarchive
2018-08-09 15:38:19 -07:00
func (api *Client) UnarchiveChannelContext(ctx context.Context, channelID string) (err error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2018-01-08 13:41:38 -08:00
"channel": {channelID},
2016-09-05 07:34:37 -07:00
}
2018-08-09 15:38:19 -07:00
2019-09-07 13:46:58 -07:00
_, err = api.channelRequest(ctx, "channels.unarchive", values)
2018-01-08 13:41:38 -08:00
return err
2016-09-05 07:34:37 -07:00
}
// CreateChannel creates a channel with the given name and returns a *Channel
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.create
func (api *Client) CreateChannel(channelName string) (*Channel, error) {
return api.CreateChannelContext(context.Background(), channelName)
2017-07-16 05:29:46 -07:00
}
// CreateChannelContext creates a channel with the given name and returns a *Channel with a custom context
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.create
func (api *Client) CreateChannelContext(ctx context.Context, channelName string) (*Channel, error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2018-01-08 13:41:38 -08:00
"name": {channelName},
2016-09-05 07:34:37 -07:00
}
2018-08-09 15:38:19 -07:00
2019-09-07 13:46:58 -07:00
response, err := api.channelRequest(ctx, "channels.create", values)
2016-09-05 07:34:37 -07:00
if err != nil {
return nil, err
}
return &response.Channel, nil
}
// GetChannelHistory retrieves the channel history
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.history
func (api *Client) GetChannelHistory(channelID string, params HistoryParameters) (*History, error) {
return api.GetChannelHistoryContext(context.Background(), channelID, params)
2017-07-16 05:29:46 -07:00
}
// GetChannelHistoryContext retrieves the channel history with a custom context
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.history
func (api *Client) GetChannelHistoryContext(ctx context.Context, channelID string, params HistoryParameters) (*History, error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2018-01-08 13:41:38 -08:00
"channel": {channelID},
2016-09-05 07:34:37 -07:00
}
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")
}
}
2018-08-09 15:38:19 -07:00
2016-09-05 07:34:37 -07:00
if params.Unreads != DEFAULT_HISTORY_UNREADS {
if params.Unreads {
values.Add("unreads", "1")
} else {
values.Add("unreads", "0")
}
}
2018-08-09 15:38:19 -07:00
2019-09-07 13:46:58 -07:00
response, err := api.channelRequest(ctx, "channels.history", values)
2016-09-05 07:34:37 -07:00
if err != nil {
return nil, err
}
return &response.History, nil
}
// GetChannelInfo retrieves the given channel
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.info
func (api *Client) GetChannelInfo(channelID string) (*Channel, error) {
return api.GetChannelInfoContext(context.Background(), channelID)
2017-07-16 05:29:46 -07:00
}
// GetChannelInfoContext retrieves the given channel with a custom context
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.info
func (api *Client) GetChannelInfoContext(ctx context.Context, channelID string) (*Channel, error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
"token": {api.token},
"channel": {channelID},
"include_locale": {strconv.FormatBool(true)},
2016-09-05 07:34:37 -07:00
}
2018-08-09 15:38:19 -07:00
2019-09-07 13:46:58 -07:00
response, err := api.channelRequest(ctx, "channels.info", values)
2016-09-05 07:34:37 -07:00
if err != nil {
return nil, err
}
return &response.Channel, nil
}
// InviteUserToChannel invites a user to a given channel and returns a *Channel
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.invite
func (api *Client) InviteUserToChannel(channelID, user string) (*Channel, error) {
return api.InviteUserToChannelContext(context.Background(), channelID, user)
2017-07-16 05:29:46 -07:00
}
2018-12-01 10:55:35 -08:00
// InviteUserToChannelContext invites a user to a given channel and returns a *Channel with a custom context
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.invite
func (api *Client) InviteUserToChannelContext(ctx context.Context, channelID, user string) (*Channel, error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2018-01-08 13:41:38 -08:00
"channel": {channelID},
2016-09-05 07:34:37 -07:00
"user": {user},
}
2018-08-09 15:38:19 -07:00
2019-09-07 13:46:58 -07:00
response, err := api.channelRequest(ctx, "channels.invite", values)
2016-09-05 07:34:37 -07:00
if err != nil {
return nil, err
}
return &response.Channel, nil
}
// JoinChannel joins the currently authenticated user to a channel
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.join
func (api *Client) JoinChannel(channelName string) (*Channel, error) {
return api.JoinChannelContext(context.Background(), channelName)
2017-07-16 05:29:46 -07:00
}
// JoinChannelContext joins the currently authenticated user to a channel with a custom context
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.join
func (api *Client) JoinChannelContext(ctx context.Context, channelName string) (*Channel, error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2018-01-08 13:41:38 -08:00
"name": {channelName},
2016-09-05 07:34:37 -07:00
}
2018-08-09 15:38:19 -07:00
2019-09-07 13:46:58 -07:00
response, err := api.channelRequest(ctx, "channels.join", values)
2016-09-05 07:34:37 -07:00
if err != nil {
return nil, err
}
return &response.Channel, nil
}
// LeaveChannel makes the authenticated user leave the given channel
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.leave
func (api *Client) LeaveChannel(channelID string) (bool, error) {
return api.LeaveChannelContext(context.Background(), channelID)
2017-07-16 05:29:46 -07:00
}
// LeaveChannelContext makes the authenticated user leave the given channel with a custom context
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.leave
func (api *Client) LeaveChannelContext(ctx context.Context, channelID string) (bool, error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2018-01-08 13:41:38 -08:00
"channel": {channelID},
2016-09-05 07:34:37 -07:00
}
2018-08-09 15:38:19 -07:00
2019-09-07 13:46:58 -07:00
response, err := api.channelRequest(ctx, "channels.leave", values)
2016-09-05 07:34:37 -07:00
if err != nil {
return false, err
}
2018-08-09 15:38:19 -07:00
return response.NotInChannel, nil
2016-09-05 07:34:37 -07:00
}
// KickUserFromChannel kicks a user from a given channel
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.kick
func (api *Client) KickUserFromChannel(channelID, user string) error {
return api.KickUserFromChannelContext(context.Background(), channelID, user)
2017-07-16 05:29:46 -07:00
}
// KickUserFromChannelContext kicks a user from a given channel with a custom context
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.kick
2018-08-09 15:38:19 -07:00
func (api *Client) KickUserFromChannelContext(ctx context.Context, channelID, user string) (err error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2018-01-08 13:41:38 -08:00
"channel": {channelID},
2016-09-05 07:34:37 -07:00
"user": {user},
}
2018-08-09 15:38:19 -07:00
2019-09-07 13:46:58 -07:00
_, err = api.channelRequest(ctx, "channels.kick", values)
2018-01-08 13:41:38 -08:00
return err
2016-09-05 07:34:37 -07:00
}
2020-03-28 15:50:47 -07:00
func newChannelPagination(c *Client, options ...GetChannelsOption) (cp ChannelPagination) {
cp = ChannelPagination{
c: c,
limit: 200, // per slack api documentation.
}
for _, opt := range options {
opt(&cp)
}
return cp
}
// ChannelPagination allows for paginating over the channels
type ChannelPagination struct {
Channels []Channel
limit int
excludeArchived bool
excludeMembers bool
previousResp *ResponseMetadata
c *Client
}
// Done checks if the pagination has completed
func (ChannelPagination) Done(err error) bool {
return err == errPaginationComplete
}
// Failure checks if pagination failed.
func (t ChannelPagination) Failure(err error) error {
if t.Done(err) {
return nil
}
return err
}
func (t ChannelPagination) Next(ctx context.Context) (_ ChannelPagination, err error) {
var (
resp *channelResponseFull
)
if t.c == nil || (t.previousResp != nil && t.previousResp.Cursor == "") {
return t, errPaginationComplete
}
t.previousResp = t.previousResp.initialize()
values := url.Values{
"limit": {strconv.Itoa(t.limit)},
"exclude_archived": {strconv.FormatBool(t.excludeArchived)},
"exclude_members": {strconv.FormatBool(t.excludeMembers)},
"token": {t.c.token},
"cursor": {t.previousResp.Cursor},
}
if resp, err = t.c.channelRequest(ctx, "channels.list", values); err != nil {
return t, err
}
t.c.Debugf("GetChannelsContext: got %d channels; metadata %v", len(resp.Channels), resp.Metadata)
t.Channels = resp.Channels
t.previousResp = &resp.Metadata
return t, nil
}
// GetChannelsPaginated fetches channels in a paginated fashion, see GetChannelsContext for usage.
func (api *Client) GetChannelsPaginated(options ...GetChannelsOption) ChannelPagination {
return newChannelPagination(api, options...)
}
2016-09-05 07:34:37 -07:00
// GetChannels retrieves all the channels
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.list
func (api *Client) GetChannels(excludeArchived bool, options ...GetChannelsOption) ([]Channel, error) {
return api.GetChannelsContext(context.Background(), excludeArchived, options...)
2017-07-16 05:29:46 -07:00
}
// GetChannelsContext retrieves all the channels with a custom context
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.list
2020-03-28 15:50:47 -07:00
func (api *Client) GetChannelsContext(ctx context.Context, excludeArchived bool, options ...GetChannelsOption) (results []Channel, err error) {
2016-09-05 07:34:37 -07:00
if excludeArchived {
options = append(options, GetChannelsOptionExcludeArchived())
}
2020-03-28 15:50:47 -07:00
p := api.GetChannelsPaginated(options...)
for err == nil {
p, err = p.Next(ctx)
if err == nil {
results = append(results, p.Channels...)
} else if rateLimitedError, ok := err.(*RateLimitedError); ok {
select {
case <-ctx.Done():
err = ctx.Err()
case <-time.After(rateLimitedError.RetryAfter):
err = nil
}
}
2016-09-05 07:34:37 -07:00
}
2018-08-09 15:38:19 -07:00
2020-03-28 15:50:47 -07:00
return results, p.Failure(err)
2016-09-05 07:34:37 -07:00
}
// SetChannelReadMark sets the read mark of a given channel to a specific point
// Clients should try to avoid making this call too often. When needing to mark a read position, a client should set a
// timer before making the call. In this way, any further updates needed during the timeout will not generate extra calls
// (just one per channel). This is useful for when reading scroll-back history, or following a busy live channel. A
// timeout of 5 seconds is a good starting point. Be sure to flush these calls on shutdown/logout.
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.mark
func (api *Client) SetChannelReadMark(channelID, ts string) error {
return api.SetChannelReadMarkContext(context.Background(), channelID, ts)
2017-07-16 05:29:46 -07:00
}
// SetChannelReadMarkContext sets the read mark of a given channel to a specific point with a custom context
// For more details see SetChannelReadMark documentation
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.mark
2018-08-09 15:38:19 -07:00
func (api *Client) SetChannelReadMarkContext(ctx context.Context, channelID, ts string) (err error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2018-01-08 13:41:38 -08:00
"channel": {channelID},
2016-09-05 07:34:37 -07:00
"ts": {ts},
}
2018-08-09 15:38:19 -07:00
2019-09-07 13:46:58 -07:00
_, err = api.channelRequest(ctx, "channels.mark", values)
2018-01-08 13:41:38 -08:00
return err
2016-09-05 07:34:37 -07:00
}
// RenameChannel renames a given channel
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.rename
func (api *Client) RenameChannel(channelID, name string) (*Channel, error) {
return api.RenameChannelContext(context.Background(), channelID, name)
2017-07-16 05:29:46 -07:00
}
// RenameChannelContext renames a given channel with a custom context
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.rename
func (api *Client) RenameChannelContext(ctx context.Context, channelID, name string) (*Channel, error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2018-01-08 13:41:38 -08:00
"channel": {channelID},
2016-09-05 07:34:37 -07:00
"name": {name},
}
2018-08-09 15:38:19 -07:00
2016-09-05 07:34:37 -07:00
// XXX: the created entry in this call returns a string instead of a number
// so I may have to do some workaround to solve it.
2019-09-07 13:46:58 -07:00
response, err := api.channelRequest(ctx, "channels.rename", values)
2016-09-05 07:34:37 -07:00
if err != nil {
return nil, err
}
return &response.Channel, nil
}
2017-07-16 05:29:46 -07:00
// SetChannelPurpose sets the channel purpose and returns the purpose that was successfully set
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.setPurpose
func (api *Client) SetChannelPurpose(channelID, purpose string) (string, error) {
return api.SetChannelPurposeContext(context.Background(), channelID, purpose)
2017-07-16 05:29:46 -07:00
}
// SetChannelPurposeContext sets the channel purpose and returns the purpose that was successfully set with a custom context
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.setPurpose
func (api *Client) SetChannelPurposeContext(ctx context.Context, channelID, purpose string) (string, error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2018-01-08 13:41:38 -08:00
"channel": {channelID},
2016-09-05 07:34:37 -07:00
"purpose": {purpose},
}
2018-08-09 15:38:19 -07:00
2019-09-07 13:46:58 -07:00
response, err := api.channelRequest(ctx, "channels.setPurpose", values)
2016-09-05 07:34:37 -07:00
if err != nil {
return "", err
}
return response.Purpose, nil
}
// SetChannelTopic sets the channel topic and returns the topic that was successfully set
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.setTopic
func (api *Client) SetChannelTopic(channelID, topic string) (string, error) {
return api.SetChannelTopicContext(context.Background(), channelID, topic)
2017-07-16 05:29:46 -07:00
}
// SetChannelTopicContext sets the channel topic and returns the topic that was successfully set with a custom context
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.setTopic
func (api *Client) SetChannelTopicContext(ctx context.Context, channelID, topic string) (string, error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2018-01-08 13:41:38 -08:00
"channel": {channelID},
2016-09-05 07:34:37 -07:00
"topic": {topic},
}
2018-08-09 15:38:19 -07:00
2019-09-07 13:46:58 -07:00
response, err := api.channelRequest(ctx, "channels.setTopic", values)
2016-09-05 07:34:37 -07:00
if err != nil {
return "", err
}
return response.Topic, nil
}
2017-07-16 05:29:46 -07:00
// GetChannelReplies gets an entire thread (a message plus all the messages in reply to it).
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.replies
func (api *Client) GetChannelReplies(channelID, thread_ts string) ([]Message, error) {
return api.GetChannelRepliesContext(context.Background(), channelID, thread_ts)
2017-07-16 05:29:46 -07:00
}
// GetChannelRepliesContext gets an entire thread (a message plus all the messages in reply to it) with a custom context
2018-01-08 13:41:38 -08:00
// see https://api.slack.com/methods/channels.replies
func (api *Client) GetChannelRepliesContext(ctx context.Context, channelID, thread_ts string) ([]Message, error) {
2017-07-16 05:29:46 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2018-01-08 13:41:38 -08:00
"channel": {channelID},
2017-07-16 05:29:46 -07:00
"thread_ts": {thread_ts},
}
2019-09-07 13:46:58 -07:00
response, err := api.channelRequest(ctx, "channels.replies", values)
2017-07-16 05:29:46 -07:00
if err != nil {
return nil, err
}
return response.History.Messages, nil
}