Bumps the go-modules-updates group in /backend with 9 updates: | Package | From | To | | --- | --- | --- | | [github.com/PuerkitoBio/goquery](https://github.com/PuerkitoBio/goquery) | `1.9.2` | `1.10.0` | | [github.com/alecthomas/chroma/v2](https://github.com/alecthomas/chroma) | `2.13.0` | `2.14.0` | | [github.com/go-pkgz/notify](https://github.com/go-pkgz/notify) | `1.1.1` | `1.2.0` | | [github.com/go-pkgz/repeater](https://github.com/go-pkgz/repeater) | `1.1.3` | `1.2.0` | | [github.com/rs/xid](https://github.com/rs/xid) | `1.5.0` | `1.6.0` | | [go.etcd.io/bbolt](https://github.com/etcd-io/bbolt) | `1.3.10` | `1.3.11` | | [golang.org/x/crypto](https://github.com/golang/crypto) | `0.25.0` | `0.27.0` | | [golang.org/x/image](https://github.com/golang/image) | `0.18.0` | `0.22.0` | | [golang.org/x/net](https://github.com/golang/net) | `0.27.0` | `0.29.0` | Updates `github.com/PuerkitoBio/goquery` from 1.9.2 to 1.10.0 - [Release notes](https://github.com/PuerkitoBio/goquery/releases) - [Commits](https://github.com/PuerkitoBio/goquery/compare/v1.9.2...v1.10.0) Updates `github.com/alecthomas/chroma/v2` from 2.13.0 to 2.14.0 - [Release notes](https://github.com/alecthomas/chroma/releases) - [Changelog](https://github.com/alecthomas/chroma/blob/master/.goreleaser.yml) - [Commits](https://github.com/alecthomas/chroma/compare/v2.13.0...v2.14.0) Updates `github.com/go-pkgz/notify` from 1.1.1 to 1.2.0 - [Release notes](https://github.com/go-pkgz/notify/releases) - [Commits](https://github.com/go-pkgz/notify/compare/v1.1.1...v1.2.0) Updates `github.com/go-pkgz/repeater` from 1.1.3 to 1.2.0 - [Release notes](https://github.com/go-pkgz/repeater/releases) - [Commits](https://github.com/go-pkgz/repeater/compare/v1.1.3...v1.2.0) Updates `github.com/rs/xid` from 1.5.0 to 1.6.0 - [Release notes](https://github.com/rs/xid/releases) - [Commits](https://github.com/rs/xid/compare/v1.5.0...v1.6.0) Updates `go.etcd.io/bbolt` from 1.3.10 to 1.3.11 - [Release notes](https://github.com/etcd-io/bbolt/releases) - [Commits](https://github.com/etcd-io/bbolt/compare/v1.3.10...v1.3.11) Updates `golang.org/x/crypto` from 0.25.0 to 0.27.0 - [Commits](https://github.com/golang/crypto/compare/v0.25.0...v0.27.0) Updates `golang.org/x/image` from 0.18.0 to 0.22.0 - [Commits](https://github.com/golang/image/compare/v0.18.0...v0.22.0) Updates `golang.org/x/net` from 0.27.0 to 0.29.0 - [Commits](https://github.com/golang/net/compare/v0.27.0...v0.29.0) --- updated-dependencies: - dependency-name: github.com/PuerkitoBio/goquery dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/alecthomas/chroma/v2 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/go-pkgz/notify dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/go-pkgz/repeater dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/rs/xid dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: go.etcd.io/bbolt dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: golang.org/x/crypto dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: golang.org/x/image dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: golang.org/x/net dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates ... Signed-off-by: dependabot[bot] <support@github.com>
270 lines
7.0 KiB
Go
270 lines
7.0 KiB
Go
package slack
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
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.
|
|
func (api *Client) AddStar(channel string, item ItemRef) error {
|
|
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
|
|
func (api *Client) AddStarContext(ctx context.Context, channel string, item ItemRef) error {
|
|
values := url.Values{
|
|
"channel": {channel},
|
|
"token": {api.token},
|
|
}
|
|
if item.Timestamp != "" {
|
|
values.Set("timestamp", item.Timestamp)
|
|
}
|
|
if item.File != "" {
|
|
values.Set("file", item.File)
|
|
}
|
|
if item.Comment != "" {
|
|
values.Set("file_comment", item.Comment)
|
|
}
|
|
|
|
response := &SlackResponse{}
|
|
if err := api.postMethod(ctx, "stars.add", values, response); err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Err()
|
|
}
|
|
|
|
// RemoveStar removes a starred item from a channel.
|
|
// For more information see the RemoveStarContext documentation.
|
|
func (api *Client) RemoveStar(channel string, item ItemRef) error {
|
|
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
|
|
func (api *Client) RemoveStarContext(ctx context.Context, channel string, item ItemRef) error {
|
|
values := url.Values{
|
|
"channel": {channel},
|
|
"token": {api.token},
|
|
}
|
|
if item.Timestamp != "" {
|
|
values.Set("timestamp", item.Timestamp)
|
|
}
|
|
if item.File != "" {
|
|
values.Set("file", item.File)
|
|
}
|
|
if item.Comment != "" {
|
|
values.Set("file_comment", item.Comment)
|
|
}
|
|
|
|
response := &SlackResponse{}
|
|
if err := api.postMethod(ctx, "stars.remove", values, response); err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Err()
|
|
}
|
|
|
|
// ListStars returns information about the stars a user added.
|
|
// For more information see the ListStarsContext documentation.
|
|
func (api *Client) ListStars(params StarsParameters) ([]Item, *Paging, error) {
|
|
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
|
|
func (api *Client) ListStarsContext(ctx context.Context, params StarsParameters) ([]Item, *Paging, error) {
|
|
values := url.Values{
|
|
"token": {api.token},
|
|
}
|
|
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))
|
|
}
|
|
|
|
response := &listResponseFull{}
|
|
err := api.postMethod(ctx, "stars.list", values, response)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
if err := response.Err(); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
return response.Items, &response.Paging, nil
|
|
}
|
|
|
|
// GetStarred returns a list of StarredItem items.
|
|
//
|
|
// The user then has to iterate over them and figure out what they should
|
|
// 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":
|
|
// ...
|
|
// }
|
|
//
|
|
// This function still exists to maintain backwards compatibility.
|
|
// I exposed it as returning []StarredItem, so it shall stay as StarredItem.
|
|
func (api *Client) GetStarred(params StarsParameters) ([]StarredItem, *Paging, error) {
|
|
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)
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|