Skip to content

Commit 9808e43

Browse files
committed
feat: Align GetReactions with Slack API
!BREAKING CHANGE Previously this method returned only reaction details, but with this change it started to also return the item itself (e.g. message, file, file_comment). This makes this closer to the actual response of Slack API. To migrate an existing code, you can just use resp.Reactions to access the slice of []ItemReaction.
1 parent e6bc4a0 commit 9808e43

File tree

3 files changed

+186
-53
lines changed

3 files changed

+186
-53
lines changed

examples/reactions/reactions.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ func main() {
7777
}
7878

7979
// Get all reactions on the message.
80-
msgReactions, err := api.GetReactions(msgRef, slack.NewGetReactionsParameters())
80+
msgReactionsResp, err := api.GetReactions(msgRef, slack.NewGetReactionsParameters())
8181
if err != nil {
8282
fmt.Printf("Error getting reactions: %s\n", err)
8383
return
8484
}
8585
fmt.Printf("\n")
86-
fmt.Printf("%d reactions to message...\n", len(msgReactions))
87-
for _, r := range msgReactions {
86+
fmt.Printf("%d reactions to message...\n", len(msgReactionsResp.Reactions))
87+
for _, r := range msgReactionsResp.Reactions {
8888
fmt.Printf(" %d users say %s\n", r.Count, r.Name)
8989
}
9090

@@ -111,14 +111,14 @@ func main() {
111111
}
112112

113113
// Get all reactions on the message.
114-
msgReactions, err = api.GetReactions(msgRef, slack.NewGetReactionsParameters())
114+
msgReactionsResp, err = api.GetReactions(msgRef, slack.NewGetReactionsParameters())
115115
if err != nil {
116116
fmt.Printf("Error getting reactions: %s\n", err)
117117
return
118118
}
119119
fmt.Printf("\n")
120-
fmt.Printf("%d reactions to message after removing cry...\n", len(msgReactions))
121-
for _, r := range msgReactions {
120+
fmt.Printf("%d reactions to message after removing cry...\n", len(msgReactionsResp.Reactions))
121+
for _, r := range msgReactionsResp.Reactions {
122122
fmt.Printf(" %d users say %s\n", r.Count, r.Name)
123123
}
124124
}

reactions.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,37 @@ func NewGetReactionsParameters() GetReactionsParameters {
3535
type getReactionsResponseFull struct {
3636
Type string
3737
M struct {
38-
Reactions []ItemReaction
38+
*Message // message structure already contains reactions
3939
} `json:"message"`
4040
F struct {
41+
*File
4142
Reactions []ItemReaction
4243
} `json:"file"`
4344
FC struct {
45+
*Comment
4446
Reactions []ItemReaction
4547
} `json:"comment"`
4648
SlackResponse
4749
}
4850

49-
func (res getReactionsResponseFull) extractReactions() []ItemReaction {
50-
switch res.Type {
51+
func (res getReactionsResponseFull) extractReactedItem() ReactedItem {
52+
item := ReactedItem{}
53+
item.Type = res.Type
54+
55+
switch item.Type {
5156
case "message":
52-
return res.M.Reactions
57+
item.Channel = res.M.Channel
58+
item.Message = res.M.Message
59+
item.Reactions = res.M.Reactions
5360
case "file":
54-
return res.F.Reactions
61+
item.File = res.F.File
62+
item.Reactions = res.F.Reactions
5563
case "file_comment":
56-
return res.FC.Reactions
64+
item.File = res.F.File
65+
item.Comment = res.FC.Comment
66+
item.Reactions = res.FC.Reactions
5767
}
58-
return []ItemReaction{}
68+
return item
5969
}
6070

6171
const (
@@ -200,15 +210,15 @@ func (api *Client) RemoveReactionContext(ctx context.Context, name string, item
200210
return response.Err()
201211
}
202212

203-
// GetReactions returns details about the reactions on an item.
213+
// GetReactions returns item and details about the reactions on an item.
204214
// For more details, see GetReactionsContext documentation.
205-
func (api *Client) GetReactions(item ItemRef, params GetReactionsParameters) ([]ItemReaction, error) {
215+
func (api *Client) GetReactions(item ItemRef, params GetReactionsParameters) (ReactedItem, error) {
206216
return api.GetReactionsContext(context.Background(), item, params)
207217
}
208218

209-
// GetReactionsContext returns details about the reactions on an item with a custom context.
219+
// GetReactionsContext returns item and details about the reactions on an item with a custom context.
210220
// Slack API docs: https://api.slack.com/methods/reactions.get
211-
func (api *Client) GetReactionsContext(ctx context.Context, item ItemRef, params GetReactionsParameters) ([]ItemReaction, error) {
221+
func (api *Client) GetReactionsContext(ctx context.Context, item ItemRef, params GetReactionsParameters) (ReactedItem, error) {
212222
values := url.Values{
213223
"token": {api.token},
214224
}
@@ -230,14 +240,14 @@ func (api *Client) GetReactionsContext(ctx context.Context, item ItemRef, params
230240

231241
response := &getReactionsResponseFull{}
232242
if err := api.postMethod(ctx, "reactions.get", values, response); err != nil {
233-
return nil, err
243+
return ReactedItem{}, err
234244
}
235245

236246
if err := response.Err(); err != nil {
237-
return nil, err
247+
return ReactedItem{}, err
238248
}
239249

240-
return response.extractReactions(), nil
250+
return response.extractReactedItem(), nil
241251
}
242252

243253
// ListReactions returns information about the items a user reacted to.

reactions_test.go

Lines changed: 156 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ func TestSlack_GetReactions(t *testing.T) {
139139
once.Do(startServer)
140140
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
141141
tests := []struct {
142-
ref ItemRef
143-
params GetReactionsParameters
144-
wantParams map[string]string
145-
json string
146-
wantReactions []ItemReaction
142+
ref ItemRef
143+
params GetReactionsParameters
144+
wantParams map[string]string
145+
json string
146+
wantReactedItem ReactedItem
147147
}{
148148
{
149149
NewRefToMessage("ChannelID", "123"),
@@ -153,24 +153,40 @@ func TestSlack_GetReactions(t *testing.T) {
153153
"timestamp": "123",
154154
},
155155
`{"ok": true,
156-
"type": "message",
157-
"message": {
158-
"reactions": [
159-
{
160-
"name": "astonished",
161-
"count": 3,
162-
"users": [ "U1", "U2", "U3" ]
163-
},
164-
{
165-
"name": "clock1",
166-
"count": 3,
167-
"users": [ "U1", "U2" ]
168-
}
169-
]
170-
}}`,
171-
[]ItemReaction{
172-
{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
173-
{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
156+
"type": "message",
157+
"message": {
158+
"channel": "ChannelID",
159+
"text": "lorem ipsum dolor sit amet",
160+
"ts": "123",
161+
"user": "U2147483828",
162+
"reactions": [
163+
{
164+
"name": "astonished",
165+
"count": 3,
166+
"users": [ "U1", "U2", "U3" ]
167+
},
168+
{
169+
"name": "clock1",
170+
"count": 3,
171+
"users": [ "U1", "U2" ]
172+
}
173+
]
174+
}}`,
175+
ReactedItem{
176+
Item: Item{
177+
Type: "message", Message: &Message{
178+
Msg: Msg{
179+
Text: "lorem ipsum dolor sit amet",
180+
Channel: "ChannelID",
181+
User: "U2147483828",
182+
Timestamp: "123",
183+
},
184+
},
185+
},
186+
Reactions: []ItemReaction{
187+
{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
188+
{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
189+
},
174190
},
175191
},
176192
{
@@ -183,6 +199,19 @@ func TestSlack_GetReactions(t *testing.T) {
183199
`{"ok": true,
184200
"type": "file",
185201
"file": {
202+
"id": "F0A12BCDE",
203+
"created": 1531763342,
204+
"timestamp": 1531763342,
205+
"name": "tedair.gif",
206+
"title": "tedair.gif",
207+
"mimetype": "image/gif",
208+
"filetype": "gif",
209+
"pretty_type": "GIF",
210+
"user": "U012A3BCD",
211+
"editable": false,
212+
"size": 137531,
213+
"mode": "hosted",
214+
"is_external": false,
186215
"reactions": [
187216
{
188217
"name": "astonished",
@@ -196,22 +225,48 @@ func TestSlack_GetReactions(t *testing.T) {
196225
}
197226
]
198227
}}`,
199-
[]ItemReaction{
200-
{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
201-
{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
228+
ReactedItem{
229+
Item: Item{
230+
Type: "file", File: &File{
231+
Name: "tedair.gif",
232+
ID: "F0A12BCDE",
233+
Created: 1531763342,
234+
Timestamp: 1531763342,
235+
User: "U012A3BCD",
236+
Editable: false,
237+
Size: 137531,
238+
},
239+
},
240+
Reactions: []ItemReaction{
241+
{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
242+
{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
243+
},
202244
},
203245
},
204246
{
205-
206247
NewRefToComment("FileCommentID"),
207248
GetReactionsParameters{},
208249
map[string]string{
209250
"file_comment": "FileCommentID",
210251
},
211252
`{"ok": true,
212253
"type": "file_comment",
213-
"file": {},
254+
"file": {
255+
"id": "F0A12BCDE",
256+
"created": 1531763342,
257+
"timestamp": 1531763342,
258+
"name": "tedair.gif",
259+
"title": "tedair.gif",
260+
"mimetype": "image/gif",
261+
"filetype": "gif",
262+
"pretty_type": "GIF",
263+
"user": "U012A3BCD",
264+
"editable": false,
265+
"size": 137531,
266+
"is_external": false
267+
},
214268
"comment": {
269+
"comment": "lorem ipsum dolor sit amet comment",
215270
"reactions": [
216271
{
217272
"name": "astonished",
@@ -225,9 +280,19 @@ func TestSlack_GetReactions(t *testing.T) {
225280
}
226281
]
227282
}}`,
228-
[]ItemReaction{
229-
{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
230-
{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
283+
ReactedItem{
284+
Item: Item{
285+
Type: "file_comment", File: &File{
286+
Name: "tedair.gif",
287+
},
288+
Comment: &Comment{
289+
Comment: "lorem ipsum dolor sit amet comment",
290+
},
291+
},
292+
Reactions: []ItemReaction{
293+
{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
294+
{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
295+
},
231296
},
232297
},
233298
}
@@ -240,12 +305,70 @@ func TestSlack_GetReactions(t *testing.T) {
240305
if err != nil {
241306
t.Fatalf("%d: Unexpected error: %s", i, err)
242307
}
243-
if !reflect.DeepEqual(got, test.wantReactions) {
244-
t.Errorf("%d: Got reaction %#v, want %#v", i, got, test.wantReactions)
308+
if !reflect.DeepEqual(got.Reactions, test.wantReactedItem.Reactions) {
309+
t.Errorf("%d: Got reaction %#v, want %#v", i, got.Reactions, test.wantReactedItem.Reactions)
245310
}
246311
if !reflect.DeepEqual(rh.gotParams, test.wantParams) {
247312
t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams)
248313
}
314+
315+
switch got.Type {
316+
case "message":
317+
if got.Message == nil {
318+
t.Fatalf("%d: Got message %#v, want %#v", i, got.Message, test.wantReactedItem.Message)
319+
}
320+
321+
if got.Message.Text != test.wantReactedItem.Message.Text {
322+
t.Errorf("%d: Got message text %#v, want %#v", i, got.Message.Text, test.wantReactedItem.Message.Text)
323+
}
324+
if got.Message.Channel != test.wantReactedItem.Message.Channel {
325+
t.Errorf("%d: Got message channel %#v, want %#v", i, got.Message.Channel, test.wantReactedItem.Message.Channel)
326+
}
327+
if got.Message.User != test.wantReactedItem.Message.User {
328+
t.Errorf("%d: Got message user %#v, want %#v", i, got.Message.User, test.wantReactedItem.Message.User)
329+
}
330+
if got.Message.Timestamp != test.wantReactedItem.Message.Timestamp {
331+
t.Errorf("%d: Got message timestamp %#v, want %#v", i, got.Message.Timestamp, test.wantReactedItem.Message.Timestamp)
332+
}
333+
case "file":
334+
if got.File == nil {
335+
t.Fatalf("%d: Got file %#v, want %#v", i, got.File, test.wantReactedItem.File)
336+
}
337+
if got.File.Name != test.wantReactedItem.File.Name {
338+
t.Errorf("%d: Got file name %#v, want %#v", i, got.File.Name, test.wantReactedItem.File.Name)
339+
}
340+
if got.File.ID != test.wantReactedItem.File.ID {
341+
t.Errorf("%d: Got file ID %#v, want %#v", i, got.File.ID, test.wantReactedItem.File.ID)
342+
}
343+
if got.File.Created != test.wantReactedItem.File.Created {
344+
t.Errorf("%d: Got file created %#v, want %#v", i, got.File.Created, test.wantReactedItem.File.Created)
345+
}
346+
if got.File.Timestamp != test.wantReactedItem.File.Timestamp {
347+
t.Errorf("%d: Got file timestamp %#v, want %#v", i, got.File.Timestamp, test.wantReactedItem.File.Timestamp)
348+
}
349+
if got.File.User != test.wantReactedItem.File.User {
350+
t.Errorf("%d: Got file user %#v, want %#v", i, got.File.User, test.wantReactedItem.File.User)
351+
}
352+
if got.File.Editable != test.wantReactedItem.File.Editable {
353+
t.Errorf("%d: Got file editable %#v, want %#v", i, got.File.Editable, test.wantReactedItem.File.Editable)
354+
}
355+
if got.File.Size != test.wantReactedItem.File.Size {
356+
t.Errorf("%d: Got file size %#v, want %#v", i, got.File.Size, test.wantReactedItem.File.Size)
357+
}
358+
case "file_comment":
359+
if got.Comment == nil {
360+
t.Fatalf("%d: Got comment %#v, want %#v", i, got.Comment, test.wantReactedItem.Comment)
361+
}
362+
if got.File == nil {
363+
t.Fatalf("%d: Got file %#v, want %#v", i, got.File, test.wantReactedItem.File)
364+
}
365+
if got.File.Name != test.wantReactedItem.File.Name {
366+
t.Errorf("%d: Got file name %#v, want %#v", i, got.File.Name, test.wantReactedItem.File.Name)
367+
}
368+
if got.Comment.Comment != test.wantReactedItem.Comment.Comment {
369+
t.Errorf("%d: Got comment comment %#v, want %#v", i, got.Comment.Comment, test.wantReactedItem.Comment.Comment)
370+
}
371+
}
249372
}
250373
}
251374

0 commit comments

Comments
 (0)