diff options
Diffstat (limited to 'vendor/github.com/nlopes/slack/stars.go')
-rw-r--r-- | vendor/github.com/nlopes/slack/stars.go | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/vendor/github.com/nlopes/slack/stars.go b/vendor/github.com/nlopes/slack/stars.go index e84d0447..52967604 100644 --- a/vendor/github.com/nlopes/slack/stars.go +++ b/vendor/github.com/nlopes/slack/stars.go @@ -4,6 +4,7 @@ import ( "context" "net/url" "strconv" + "time" ) const ( @@ -158,3 +159,105 @@ func (api *Client) GetStarredContext(ctx context.Context, params StarsParameters } return starredItems, paging, nil } + +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 +} |