| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/url" |
| 6 | "strconv" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | const ( |
| 11 | DEFAULT_STARS_USER = "" |
| 12 | DEFAULT_STARS_COUNT = 100 |
| 13 | DEFAULT_STARS_PAGE = 1 |
| 14 | ) |
| 15 | |
| 16 | type StarsParameters struct { |
| 17 | User string |
| 18 | Count int |
| 19 | Page int |
| 20 | } |
| 21 | |
| 22 | type StarredItem Item |
| 23 | |
| 24 | type listResponseFull struct { |
| 25 | Items []Item `json:"items"` |
| 26 | Paging `json:"paging"` |
| 27 | SlackResponse |
| 28 | } |
| 29 | |
| 30 | // NewStarsParameters initialises StarsParameters with default values |
| 31 | func NewStarsParameters() StarsParameters { |
| 32 | return StarsParameters{ |
| 33 | User: DEFAULT_STARS_USER, |
| 34 | Count: DEFAULT_STARS_COUNT, |
| 35 | Page: DEFAULT_STARS_PAGE, |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // AddStar stars an item in a channel |
| 40 | func (api *Client) AddStar(channel string, item ItemRef) error { |
| 41 | return api.AddStarContext(context.Background(), channel, item) |
| 42 | } |
| 43 | |
| 44 | // AddStarContext stars an item in a channel with a custom context |
| 45 | func (api *Client) AddStarContext(ctx context.Context, channel string, item ItemRef) error { |
| 46 | values := url.Values{ |
| 47 | "channel": {channel}, |
| 48 | "token": {api.token}, |
| 49 | } |
| 50 | if item.Timestamp != "" { |
| 51 | values.Set("timestamp", item.Timestamp) |
| 52 | } |
| 53 | if item.File != "" { |
| 54 | values.Set("file", item.File) |
| 55 | } |
| 56 | if item.Comment != "" { |
| 57 | values.Set("file_comment", item.Comment) |
| 58 | } |
| 59 | |
| 60 | response := &SlackResponse{} |
| 61 | if err := api.postMethod(ctx, "stars.add", values, response); err != nil { |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | return response.Err() |
| 66 | } |
| 67 | |
| 68 | // RemoveStar removes a starred item from a channel |
| 69 | func (api *Client) RemoveStar(channel string, item ItemRef) error { |
| 70 | return api.RemoveStarContext(context.Background(), channel, item) |
| 71 | } |
| 72 | |
| 73 | // RemoveStarContext removes a starred item from a channel with a custom context |
| 74 | func (api *Client) RemoveStarContext(ctx context.Context, channel string, item ItemRef) error { |
| 75 | values := url.Values{ |
| 76 | "channel": {channel}, |
| 77 | "token": {api.token}, |
| 78 | } |
| 79 | if item.Timestamp != "" { |
| 80 | values.Set("timestamp", item.Timestamp) |
| 81 | } |
| 82 | if item.File != "" { |
| 83 | values.Set("file", item.File) |
| 84 | } |
| 85 | if item.Comment != "" { |
| 86 | values.Set("file_comment", item.Comment) |
| 87 | } |
| 88 | |
| 89 | response := &SlackResponse{} |
| 90 | if err := api.postMethod(ctx, "stars.remove", values, response); err != nil { |
| 91 | return err |
| 92 | } |
| 93 | |
| 94 | return response.Err() |
| 95 | } |
| 96 | |
| 97 | // ListStars returns information about the stars a user added |
| 98 | func (api *Client) ListStars(params StarsParameters) ([]Item, *Paging, error) { |
| 99 | return api.ListStarsContext(context.Background(), params) |
| 100 | } |
| 101 | |
| 102 | // ListStarsContext returns information about the stars a user added with a custom context |
| 103 | func (api *Client) ListStarsContext(ctx context.Context, params StarsParameters) ([]Item, *Paging, error) { |
| 104 | values := url.Values{ |
| 105 | "token": {api.token}, |
| 106 | } |
| 107 | if params.User != DEFAULT_STARS_USER { |
| 108 | values.Add("user", params.User) |
| 109 | } |
| 110 | if params.Count != DEFAULT_STARS_COUNT { |
| 111 | values.Add("count", strconv.Itoa(params.Count)) |
| 112 | } |
| 113 | if params.Page != DEFAULT_STARS_PAGE { |
| 114 | values.Add("page", strconv.Itoa(params.Page)) |
| 115 | } |
| 116 | |
| 117 | response := &listResponseFull{} |
| 118 | err := api.postMethod(ctx, "stars.list", values, response) |
| 119 | if err != nil { |
| 120 | return nil, nil, err |
| 121 | } |
| 122 | |
| 123 | if err := response.Err(); err != nil { |
| 124 | return nil, nil, err |
| 125 | } |
| 126 | |
| 127 | return response.Items, &response.Paging, nil |
| 128 | } |
| 129 | |
| 130 | // GetStarred returns a list of StarredItem items. |
| 131 | // |
| 132 | // The user then has to iterate over them and figure out what they should |
| 133 | // be looking at according to what is in the Type: |
| 134 | // |
| 135 | // for _, item := range items { |
| 136 | // switch c.Type { |
| 137 | // case "file_comment": |
| 138 | // log.Println(c.Comment) |
| 139 | // case "file": |
| 140 | // ... |
| 141 | // } |
| 142 | // |
| 143 | // This function still exists to maintain backwards compatibility. |
| 144 | // I exposed it as returning []StarredItem, so it shall stay as StarredItem. |
| 145 | func (api *Client) GetStarred(params StarsParameters) ([]StarredItem, *Paging, error) { |
| 146 | return api.GetStarredContext(context.Background(), params) |
| 147 | } |
| 148 | |
| 149 | // GetStarredContext returns a list of StarredItem items with a custom context |
| 150 | // |
| 151 | // For more details see GetStarred |
| 152 | func (api *Client) GetStarredContext(ctx context.Context, params StarsParameters) ([]StarredItem, *Paging, error) { |
| 153 | items, paging, err := api.ListStarsContext(ctx, params) |
| 154 | if err != nil { |
| 155 | return nil, nil, err |
| 156 | } |
| 157 | starredItems := make([]StarredItem, len(items)) |
| 158 | for i, item := range items { |
| 159 | starredItems[i] = StarredItem(item) |
| 160 | } |
| 161 | return starredItems, paging, nil |
| 162 | } |
| 163 | |
| 164 | type listResponsePaginated struct { |
| 165 | Items []Item `json:"items"` |
| 166 | SlackResponse |
| 167 | Metadata ResponseMetadata `json:"response_metadata"` |
| 168 | } |
| 169 | |
| 170 | // StarredItemPagination allows for paginating over the starred items |
| 171 | type StarredItemPagination struct { |
| 172 | Items []Item |
| 173 | limit int |
| 174 | previousResp *ResponseMetadata |
| 175 | c *Client |
| 176 | } |
| 177 | |
| 178 | // ListStarsOption options for the GetUsers method call. |
| 179 | type ListStarsOption func(*StarredItemPagination) |
| 180 | |
| 181 | // ListAllStars returns the complete list of starred items |
| 182 | func (api *Client) ListAllStars() ([]Item, error) { |
| 183 | return api.ListAllStarsContext(context.Background()) |
| 184 | } |
| 185 | |
| 186 | // ListAllStarsContext returns the list of users (with their detailed information) with a custom context |
| 187 | func (api *Client) ListAllStarsContext(ctx context.Context) (results []Item, err error) { |
| 188 | p := api.ListStarsPaginated() |
| 189 | for err == nil { |
| 190 | p, err = p.next(ctx) |
| 191 | if err == nil { |
| 192 | results = append(results, p.Items...) |
| 193 | } else if rateLimitedError, ok := err.(*RateLimitedError); ok { |
| 194 | select { |
| 195 | case <-ctx.Done(): |
| 196 | err = ctx.Err() |
| 197 | case <-time.After(rateLimitedError.RetryAfter): |
| 198 | err = nil |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return results, p.failure(err) |
| 204 | } |
| 205 | |
| 206 | // ListStarsPaginated fetches users in a paginated fashion, see ListStarsPaginationContext for usage. |
| 207 | func (api *Client) ListStarsPaginated(options ...ListStarsOption) StarredItemPagination { |
| 208 | return newStarPagination(api, options...) |
| 209 | } |
| 210 | |
| 211 | func newStarPagination(c *Client, options ...ListStarsOption) (sip StarredItemPagination) { |
| 212 | sip = StarredItemPagination{ |
| 213 | c: c, |
| 214 | limit: 200, // per slack api documentation. |
| 215 | } |
| 216 | |
| 217 | for _, opt := range options { |
| 218 | opt(&sip) |
| 219 | } |
| 220 | |
| 221 | return sip |
| 222 | } |
| 223 | |
| 224 | // done checks if the pagination has completed |
| 225 | func (StarredItemPagination) done(err error) bool { |
| 226 | return err == errPaginationComplete |
| 227 | } |
| 228 | |
| 229 | // done checks if pagination failed. |
| 230 | func (t StarredItemPagination) failure(err error) error { |
| 231 | if t.done(err) { |
| 232 | return nil |
| 233 | } |
| 234 | |
| 235 | return err |
| 236 | } |
| 237 | |
| 238 | // next gets the next list of starred items based on the cursor value |
| 239 | func (t StarredItemPagination) next(ctx context.Context) (_ StarredItemPagination, err error) { |
| 240 | var ( |
| 241 | resp *listResponsePaginated |
| 242 | ) |
| 243 | |
| 244 | if t.c == nil || (t.previousResp != nil && t.previousResp.Cursor == "") { |
| 245 | return t, errPaginationComplete |
| 246 | } |
| 247 | |
| 248 | t.previousResp = t.previousResp.initialize() |
| 249 | |
| 250 | values := url.Values{ |
| 251 | "limit": {strconv.Itoa(t.limit)}, |
| 252 | "token": {t.c.token}, |
| 253 | "cursor": {t.previousResp.Cursor}, |
| 254 | } |
| 255 | |
| 256 | if err = t.c.postMethod(ctx, "stars.list", values, &resp); err != nil { |
| 257 | return t, err |
| 258 | } |
| 259 | |
| 260 | t.previousResp = &resp.Metadata |
| 261 | t.Items = resp.Items |
| 262 | |
| 263 | return t, nil |
| 264 | } |