5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-12-27 12:25:40 +00:00
matterbridge/vendor/github.com/nlopes/slack/stars.go

160 lines
4.2 KiB
Go
Raw Normal View History

2016-09-05 14:34:37 +00:00
package slack
import (
2017-07-16 12:29:46 +00:00
"context"
2016-09-05 14:34:37 +00:00
"errors"
"net/url"
"strconv"
)
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
func (api *Client) AddStar(channel string, item ItemRef) error {
2017-07-16 12:29:46 +00:00
return api.AddStarContext(context.Background(), channel, item)
}
// AddStarContext stars an item in a channel with a custom context
func (api *Client) AddStarContext(ctx context.Context, channel string, item ItemRef) error {
2016-09-05 14:34:37 +00:00
values := url.Values{
"channel": {channel},
2018-08-09 22:38:19 +00:00
"token": {api.token},
2016-09-05 14:34:37 +00:00
}
if item.Timestamp != "" {
2018-08-09 22:38:19 +00:00
values.Set("timestamp", item.Timestamp)
2016-09-05 14:34:37 +00:00
}
if item.File != "" {
2018-08-09 22:38:19 +00:00
values.Set("file", item.File)
2016-09-05 14:34:37 +00:00
}
if item.Comment != "" {
2018-08-09 22:38:19 +00:00
values.Set("file_comment", item.Comment)
2016-09-05 14:34:37 +00:00
}
2018-08-09 22:38:19 +00:00
2016-09-05 14:34:37 +00:00
response := &SlackResponse{}
2018-08-09 22:38:19 +00:00
if err := postSlackMethod(ctx, api.httpclient, "stars.add", values, response, api.debug); err != nil {
2016-09-05 14:34:37 +00:00
return err
}
2018-08-09 22:38:19 +00:00
return response.Err()
2016-09-05 14:34:37 +00:00
}
// RemoveStar removes a starred item from a channel
func (api *Client) RemoveStar(channel string, item ItemRef) error {
2017-07-16 12:29:46 +00:00
return api.RemoveStarContext(context.Background(), channel, item)
}
// RemoveStarContext removes a starred item from a channel with a custom context
func (api *Client) RemoveStarContext(ctx context.Context, channel string, item ItemRef) error {
2016-09-05 14:34:37 +00:00
values := url.Values{
"channel": {channel},
2018-08-09 22:38:19 +00:00
"token": {api.token},
2016-09-05 14:34:37 +00:00
}
if item.Timestamp != "" {
2018-08-09 22:38:19 +00:00
values.Set("timestamp", item.Timestamp)
2016-09-05 14:34:37 +00:00
}
if item.File != "" {
2018-08-09 22:38:19 +00:00
values.Set("file", item.File)
2016-09-05 14:34:37 +00:00
}
if item.Comment != "" {
2018-08-09 22:38:19 +00:00
values.Set("file_comment", item.Comment)
2016-09-05 14:34:37 +00:00
}
2018-08-09 22:38:19 +00:00
2016-09-05 14:34:37 +00:00
response := &SlackResponse{}
2018-08-09 22:38:19 +00:00
if err := postSlackMethod(ctx, api.httpclient, "stars.remove", values, response, api.debug); err != nil {
2016-09-05 14:34:37 +00:00
return err
}
2018-08-09 22:38:19 +00:00
return response.Err()
2016-09-05 14:34:37 +00:00
}
// ListStars returns information about the stars a user added
func (api *Client) ListStars(params StarsParameters) ([]Item, *Paging, error) {
2017-07-16 12:29:46 +00:00
return api.ListStarsContext(context.Background(), params)
}
// ListStarsContext returns information about the stars a user added with a custom context
func (api *Client) ListStarsContext(ctx context.Context, params StarsParameters) ([]Item, *Paging, error) {
2016-09-05 14:34:37 +00:00
values := url.Values{
2018-08-09 22:38:19 +00:00
"token": {api.token},
2016-09-05 14:34:37 +00: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 22:38:19 +00:00
2016-09-05 14:34:37 +00:00
response := &listResponseFull{}
2018-08-09 22:38:19 +00:00
err := postSlackMethod(ctx, api.httpclient, "stars.list", values, response, api.debug)
2016-09-05 14:34:37 +00:00
if err != nil {
return nil, nil, err
}
if !response.Ok {
return nil, nil, errors.New(response.Error)
}
return response.Items, &response.Paging, nil
}
2017-07-16 12:29:46 +00:00
// GetStarred returns a list of StarredItem items.
//
// The user then has to iterate over them and figure out what they should
2016-09-05 14:34:37 +00: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":
// ...
//
// }
// 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) {
2017-07-16 12:29:46 +00: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 14:34:37 +00: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
}