Files
Dmitry VerkhoturovandUmputun 80c12a3f10 chore(deps): update Go modules
Bump Go dependencies in both backend/ and backend/_example/memory_store.

Notable updates:
- github.com/go-pkgz/lgr v0.12.1 -> v0.12.3
- github.com/klauspost/compress v1.18.2 -> v1.18.5
- github.com/PuerkitoBio/goquery v1.11.0 -> v1.12.0
- github.com/montanaflynn/stats v0.7.1 -> v0.9.0
- github.com/redis/go-redis/v9 v9.17.2 -> v9.18.0
- github.com/slack-go/slack v0.17.3 -> v0.21.1
- go.mongodb.org/mongo-driver v1.17.6 -> v1.17.9
- golang.org/x/crypto v0.48.0 -> v0.50.0
- golang.org/x/net v0.49.0 -> v0.53.0
- golang.org/x/image v0.36.0 -> v0.39.0
- golang.org/x/sys v0.41.0 -> v0.43.0
- golang.org/x/{oauth2,sync,text} minor bumps

Key markdown/sanitisation libs (bluemonday v1.0.27,
alecthomas/chroma/v2 v2.23.1, russross/blackfriday/v2 v2.1.0,
Depado/bfchroma/v2 v2.0.0) are already at the latest available
versions and were not bumped.

Verified the Chroma span-class allowlist regex in
backend/app/store/comment.go:128-131 is still fully in sync with
chroma/v2 types.go StandardTypes map (86 classes, byte-equal after
sorting). The inline comment references commit c263f6f which is
stale (Chroma is at v2 now), but the class list content is current.

Ran `go mod tidy` + `go mod vendor` + full race test suite on both
modules. All green. Added a reminder in CLAUDE.md that updating
backend/ Go modules also requires `go mod tidy` in
backend/_example/memory_store since the example module uses a
local replace directive and inherits indirect deps from the main
module.
2026-04-12 11:52:57 -05:00

270 lines
7.0 KiB
Go

package slack
import (
"context"
"net/url"
"strconv"
"time"
)
const (
DEFAULT_STARS_USER = ""
)
type StarsParameters struct {
User string
Cursor string
Limit int
TeamID string
}
type StarredItem Item
type listResponseFull struct {
Items []Item `json:"items"`
SlackResponse
ResponseMetadata `json:"response_metadata"`
}
// NewStarsParameters initialises StarsParameters with default values
func NewStarsParameters() StarsParameters {
return StarsParameters{
User: DEFAULT_STARS_USER,
}
}
// 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, string, 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, string, error) {
values := url.Values{
"token": {api.token},
}
if params.User != DEFAULT_STARS_USER {
values.Add("user", params.User)
}
if params.Cursor != "" {
values.Add("cursor", params.Cursor)
}
if params.Limit != 0 {
values.Add("limit", strconv.Itoa(params.Limit))
}
if params.TeamID != "" {
values.Add("team_id", params.TeamID)
}
response := &listResponseFull{}
err := api.postMethod(ctx, "stars.list", values, response)
if err != nil {
return nil, "", err
}
if err := response.Err(); err != nil {
return nil, "", err
}
return response.Items, response.ResponseMetadata.Cursor, 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, string, 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, string, error) {
items, nextCursor, err := api.ListStarsContext(ctx, params)
if err != nil {
return nil, "", err
}
starredItems := make([]StarredItem, len(items))
for i, item := range items {
starredItems[i] = StarredItem(item)
}
return starredItems, nextCursor, 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
}