matterbridge/vendor/github.com/nlopes/slack/pins.go

96 lines
2.5 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},
"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{}
2017-07-16 12:29:46 +00:00
if err := post(ctx, "pins.add", values, response, api.debug); err != nil {
2016-09-05 14:34:37 +00:00
return err
}
if !response.Ok {
return errors.New(response.Error)
}
return nil
}
// 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},
"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{}
2017-07-16 12:29:46 +00:00
if err := post(ctx, "pins.remove", values, response, api.debug); err != nil {
2016-09-05 14:34:37 +00:00
return err
}
if !response.Ok {
return errors.New(response.Error)
}
return nil
}
// 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},
"token": {api.config.token},
}
response := &listPinsResponseFull{}
2017-07-16 12:29:46 +00:00
err := post(ctx, "pins.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
}