Codebase list golang-github-nlopes-slack / f282e598-9350-4d40-ba4b-90cbf2cd4600/v0.4.0 pins.go
f282e598-9350-4d40-ba4b-90cbf2cd4600/v0.4.0

Tree @f282e598-9350-4d40-ba4b-90cbf2cd4600/v0.4.0 (Download .tar.gz)

pins.go @f282e598-9350-4d40-ba4b-90cbf2cd4600/v0.4.0

e297819
 
 
d4176bf
e297819
 
 
 
 
 
 
8e0186d
e297819
 
 
 
d4176bf
 
 
 
 
e297819
 
ba9c8fd
e297819
 
9ef10ad
e297819
 
9ef10ad
e297819
 
9ef10ad
e297819
ba9c8fd
8e0186d
c5b8b2e
e297819
 
2b8f4d3
 
e297819
 
 
 
d4176bf
 
 
 
 
e297819
 
ba9c8fd
e297819
 
9ef10ad
e297819
 
9ef10ad
e297819
 
9ef10ad
e297819
ba9c8fd
8e0186d
c5b8b2e
e297819
 
2b8f4d3
 
e297819
 
 
4a9ff5f
d4176bf
 
 
 
 
e297819
 
ba9c8fd
e297819
ba9c8fd
e297819
c5b8b2e
e297819
 
 
 
 
 
 
 
package slack

import (
	"context"
	"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 {
	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 {
	values := url.Values{
		"channel": {channel},
		"token":   {api.token},
	}
	if item.Timestamp != "" {
		values.Set("timestamp", item.Timestamp)
	}
	if item.File != "" {
		values.Set("file", item.File)
	}
	if item.Comment != "" {
		values.Set("file_comment", item.Comment)
	}

	response := &SlackResponse{}
	if err := postSlackMethod(ctx, api.httpclient, "pins.add", values, response, api.debug); err != nil {
		return err
	}

	return response.Err()
}

// RemovePin un-pins an item from a channel
func (api *Client) RemovePin(channel string, item ItemRef) error {
	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 {
	values := url.Values{
		"channel": {channel},
		"token":   {api.token},
	}
	if item.Timestamp != "" {
		values.Set("timestamp", item.Timestamp)
	}
	if item.File != "" {
		values.Set("file", item.File)
	}
	if item.Comment != "" {
		values.Set("file_comment", item.Comment)
	}

	response := &SlackResponse{}
	if err := postSlackMethod(ctx, api.httpclient, "pins.remove", values, response, api.debug); err != nil {
		return err
	}

	return response.Err()
}

// ListPins returns information about the items a user reacted to.
func (api *Client) ListPins(channel string) ([]Item, *Paging, error) {
	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) {
	values := url.Values{
		"channel": {channel},
		"token":   {api.token},
	}

	response := &listPinsResponseFull{}
	err := postSlackMethod(ctx, api.httpclient, "pins.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
}