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

270 lines
7.0 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"
2019-12-07 13:54:36 -08:00
"time"
2016-09-05 07:34:37 -07:00
)
const (
DEFAULT_STARS_USER = ""
DEFAULT_STARS_COUNT = 100
DEFAULT_STARS_PAGE = 1
)
type StarsParameters struct {
User string
Count int
Page int
}
type StarredItem Item
type listResponseFull struct {
Items []Item `json:"items"`
Paging `json:"paging"`
SlackResponse
}
// NewStarsParameters initialises StarsParameters with default values
func NewStarsParameters() StarsParameters {
return StarsParameters{
User: DEFAULT_STARS_USER,
Count: DEFAULT_STARS_COUNT,
Page: DEFAULT_STARS_PAGE,
}
}
// AddStar stars an item in a channel.
// For more information see the AddStarContext documentation.
2016-09-05 07:34:37 -07:00
func (api *Client) AddStar(channel string, item ItemRef) error {
2017-07-16 05:29:46 -07:00
return api.AddStarContext(context.Background(), channel, item)
}
// AddStarContext stars an item in a channel with a custom context.
// Slack API docs: https://api.slack.com/methods/stars.add
2017-07-16 05:29:46 -07:00
func (api *Client) AddStarContext(ctx context.Context, channel string, item ItemRef) error {
2016-09-05 07:34:37 -07:00
values := url.Values{
"channel": {channel},
2018-08-09 15:38:19 -07:00
"token": {api.token},
2016-09-05 07:34:37 -07:00
}
if item.Timestamp != "" {
2018-08-09 15:38:19 -07:00
values.Set("timestamp", item.Timestamp)
2016-09-05 07:34:37 -07:00
}
if item.File != "" {
2018-08-09 15:38:19 -07:00
values.Set("file", item.File)
2016-09-05 07:34:37 -07:00
}
if item.Comment != "" {
2018-08-09 15:38:19 -07:00
values.Set("file_comment", item.Comment)
2016-09-05 07:34:37 -07:00
}
2018-08-09 15:38:19 -07:00
2016-09-05 07:34:37 -07:00
response := &SlackResponse{}
2019-09-07 13:46:58 -07:00
if err := api.postMethod(ctx, "stars.add", 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
}
// RemoveStar removes a starred item from a channel.
// For more information see the RemoveStarContext documentation.
2016-09-05 07:34:37 -07:00
func (api *Client) RemoveStar(channel string, item ItemRef) error {
2017-07-16 05:29:46 -07:00
return api.RemoveStarContext(context.Background(), channel, item)
}
// RemoveStarContext removes a starred item from a channel with a custom context.
// Slack API docs: https://api.slack.com/methods/stars.remove
2017-07-16 05:29:46 -07:00
func (api *Client) RemoveStarContext(ctx context.Context, channel string, item ItemRef) error {
2016-09-05 07:34:37 -07:00
values := url.Values{
"channel": {channel},
2018-08-09 15:38:19 -07:00
"token": {api.token},
2016-09-05 07:34:37 -07:00
}
if item.Timestamp != "" {
2018-08-09 15:38:19 -07:00
values.Set("timestamp", item.Timestamp)
2016-09-05 07:34:37 -07:00
}
if item.File != "" {
2018-08-09 15:38:19 -07:00
values.Set("file", item.File)
2016-09-05 07:34:37 -07:00
}
if item.Comment != "" {
2018-08-09 15:38:19 -07:00
values.Set("file_comment", item.Comment)
2016-09-05 07:34:37 -07:00
}
2018-08-09 15:38:19 -07:00
2016-09-05 07:34:37 -07:00
response := &SlackResponse{}
2019-09-07 13:46:58 -07:00
if err := api.postMethod(ctx, "stars.remove", 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
}
// ListStars returns information about the stars a user added.
// For more information see the ListStarsContext documentation.
2016-09-05 07:34:37 -07:00
func (api *Client) ListStars(params StarsParameters) ([]Item, *Paging, error) {
2017-07-16 05:29:46 -07:00
return api.ListStarsContext(context.Background(), params)
}
// ListStarsContext returns information about the stars a user added with a custom context.
// Slack API docs: https://api.slack.com/methods/stars.list
2017-07-16 05:29:46 -07:00
func (api *Client) ListStarsContext(ctx context.Context, params StarsParameters) ([]Item, *Paging, 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 params.User != DEFAULT_STARS_USER {
values.Add("user", params.User)
}
if params.Count != DEFAULT_STARS_COUNT {
values.Add("count", strconv.Itoa(params.Count))
}
if params.Page != DEFAULT_STARS_PAGE {
values.Add("page", strconv.Itoa(params.Page))
}
2018-08-09 15:38:19 -07:00
2016-09-05 07:34:37 -07:00
response := &listResponseFull{}
2019-09-07 13:46:58 -07:00
err := api.postMethod(ctx, "stars.list", values, response)
2016-09-05 07:34:37 -07:00
if err != nil {
return nil, nil, err
}
2019-09-07 13:46:58 -07:00
if err := response.Err(); err != nil {
return nil, nil, 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.Items, &response.Paging, nil
}
2017-07-16 05:29:46 -07:00
// GetStarred returns a list of StarredItem items.
//
// The user then has to iterate over them and figure out what they should
2022-11-26 15:42:16 -08:00
// be looking at according to what is in the Type:
//
// for _, item := range items {
// switch c.Type {
// case "file_comment":
// log.Println(c.Comment)
// case "file":
// ...
// }
2016-09-05 07:34:37 -07:00
//
// This function still exists to maintain backwards compatibility.
2022-11-26 15:42:16 -08:00
// I exposed it as returning []StarredItem, so it shall stay as StarredItem.
2016-09-05 07:34:37 -07:00
func (api *Client) GetStarred(params StarsParameters) ([]StarredItem, *Paging, error) {
2017-07-16 05:29:46 -07:00
return api.GetStarredContext(context.Background(), params)
}
// GetStarredContext returns a list of StarredItem items with a custom context
// For more details see GetStarred
func (api *Client) GetStarredContext(ctx context.Context, params StarsParameters) ([]StarredItem, *Paging, error) {
items, paging, err := api.ListStarsContext(ctx, params)
2016-09-05 07:34:37 -07:00
if err != nil {
return nil, nil, err
}
starredItems := make([]StarredItem, len(items))
for i, item := range items {
starredItems[i] = StarredItem(item)
}
return starredItems, paging, nil
}
2019-12-07 13:54:36 -08:00
type listResponsePaginated struct {
Items []Item `json:"items"`
SlackResponse
Metadata ResponseMetadata `json:"response_metadata"`
}
// StarredItemPagination allows for paginating over the starred items
type StarredItemPagination struct {
Items []Item
limit int
previousResp *ResponseMetadata
c *Client
}
// ListStarsOption options for the GetUsers method call.
type ListStarsOption func(*StarredItemPagination)
// ListAllStars returns the complete list of starred items
func (api *Client) ListAllStars() ([]Item, error) {
return api.ListAllStarsContext(context.Background())
}
// ListAllStarsContext returns the list of users (with their detailed information) with a custom context
func (api *Client) ListAllStarsContext(ctx context.Context) (results []Item, err error) {
p := api.ListStarsPaginated()
for err == nil {
p, err = p.next(ctx)
if err == nil {
results = append(results, p.Items...)
} else if rateLimitedError, ok := err.(*RateLimitedError); ok {
select {
case <-ctx.Done():
err = ctx.Err()
case <-time.After(rateLimitedError.RetryAfter):
err = nil
}
}
}
return results, p.failure(err)
}
// ListStarsPaginated fetches users in a paginated fashion, see ListStarsPaginationContext for usage.
func (api *Client) ListStarsPaginated(options ...ListStarsOption) StarredItemPagination {
return newStarPagination(api, options...)
}
func newStarPagination(c *Client, options ...ListStarsOption) (sip StarredItemPagination) {
sip = StarredItemPagination{
c: c,
limit: 200, // per slack api documentation.
}
for _, opt := range options {
opt(&sip)
}
return sip
}
// done checks if the pagination has completed
func (StarredItemPagination) done(err error) bool {
return err == errPaginationComplete
}
// done checks if pagination failed.
func (t StarredItemPagination) failure(err error) error {
if t.done(err) {
return nil
}
return err
}
// next gets the next list of starred items based on the cursor value
func (t StarredItemPagination) next(ctx context.Context) (_ StarredItemPagination, err error) {
var (
resp *listResponsePaginated
)
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)},
"token": {t.c.token},
"cursor": {t.previousResp.Cursor},
}
if err = t.c.postMethod(ctx, "stars.list", values, &resp); err != nil {
return t, err
}
t.previousResp = &resp.Metadata
t.Items = resp.Items
return t, nil
}