5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-10 15:40:30 +00:00
matterbridge/vendor/github.com/nlopes/slack/pins.go

95 lines
2.4 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"
)
type listPinsResponseFull struct {
Items []Item
Paging `json:"paging"`
SlackResponse
}
// AddPin pins an item in a channel
func (api *Client) AddPin(channel string, item ItemRef) error {
2017-07-16 12:29:46 +00:00
return api.AddPinContext(context.Background(), channel, item)
}
// AddPinContext pins an item in a channel with a custom context
func (api *Client) AddPinContext(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-12-01 18:55:35 +00:00
if err := postSlackMethod(ctx, api.httpclient, "pins.add", values, response, api); 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
}
// RemovePin un-pins an item from a channel
func (api *Client) RemovePin(channel string, item ItemRef) error {
2017-07-16 12:29:46 +00:00
return api.RemovePinContext(context.Background(), channel, item)
}
// RemovePinContext un-pins an item from a channel with a custom context
func (api *Client) RemovePinContext(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-12-01 18:55:35 +00:00
if err := postSlackMethod(ctx, api.httpclient, "pins.remove", values, response, api); 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
}
// ListPins returns information about the items a user reacted to.
func (api *Client) ListPins(channel string) ([]Item, *Paging, error) {
2017-07-16 12:29:46 +00:00
return api.ListPinsContext(context.Background(), channel)
}
// ListPinsContext returns information about the items a user reacted to with a custom context.
func (api *Client) ListPinsContext(ctx context.Context, channel string) ([]Item, *Paging, 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
}
2018-08-09 22:38:19 +00:00
2016-09-05 14:34:37 +00:00
response := &listPinsResponseFull{}
2018-12-01 18:55:35 +00:00
err := postSlackMethod(ctx, api.httpclient, "pins.list", values, response, api)
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
}