diff options
author | Wim <wim@42.be> | 2018-01-08 22:41:38 +0100 |
---|---|---|
committer | Wim <wim@42.be> | 2018-01-09 00:07:55 +0100 |
commit | be898b44c3c057c7b70efb83f66b7bbbdd5e9276 (patch) | |
tree | 01c8983913bcae177b5e0e015de9a45abe8369a7 /vendor/github.com/nlopes/slack/stars.go | |
parent | 4828c434439949f62a6e219ab9947c221a06b111 (diff) | |
download | matterbridge-msglm-be898b44c3c057c7b70efb83f66b7bbbdd5e9276.tar.gz matterbridge-msglm-be898b44c3c057c7b70efb83f66b7bbbdd5e9276.tar.bz2 matterbridge-msglm-be898b44c3c057c7b70efb83f66b7bbbdd5e9276.zip |
Update vendor (slack)
Diffstat (limited to 'vendor/github.com/nlopes/slack/stars.go')
-rw-r--r-- | vendor/github.com/nlopes/slack/stars.go | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/vendor/github.com/nlopes/slack/stars.go b/vendor/github.com/nlopes/slack/stars.go new file mode 100644 index 00000000..cf4a4a11 --- /dev/null +++ b/vendor/github.com/nlopes/slack/stars.go @@ -0,0 +1,160 @@ +package slack + +import ( + "context" + "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 { + 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 { + values := url.Values{ + "channel": {channel}, + "token": {api.config.token}, + } + if item.Timestamp != "" { + values.Set("timestamp", string(item.Timestamp)) + } + if item.File != "" { + values.Set("file", string(item.File)) + } + if item.Comment != "" { + values.Set("file_comment", string(item.Comment)) + } + response := &SlackResponse{} + if err := post(ctx, "stars.add", values, response, api.debug); err != nil { + return err + } + if !response.Ok { + return errors.New(response.Error) + } + return nil +} + +// RemoveStar removes a starred item from a channel +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 +func (api *Client) RemoveStarContext(ctx context.Context, channel string, item ItemRef) error { + values := url.Values{ + "channel": {channel}, + "token": {api.config.token}, + } + if item.Timestamp != "" { + values.Set("timestamp", string(item.Timestamp)) + } + if item.File != "" { + values.Set("file", string(item.File)) + } + if item.Comment != "" { + values.Set("file_comment", string(item.Comment)) + } + response := &SlackResponse{} + if err := post(ctx, "stars.remove", values, response, api.debug); err != nil { + return err + } + if !response.Ok { + return errors.New(response.Error) + } + return nil +} + +// ListStars returns information about the stars a user added +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 +func (api *Client) ListStarsContext(ctx context.Context, params StarsParameters) ([]Item, *Paging, error) { + values := url.Values{ + "token": {api.config.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 := post(ctx, "stars.list", values, response, api.debug) + if err != nil { + return nil, nil, err + } + if !response.Ok { + return nil, nil, errors.New(response.Error) + } + 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 +} |