Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions examples/reactions/reactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ func main() {
}

// Get all reactions on the message.
msgReactions, err := api.GetReactions(msgRef, slack.NewGetReactionsParameters())
msgReactionsResp, err := api.GetReactions(msgRef, slack.NewGetReactionsParameters())
if err != nil {
fmt.Printf("Error getting reactions: %s\n", err)
return
}
fmt.Printf("\n")
fmt.Printf("%d reactions to message...\n", len(msgReactions))
for _, r := range msgReactions {
fmt.Printf("%d reactions to message...\n", len(msgReactionsResp.Reactions))
for _, r := range msgReactionsResp.Reactions {
fmt.Printf(" %d users say %s\n", r.Count, r.Name)
}

Expand All @@ -111,14 +111,14 @@ func main() {
}

// Get all reactions on the message.
msgReactions, err = api.GetReactions(msgRef, slack.NewGetReactionsParameters())
msgReactionsResp, err = api.GetReactions(msgRef, slack.NewGetReactionsParameters())
if err != nil {
fmt.Printf("Error getting reactions: %s\n", err)
return
}
fmt.Printf("\n")
fmt.Printf("%d reactions to message after removing cry...\n", len(msgReactions))
for _, r := range msgReactions {
fmt.Printf("%d reactions to message after removing cry...\n", len(msgReactionsResp.Reactions))
for _, r := range msgReactionsResp.Reactions {
fmt.Printf(" %d users say %s\n", r.Count, r.Name)
}
}
38 changes: 24 additions & 14 deletions reactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,37 @@ func NewGetReactionsParameters() GetReactionsParameters {
type getReactionsResponseFull struct {
Type string
M struct {
Reactions []ItemReaction
*Message // message structure already contains reactions
} `json:"message"`
F struct {
*File
Reactions []ItemReaction
} `json:"file"`
FC struct {
*Comment
Reactions []ItemReaction
} `json:"comment"`
SlackResponse
}

func (res getReactionsResponseFull) extractReactions() []ItemReaction {
switch res.Type {
func (res getReactionsResponseFull) extractReactedItem() ReactedItem {
item := ReactedItem{}
item.Type = res.Type

switch item.Type {
case "message":
return res.M.Reactions
item.Channel = res.M.Channel
item.Message = res.M.Message
item.Reactions = res.M.Reactions
case "file":
return res.F.Reactions
item.File = res.F.File
item.Reactions = res.F.Reactions
case "file_comment":
return res.FC.Reactions
item.File = res.F.File
item.Comment = res.FC.Comment
item.Reactions = res.FC.Reactions
}
return []ItemReaction{}
return item
}

const (
Expand Down Expand Up @@ -200,15 +210,15 @@ func (api *Client) RemoveReactionContext(ctx context.Context, name string, item
return response.Err()
}

// GetReactions returns details about the reactions on an item.
// GetReactions returns item and details about the reactions on an item.
// For more details, see GetReactionsContext documentation.
func (api *Client) GetReactions(item ItemRef, params GetReactionsParameters) ([]ItemReaction, error) {
func (api *Client) GetReactions(item ItemRef, params GetReactionsParameters) (ReactedItem, error) {
return api.GetReactionsContext(context.Background(), item, params)
}

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

response := &getReactionsResponseFull{}
if err := api.postMethod(ctx, "reactions.get", values, response); err != nil {
return nil, err
return ReactedItem{}, err
}

if err := response.Err(); err != nil {
return nil, err
return ReactedItem{}, err
}

return response.extractReactions(), nil
return response.extractReactedItem(), nil
}

// ListReactions returns information about the items a user reacted to.
Expand Down
189 changes: 156 additions & 33 deletions reactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ func TestSlack_GetReactions(t *testing.T) {
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
tests := []struct {
ref ItemRef
params GetReactionsParameters
wantParams map[string]string
json string
wantReactions []ItemReaction
ref ItemRef
params GetReactionsParameters
wantParams map[string]string
json string
wantReactedItem ReactedItem
}{
{
NewRefToMessage("ChannelID", "123"),
Expand All @@ -153,24 +153,40 @@ func TestSlack_GetReactions(t *testing.T) {
"timestamp": "123",
},
`{"ok": true,
"type": "message",
"message": {
"reactions": [
{
"name": "astonished",
"count": 3,
"users": [ "U1", "U2", "U3" ]
},
{
"name": "clock1",
"count": 3,
"users": [ "U1", "U2" ]
}
]
}}`,
[]ItemReaction{
{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
"type": "message",
"message": {
"channel": "ChannelID",
"text": "lorem ipsum dolor sit amet",
"ts": "123",
"user": "U2147483828",
"reactions": [
{
"name": "astonished",
"count": 3,
"users": [ "U1", "U2", "U3" ]
},
{
"name": "clock1",
"count": 3,
"users": [ "U1", "U2" ]
}
]
}}`,
ReactedItem{
Item: Item{
Type: "message", Message: &Message{
Msg: Msg{
Text: "lorem ipsum dolor sit amet",
Channel: "ChannelID",
User: "U2147483828",
Timestamp: "123",
},
},
},
Reactions: []ItemReaction{
{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
},
},
},
{
Expand All @@ -183,6 +199,19 @@ func TestSlack_GetReactions(t *testing.T) {
`{"ok": true,
"type": "file",
"file": {
"id": "F0A12BCDE",
"created": 1531763342,
"timestamp": 1531763342,
"name": "tedair.gif",
"title": "tedair.gif",
"mimetype": "image/gif",
"filetype": "gif",
"pretty_type": "GIF",
"user": "U012A3BCD",
"editable": false,
"size": 137531,
"mode": "hosted",
"is_external": false,
"reactions": [
{
"name": "astonished",
Expand All @@ -196,22 +225,48 @@ func TestSlack_GetReactions(t *testing.T) {
}
]
}}`,
[]ItemReaction{
{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
ReactedItem{
Item: Item{
Type: "file", File: &File{
Name: "tedair.gif",
ID: "F0A12BCDE",
Created: 1531763342,
Timestamp: 1531763342,
User: "U012A3BCD",
Editable: false,
Size: 137531,
},
},
Reactions: []ItemReaction{
{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
},
},
},
{

NewRefToComment("FileCommentID"),
GetReactionsParameters{},
map[string]string{
"file_comment": "FileCommentID",
},
`{"ok": true,
"type": "file_comment",
"file": {},
"file": {
"id": "F0A12BCDE",
"created": 1531763342,
"timestamp": 1531763342,
"name": "tedair.gif",
"title": "tedair.gif",
"mimetype": "image/gif",
"filetype": "gif",
"pretty_type": "GIF",
"user": "U012A3BCD",
"editable": false,
"size": 137531,
"is_external": false
},
"comment": {
"comment": "lorem ipsum dolor sit amet comment",
"reactions": [
{
"name": "astonished",
Expand All @@ -225,9 +280,19 @@ func TestSlack_GetReactions(t *testing.T) {
}
]
}}`,
[]ItemReaction{
{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
ReactedItem{
Item: Item{
Type: "file_comment", File: &File{
Name: "tedair.gif",
},
Comment: &Comment{
Comment: "lorem ipsum dolor sit amet comment",
},
},
Reactions: []ItemReaction{
{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
},
},
},
}
Expand All @@ -240,12 +305,70 @@ func TestSlack_GetReactions(t *testing.T) {
if err != nil {
t.Fatalf("%d: Unexpected error: %s", i, err)
}
if !reflect.DeepEqual(got, test.wantReactions) {
t.Errorf("%d: Got reaction %#v, want %#v", i, got, test.wantReactions)
if !reflect.DeepEqual(got.Reactions, test.wantReactedItem.Reactions) {
t.Errorf("%d: Got reaction %#v, want %#v", i, got.Reactions, test.wantReactedItem.Reactions)
}
if !reflect.DeepEqual(rh.gotParams, test.wantParams) {
t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams)
}

switch got.Type {
case "message":
if got.Message == nil {
t.Fatalf("%d: Got message %#v, want %#v", i, got.Message, test.wantReactedItem.Message)
}

if got.Message.Text != test.wantReactedItem.Message.Text {
t.Errorf("%d: Got message text %#v, want %#v", i, got.Message.Text, test.wantReactedItem.Message.Text)
}
if got.Message.Channel != test.wantReactedItem.Message.Channel {
t.Errorf("%d: Got message channel %#v, want %#v", i, got.Message.Channel, test.wantReactedItem.Message.Channel)
}
if got.Message.User != test.wantReactedItem.Message.User {
t.Errorf("%d: Got message user %#v, want %#v", i, got.Message.User, test.wantReactedItem.Message.User)
}
if got.Message.Timestamp != test.wantReactedItem.Message.Timestamp {
t.Errorf("%d: Got message timestamp %#v, want %#v", i, got.Message.Timestamp, test.wantReactedItem.Message.Timestamp)
}
case "file":
if got.File == nil {
t.Fatalf("%d: Got file %#v, want %#v", i, got.File, test.wantReactedItem.File)
}
if got.File.Name != test.wantReactedItem.File.Name {
t.Errorf("%d: Got file name %#v, want %#v", i, got.File.Name, test.wantReactedItem.File.Name)
}
if got.File.ID != test.wantReactedItem.File.ID {
t.Errorf("%d: Got file ID %#v, want %#v", i, got.File.ID, test.wantReactedItem.File.ID)
}
if got.File.Created != test.wantReactedItem.File.Created {
t.Errorf("%d: Got file created %#v, want %#v", i, got.File.Created, test.wantReactedItem.File.Created)
}
if got.File.Timestamp != test.wantReactedItem.File.Timestamp {
t.Errorf("%d: Got file timestamp %#v, want %#v", i, got.File.Timestamp, test.wantReactedItem.File.Timestamp)
}
if got.File.User != test.wantReactedItem.File.User {
t.Errorf("%d: Got file user %#v, want %#v", i, got.File.User, test.wantReactedItem.File.User)
}
if got.File.Editable != test.wantReactedItem.File.Editable {
t.Errorf("%d: Got file editable %#v, want %#v", i, got.File.Editable, test.wantReactedItem.File.Editable)
}
if got.File.Size != test.wantReactedItem.File.Size {
t.Errorf("%d: Got file size %#v, want %#v", i, got.File.Size, test.wantReactedItem.File.Size)
}
case "file_comment":
if got.Comment == nil {
t.Fatalf("%d: Got comment %#v, want %#v", i, got.Comment, test.wantReactedItem.Comment)
}
if got.File == nil {
t.Fatalf("%d: Got file %#v, want %#v", i, got.File, test.wantReactedItem.File)
}
if got.File.Name != test.wantReactedItem.File.Name {
t.Errorf("%d: Got file name %#v, want %#v", i, got.File.Name, test.wantReactedItem.File.Name)
}
if got.Comment.Comment != test.wantReactedItem.Comment.Comment {
t.Errorf("%d: Got comment comment %#v, want %#v", i, got.Comment.Comment, test.wantReactedItem.Comment.Comment)
}
}
}
}

Expand Down