diff options
Diffstat (limited to 'vendor/github.com/nlopes/slack/channels.go')
-rw-r--r-- | vendor/github.com/nlopes/slack/channels.go | 127 |
1 files changed, 108 insertions, 19 deletions
diff --git a/vendor/github.com/nlopes/slack/channels.go b/vendor/github.com/nlopes/slack/channels.go index 05cfbe1d..4a67b2fb 100644 --- a/vendor/github.com/nlopes/slack/channels.go +++ b/vendor/github.com/nlopes/slack/channels.go @@ -1,6 +1,7 @@ package slack import ( + "context" "errors" "net/url" "strconv" @@ -24,9 +25,9 @@ type Channel struct { IsMember bool `json:"is_member"` } -func channelRequest(path string, values url.Values, debug bool) (*channelResponseFull, error) { +func channelRequest(ctx context.Context, path string, values url.Values, debug bool) (*channelResponseFull, error) { response := &channelResponseFull{} - err := post(path, values, response, debug) + err := post(ctx, path, values, response, debug) if err != nil { return nil, err } @@ -38,11 +39,16 @@ func channelRequest(path string, values url.Values, debug bool) (*channelRespons // ArchiveChannel archives the given channel func (api *Client) ArchiveChannel(channel string) error { + return api.ArchiveChannelContext(context.Background(), channel) +} + +// ArchiveChannelContext archives the given channel with a custom context +func (api *Client) ArchiveChannelContext(ctx context.Context, channel string) error { values := url.Values{ "token": {api.config.token}, "channel": {channel}, } - _, err := channelRequest("channels.archive", values, api.debug) + _, err := channelRequest(ctx, "channels.archive", values, api.debug) if err != nil { return err } @@ -51,11 +57,16 @@ func (api *Client) ArchiveChannel(channel string) error { // UnarchiveChannel unarchives the given channel func (api *Client) UnarchiveChannel(channel string) error { + return api.UnarchiveChannelContext(context.Background(), channel) +} + +// UnarchiveChannelContext unarchives the given channel with a custom context +func (api *Client) UnarchiveChannelContext(ctx context.Context, channel string) error { values := url.Values{ "token": {api.config.token}, "channel": {channel}, } - _, err := channelRequest("channels.unarchive", values, api.debug) + _, err := channelRequest(ctx, "channels.unarchive", values, api.debug) if err != nil { return err } @@ -64,11 +75,16 @@ func (api *Client) UnarchiveChannel(channel string) error { // CreateChannel creates a channel with the given name and returns a *Channel func (api *Client) CreateChannel(channel string) (*Channel, error) { + return api.CreateChannelContext(context.Background(), channel) +} + +// CreateChannelContext creates a channel with the given name and returns a *Channel with a custom context +func (api *Client) CreateChannelContext(ctx context.Context, channel string) (*Channel, error) { values := url.Values{ "token": {api.config.token}, "name": {channel}, } - response, err := channelRequest("channels.create", values, api.debug) + response, err := channelRequest(ctx, "channels.create", values, api.debug) if err != nil { return nil, err } @@ -77,6 +93,11 @@ func (api *Client) CreateChannel(channel string) (*Channel, error) { // GetChannelHistory retrieves the channel history func (api *Client) GetChannelHistory(channel string, params HistoryParameters) (*History, error) { + return api.GetChannelHistoryContext(context.Background(), channel, params) +} + +// GetChannelHistoryContext retrieves the channel history with a custom context +func (api *Client) GetChannelHistoryContext(ctx context.Context, channel string, params HistoryParameters) (*History, error) { values := url.Values{ "token": {api.config.token}, "channel": {channel}, @@ -104,7 +125,7 @@ func (api *Client) GetChannelHistory(channel string, params HistoryParameters) ( values.Add("unreads", "0") } } - response, err := channelRequest("channels.history", values, api.debug) + response, err := channelRequest(ctx, "channels.history", values, api.debug) if err != nil { return nil, err } @@ -113,11 +134,16 @@ func (api *Client) GetChannelHistory(channel string, params HistoryParameters) ( // GetChannelInfo retrieves the given channel func (api *Client) GetChannelInfo(channel string) (*Channel, error) { + return api.GetChannelInfoContext(context.Background(), channel) +} + +// GetChannelInfoContext retrieves the given channel with a custom context +func (api *Client) GetChannelInfoContext(ctx context.Context, channel string) (*Channel, error) { values := url.Values{ "token": {api.config.token}, "channel": {channel}, } - response, err := channelRequest("channels.info", values, api.debug) + response, err := channelRequest(ctx, "channels.info", values, api.debug) if err != nil { return nil, err } @@ -126,12 +152,17 @@ func (api *Client) GetChannelInfo(channel string) (*Channel, error) { // InviteUserToChannel invites a user to a given channel and returns a *Channel func (api *Client) InviteUserToChannel(channel, user string) (*Channel, error) { + return api.InviteUserToChannelContext(context.Background(), channel, user) +} + +// InviteUserToChannelCustom invites a user to a given channel and returns a *Channel with a custom context +func (api *Client) InviteUserToChannelContext(ctx context.Context, channel, user string) (*Channel, error) { values := url.Values{ "token": {api.config.token}, "channel": {channel}, "user": {user}, } - response, err := channelRequest("channels.invite", values, api.debug) + response, err := channelRequest(ctx, "channels.invite", values, api.debug) if err != nil { return nil, err } @@ -140,11 +171,16 @@ func (api *Client) InviteUserToChannel(channel, user string) (*Channel, error) { // JoinChannel joins the currently authenticated user to a channel func (api *Client) JoinChannel(channel string) (*Channel, error) { + return api.JoinChannelContext(context.Background(), channel) +} + +// JoinChannelContext joins the currently authenticated user to a channel with a custom context +func (api *Client) JoinChannelContext(ctx context.Context, channel string) (*Channel, error) { values := url.Values{ "token": {api.config.token}, "name": {channel}, } - response, err := channelRequest("channels.join", values, api.debug) + response, err := channelRequest(ctx, "channels.join", values, api.debug) if err != nil { return nil, err } @@ -153,11 +189,16 @@ func (api *Client) JoinChannel(channel string) (*Channel, error) { // LeaveChannel makes the authenticated user leave the given channel func (api *Client) LeaveChannel(channel string) (bool, error) { + return api.LeaveChannelContext(context.Background(), channel) +} + +// LeaveChannelContext makes the authenticated user leave the given channel with a custom context +func (api *Client) LeaveChannelContext(ctx context.Context, channel string) (bool, error) { values := url.Values{ "token": {api.config.token}, "channel": {channel}, } - response, err := channelRequest("channels.leave", values, api.debug) + response, err := channelRequest(ctx, "channels.leave", values, api.debug) if err != nil { return false, err } @@ -169,12 +210,17 @@ func (api *Client) LeaveChannel(channel string) (bool, error) { // KickUserFromChannel kicks a user from a given channel func (api *Client) KickUserFromChannel(channel, user string) error { + return api.KickUserFromChannelContext(context.Background(), channel, user) +} + +// KickUserFromChannelContext kicks a user from a given channel with a custom context +func (api *Client) KickUserFromChannelContext(ctx context.Context, channel, user string) error { values := url.Values{ "token": {api.config.token}, "channel": {channel}, "user": {user}, } - _, err := channelRequest("channels.kick", values, api.debug) + _, err := channelRequest(ctx, "channels.kick", values, api.debug) if err != nil { return err } @@ -183,13 +229,18 @@ func (api *Client) KickUserFromChannel(channel, user string) error { // GetChannels retrieves all the channels func (api *Client) GetChannels(excludeArchived bool) ([]Channel, error) { + return api.GetChannelsContext(context.Background(), excludeArchived) +} + +// GetChannelsContext retrieves all the channels with a custom context +func (api *Client) GetChannelsContext(ctx context.Context, excludeArchived bool) ([]Channel, error) { values := url.Values{ "token": {api.config.token}, } if excludeArchived { values.Add("exclude_archived", "1") } - response, err := channelRequest("channels.list", values, api.debug) + response, err := channelRequest(ctx, "channels.list", values, api.debug) if err != nil { return nil, err } @@ -202,12 +253,18 @@ func (api *Client) GetChannels(excludeArchived bool) ([]Channel, error) { // (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. func (api *Client) SetChannelReadMark(channel, ts string) error { + return api.SetChannelReadMarkContext(context.Background(), channel, ts) +} + +// SetChannelReadMarkContext sets the read mark of a given channel to a specific point with a custom context +// For more details see SetChannelReadMark documentation +func (api *Client) SetChannelReadMarkContext(ctx context.Context, channel, ts string) error { values := url.Values{ "token": {api.config.token}, "channel": {channel}, "ts": {ts}, } - _, err := channelRequest("channels.mark", values, api.debug) + _, err := channelRequest(ctx, "channels.mark", values, api.debug) if err != nil { return err } @@ -216,6 +273,11 @@ func (api *Client) SetChannelReadMark(channel, ts string) error { // RenameChannel renames a given channel func (api *Client) RenameChannel(channel, name string) (*Channel, error) { + return api.RenameChannelContext(context.Background(), channel, name) +} + +// RenameChannelContext renames a given channel with a custom context +func (api *Client) RenameChannelContext(ctx context.Context, channel, name string) (*Channel, error) { values := url.Values{ "token": {api.config.token}, "channel": {channel}, @@ -223,23 +285,26 @@ func (api *Client) RenameChannel(channel, name string) (*Channel, error) { } // 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. - response, err := channelRequest("channels.rename", values, api.debug) + response, err := channelRequest(ctx, "channels.rename", values, api.debug) if err != nil { return nil, err } return &response.Channel, nil - } -// SetChannelPurpose sets the channel purpose and returns the purpose that was -// successfully set +// SetChannelPurpose sets the channel purpose and returns the purpose that was successfully set func (api *Client) SetChannelPurpose(channel, purpose string) (string, error) { + return api.SetChannelPurposeContext(context.Background(), channel, purpose) +} + +// SetChannelPurposeContext sets the channel purpose and returns the purpose that was successfully set with a custom context +func (api *Client) SetChannelPurposeContext(ctx context.Context, channel, purpose string) (string, error) { values := url.Values{ "token": {api.config.token}, "channel": {channel}, "purpose": {purpose}, } - response, err := channelRequest("channels.setPurpose", values, api.debug) + response, err := channelRequest(ctx, "channels.setPurpose", values, api.debug) if err != nil { return "", err } @@ -248,14 +313,38 @@ func (api *Client) SetChannelPurpose(channel, purpose string) (string, error) { // SetChannelTopic sets the channel topic and returns the topic that was successfully set func (api *Client) SetChannelTopic(channel, topic string) (string, error) { + return api.SetChannelTopicContext(context.Background(), channel, topic) +} + +// SetChannelTopicContext sets the channel topic and returns the topic that was successfully set with a custom context +func (api *Client) SetChannelTopicContext(ctx context.Context, channel, topic string) (string, error) { values := url.Values{ "token": {api.config.token}, "channel": {channel}, "topic": {topic}, } - response, err := channelRequest("channels.setTopic", values, api.debug) + response, err := channelRequest(ctx, "channels.setTopic", values, api.debug) if err != nil { return "", err } return response.Topic, nil } + +// GetChannelReplies gets an entire thread (a message plus all the messages in reply to it). +func (api *Client) GetChannelReplies(channel, thread_ts string) ([]Message, error) { + return api.GetChannelRepliesContext(context.Background(), channel, thread_ts) +} + +// GetChannelRepliesContext gets an entire thread (a message plus all the messages in reply to it) with a custom context +func (api *Client) GetChannelRepliesContext(ctx context.Context, channel, thread_ts string) ([]Message, error) { + values := url.Values{ + "token": {api.config.token}, + "channel": {channel}, + "thread_ts": {thread_ts}, + } + response, err := channelRequest(ctx, "channels.replies", values, api.debug) + if err != nil { + return nil, err + } + return response.History.Messages, nil +} |