Package list golang-github-nlopes-slack / 4a9ff5f
Add stars.add and stars.remove This also moves pins to use the current spec at api.slack.com. Norberto Lopes 5 years ago
5 changed file(s) with 274 addition(s) and 124 deletion(s). Raw diff Collapse all Expand all
9696 }
9797
9898 // List all of the users pins.
99 listPins, _, err := api.ListPins(channelID, slack.NewListPinsParameters())
99 listPins, _, err := api.ListPins(channelID)
100100 if err != nil {
101101 fmt.Printf("Error listing pins: %s\n", err)
102102 return
22 import (
33 "errors"
44 "net/url"
5 "strconv"
65 )
7
8 const (
9 DEFAULT_PINS_COUNT = 100
10 DEFAULT_PINS_PAGE = 1
11 )
12
13 // ListPinsParameters contains all the optional parameters for the pins.list call
14 type ListPinsParameters struct {
15 Count int
16 Page int
17 }
18
19 // NewListPinsParameters initializes the inputs to find all pins
20 // performed by a user.
21 func NewListPinsParameters() ListPinsParameters {
22 return ListPinsParameters{
23 Count: DEFAULT_PINS_COUNT,
24 Page: DEFAULT_PINS_PAGE,
25 }
26 }
276
287 type listPinsResponseFull struct {
298 Items []Item
8261 }
8362
8463 // ListPins returns information about the items a user reacted to.
85 func (api *Client) ListPins(channel string, params ListPinsParameters) ([]Item, *Paging, error) {
64 func (api *Client) ListPins(channel string) ([]Item, *Paging, error) {
8665 values := url.Values{
8766 "channel": {channel},
8867 "token": {api.config.token},
89 }
90 if params.Count != DEFAULT_PINS_COUNT {
91 values.Add("count", strconv.Itoa(params.Count))
92 }
93 if params.Page != DEFAULT_PINS_PAGE {
94 values.Add("page", strconv.Itoa(params.Page))
9568 }
9669 response := &listPinsResponseFull{}
9770 err := post("pins.list", values, response, api.debug)
201201 }
202202 wantParams := map[string]string{
203203 "channel": "ChannelID",
204 "count": "200",
205 "page": "2",
206 }
207 params := NewListPinsParameters()
208 params.Count = 200
209 params.Page = 2
210 got, paging, err := api.ListPins("ChannelID", params)
204 }
205 got, paging, err := api.ListPins("ChannelID")
211206 if err != nil {
212207 t.Fatalf("Unexpected error: %s", err)
213208 }
1717 Page int
1818 }
1919
20 // StarredItem is an item that has been starred.
21 type StarredItem struct {
22 Item
23 }
20 type StarredItem Item
2421
25 type starsResponseFull struct {
26 Items []StarredItem `json:"items"`
22 type listResponseFull struct {
23 Items []Item `json:"items"`
2724 Paging `json:"paging"`
2825 SlackResponse
2926 }
3027
28 // NewStarsParameters initialises StarsParameters with default values
3129 func NewStarsParameters() StarsParameters {
3230 return StarsParameters{
3331 User: DEFAULT_STARS_USER,
3634 }
3735 }
3836
39 // GetStarred returns a list of StarredItem items. The user then has to iterate over them and figure out what they should
40 // be looking at according to what is in the Type.
41 // for _, item := range items {
42 // switch c.Type {
43 // case "file_comment":
44 // log.Println(c.Comment)
45 // case "file":
46 // ...
47 // }
48 // }
49 func (api *Client) GetStarred(params StarsParameters) ([]StarredItem, *Paging, error) {
37 // AddStar stars an item in a channel
38 func (api *Client) AddStar(channel string, item ItemRef) error {
39 values := url.Values{
40 "channel": {channel},
41 "token": {api.config.token},
42 }
43 if item.Timestamp != "" {
44 values.Set("timestamp", string(item.Timestamp))
45 }
46 if item.File != "" {
47 values.Set("file", string(item.File))
48 }
49 if item.Comment != "" {
50 values.Set("file_comment", string(item.Comment))
51 }
52 response := &SlackResponse{}
53 if err := post("stars.add", values, response, api.debug); err != nil {
54 return err
55 }
56 if !response.Ok {
57 return errors.New(response.Error)
58 }
59 return nil
60 }
61
62 // RemoveStar removes a starred item from a channel
63 func (api *Client) RemoveStar(channel string, item ItemRef) error {
64 values := url.Values{
65 "channel": {channel},
66 "token": {api.config.token},
67 }
68 if item.Timestamp != "" {
69 values.Set("timestamp", string(item.Timestamp))
70 }
71 if item.File != "" {
72 values.Set("file", string(item.File))
73 }
74 if item.Comment != "" {
75 values.Set("file_comment", string(item.Comment))
76 }
77 response := &SlackResponse{}
78 if err := post("stars.remove", values, response, api.debug); err != nil {
79 return err
80 }
81 if !response.Ok {
82 return errors.New(response.Error)
83 }
84 return nil
85 }
86
87 // ListStars returns information about the stars a user added
88 func (api *Client) ListStars(params StarsParameters) ([]Item, *Paging, error) {
5089 values := url.Values{
5190 "token": {api.config.token},
5291 }
5998 if params.Page != DEFAULT_STARS_PAGE {
6099 values.Add("page", strconv.Itoa(params.Page))
61100 }
62 response := &starsResponseFull{}
101 response := &listResponseFull{}
63102 err := post("stars.list", values, response, api.debug)
64103 if err != nil {
65104 return nil, nil, err
69108 }
70109 return response.Items, &response.Paging, nil
71110 }
111
112 // GetStarred returns a list of StarredItem items. The user then has to iterate over them and figure out what they should
113 // be looking at according to what is in the Type.
114 // for _, item := range items {
115 // switch c.Type {
116 // case "file_comment":
117 // log.Println(c.Comment)
118 // case "file":
119 // ...
120 //
121 // }
122 // This function still exists to maintain backwards compatibility.
123 // I exposed it as returning []StarredItem, so it shall stay as StarredItem
124 func (api *Client) GetStarred(params StarsParameters) ([]StarredItem, *Paging, error) {
125 items, paging, err := api.ListStars(params)
126 if err != nil {
127 return nil, nil, err
128 }
129 starredItems := make([]StarredItem, len(items))
130 for i, item := range items {
131 starredItems[i] = StarredItem(item)
132 }
133 return starredItems, paging, nil
134 }
00 package slack
11
22 import (
3 "fmt"
34 "net/http"
45 "reflect"
56 "testing"
2627 func (sh *starsHandler) handler(w http.ResponseWriter, r *http.Request) {
2728 sh.accumulateFormValue("user", r)
2829 sh.accumulateFormValue("count", r)
30 sh.accumulateFormValue("channel", r)
31 sh.accumulateFormValue("file", r)
32 sh.accumulateFormValue("file_comment", r)
2933 sh.accumulateFormValue("page", r)
34 sh.accumulateFormValue("timestamp", r)
3035 w.Header().Set("Content-Type", "application/json")
3136 w.Write([]byte(sh.response))
3237 }
3338
34 func TestSlack_GetStarred(t *testing.T) {
39 func TestSlack_AddStar(t *testing.T) {
3540 once.Do(startServer)
3641 SLACK_API = "http://" + serverAddr + "/"
3742 api := New("testing-token")
3843 tests := []struct {
39 params StarsParameters
40 wantParams map[string]string
41 json string
42 starredItems []StarredItem
43 paging *Paging
44 channel string
45 ref ItemRef
46 wantParams map[string]string
4447 }{
4548 {
46 StarsParameters{
47 User: "U1",
48 Count: 10,
49 Page: 100,
50 },
51 map[string]string{
52 "user": "U1",
53 "count": "10",
54 "page": "100",
55 },
56 `{"ok": true,
49 "ChannelID",
50 NewRefToMessage("ChannelID", "123"),
51 map[string]string{
52 "channel": "ChannelID",
53 "timestamp": "123",
54 },
55 },
56 {
57 "ChannelID",
58 NewRefToFile("FileID"),
59 map[string]string{
60 "channel": "ChannelID",
61 "file": "FileID",
62 },
63 },
64 {
65 "ChannelID",
66 NewRefToComment("FileCommentID"),
67 map[string]string{
68 "channel": "ChannelID",
69 "file_comment": "FileCommentID",
70 },
71 },
72 }
73 var rh *starsHandler
74 http.HandleFunc("/stars.add", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
75 for i, test := range tests {
76 rh = newStarsHandler()
77 err := api.AddStar(test.channel, test.ref)
78 if err != nil {
79 t.Fatalf("%d: Unexpected error: %s", i, err)
80 }
81 if !reflect.DeepEqual(rh.gotParams, test.wantParams) {
82 t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams)
83 }
84 }
85 }
86
87 func TestSlack_RemoveStar(t *testing.T) {
88 once.Do(startServer)
89 SLACK_API = "http://" + serverAddr + "/"
90 api := New("testing-token")
91 tests := []struct {
92 channel string
93 ref ItemRef
94 wantParams map[string]string
95 }{
96 {
97 "ChannelID",
98 NewRefToMessage("ChannelID", "123"),
99 map[string]string{
100 "channel": "ChannelID",
101 "timestamp": "123",
102 },
103 },
104 {
105 "ChannelID",
106 NewRefToFile("FileID"),
107 map[string]string{
108 "channel": "ChannelID",
109 "file": "FileID",
110 },
111 },
112 {
113 "ChannelID",
114 NewRefToComment("FileCommentID"),
115 map[string]string{
116 "channel": "ChannelID",
117 "file_comment": "FileCommentID",
118 },
119 },
120 }
121 var rh *starsHandler
122 http.HandleFunc("/stars.remove", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
123 for i, test := range tests {
124 rh = newStarsHandler()
125 err := api.RemoveStar(test.channel, test.ref)
126 if err != nil {
127 t.Fatalf("%d: Unexpected error: %s", i, err)
128 }
129 if !reflect.DeepEqual(rh.gotParams, test.wantParams) {
130 t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams)
131 }
132 }
133 }
134
135 func TestSlack_ListStars(t *testing.T) {
136 once.Do(startServer)
137 SLACK_API = "http://" + serverAddr + "/"
138 api := New("testing-token")
139 rh := newStarsHandler()
140 http.HandleFunc("/stars.list", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
141 rh.response = `{"ok": true,
57142 "items": [
58143 {
59144 "type": "message",
60 "channel": "C2147483705",
145 "channel": "C1",
61146 "message": {
62 "text": "hello"
147 "text": "hello",
148 "reactions": [
149 {
150 "name": "astonished",
151 "count": 3,
152 "users": [ "U1", "U2", "U3" ]
153 },
154 {
155 "name": "clock1",
156 "count": 3,
157 "users": [ "U1", "U2" ]
158 }
159 ]
63160 }
64161 },
65162 {
66163 "type": "file",
67164 "file": {
68 "name": "toy"
165 "name": "toy",
166 "reactions": [
167 {
168 "name": "clock1",
169 "count": 3,
170 "users": [ "U1", "U2" ]
171 }
172 ]
69173 }
70174 },
71175 {
74178 "name": "toy"
75179 },
76180 "comment": {
77 "comment": "nice"
181 "comment": "cool toy",
182 "reactions": [
183 {
184 "name": "astonished",
185 "count": 3,
186 "users": [ "U1", "U2", "U3" ]
187 }
188 ]
78189 }
79 },
80 {
81 "type": "channel",
82 "channel": "C2147483705"
83 },
84 {
85 "type": "im",
86 "channel": "D1"
87 },
88 {
89 "type": "group",
90 "channel": "G1"
91190 }
92191 ],
93192 "paging": {
95194 "total": 4,
96195 "page": 1,
97196 "pages": 1
98 }}`,
99 []StarredItem{
100 {Item: NewMessageItem("C2147483705", &Message{Msg: Msg{Text: "hello"}})},
101 {Item: NewFileItem(&File{Name: "toy"})},
102 {Item: NewFileCommentItem(&File{Name: "toy"}, &Comment{Comment: "nice"})},
103 {Item: NewChannelItem("C2147483705")},
104 {Item: NewIMItem("D1")},
105 {Item: NewGroupItem("G1")},
106 },
107 &Paging{
108 Count: 100,
109 Total: 4,
110 Page: 1,
111 Pages: 1,
112 },
113 },
114 }
115 var sh *starsHandler
116 http.HandleFunc("/stars.list", func(w http.ResponseWriter, r *http.Request) { sh.handler(w, r) })
117 for i, test := range tests {
118 sh = newStarsHandler()
119 sh.response = test.json
120 responseItems, responsePaging, err := api.GetStarred(test.params)
121 if err != nil {
122 t.Fatalf("%d Unexpected error: %s", i, err)
123 }
124 if !reflect.DeepEqual(sh.gotParams, test.wantParams) {
125 t.Errorf("%d got %v; want %v", i, sh.gotParams, test.wantParams)
126 }
127 if !reflect.DeepEqual(responseItems, test.starredItems) {
128 t.Errorf("%d got %v; want %v", i, responseItems, test.starredItems)
129 }
130 if !reflect.DeepEqual(responsePaging, test.paging) {
131 t.Errorf("%d got %v; want %v", i, responsePaging, test.paging)
132 }
133 }
134 }
197 }}`
198 want := []Item{
199 NewMessageItem("C1", &Message{Msg: Msg{Text: "hello"}}),
200 NewFileItem(&File{Name: "toy"}),
201 NewFileCommentItem(&File{Name: "toy"}, &Comment{Comment: "cool toy"}),
202 }
203 wantStarred := make([]StarredItem, len(want))
204 for i, item := range want {
205 wantStarred[i] = StarredItem(item)
206 }
207 wantParams := map[string]string{
208 "count": "200",
209 "page": "2",
210 }
211 params := NewStarsParameters()
212 params.Count = 200
213 params.Page = 2
214 got, paging, err := api.ListStars(params)
215 if err != nil {
216 t.Fatalf("Unexpected error: %s", err)
217 }
218 if !reflect.DeepEqual(got, want) {
219 t.Errorf("Got Stars %#v, want %#v", got, want)
220 for i, item := range got {
221 fmt.Printf("Item %d, Type: %s\n", i, item.Type)
222 fmt.Printf("Message %#v\n", item.Message)
223 fmt.Printf("File %#v\n", item.File)
224 fmt.Printf("Comment %#v\n", item.Comment)
225 }
226 }
227 if !reflect.DeepEqual(rh.gotParams, wantParams) {
228 t.Errorf("Got params %#v, want %#v", rh.gotParams, wantParams)
229 }
230 if reflect.DeepEqual(paging, Paging{}) {
231 t.Errorf("Want paging data, got empty struct")
232 }
233 // Test GetStarred
234 gotStarred, paging, err := api.GetStarred(params)
235 if err != nil {
236 t.Fatalf("Unexpected error: %s", err)
237 }
238 if !reflect.DeepEqual(gotStarred, wantStarred) {
239 t.Errorf("Got Stars %#v, want %#v", gotStarred, wantStarred)
240 for i, item := range got {
241 fmt.Printf("Item %d, Type: %s\n", i, item.Type)
242 fmt.Printf("Message %#v\n", item.Message)
243 fmt.Printf("File %#v\n", item.File)
244 fmt.Printf("Comment %#v\n", item.Comment)
245 }
246 }
247 if !reflect.DeepEqual(rh.gotParams, wantParams) {
248 t.Errorf("Got params %#v, want %#v", rh.gotParams, wantParams)
249 }
250 if reflect.DeepEqual(paging, Paging{}) {
251 t.Errorf("Want paging data, got empty struct")
252 }
253 }