diff --git a/examples/reactions/reactions.go b/examples/reactions/reactions.go index 6d2a1c9..4bda31d 100644 --- a/examples/reactions/reactions.go +++ b/examples/reactions/reactions.go @@ -67,22 +67,19 @@ msgRef := slack.NewRefToMessage(channelID, timestamp) // React with :+1: - reactionParams := slack.NewAddReactionParameters("+1", msgRef) - if err := api.AddReaction(reactionParams); err != nil { + if err := api.AddReaction("+1", msgRef); err != nil { fmt.Printf("Error adding reaction: %s\n", err) return } // React with :-1: - reactionParams = slack.NewAddReactionParameters("cry", msgRef) - if err := api.AddReaction(reactionParams); err != nil { + if err := api.AddReaction("cry", msgRef); err != nil { fmt.Printf("Error adding reaction: %s\n", err) return } // Get all reactions on the message. - getReactionsParams := slack.NewGetReactionsParameters(msgRef) - msgReactions, err := api.GetReactions(getReactionsParams) + msgReactions, err := api.GetReactions(msgRef, slack.NewGetReactionsParameters()) if err != nil { fmt.Printf("Error getting reactions: %s\n", err) return @@ -94,8 +91,7 @@ } // List all of the users reactions. - listReactionsParams := slack.NewListReactionsParameters(postAsUserID) - listReactions, _, err := api.ListReactions(listReactionsParams) + listReactions, _, err := api.ListReactions(slack.NewListReactionsParameters()) if err != nil { fmt.Printf("Error listing reactions: %s\n", err) return @@ -110,16 +106,14 @@ } // Remove the :cry: reaction. - removeReactionParams := slack.NewRemoveReactionParameters("cry", msgRef) - err = api.RemoveReaction(removeReactionParams) + err = api.RemoveReaction("cry", msgRef) if err != nil { fmt.Printf("Error remove reaction: %s\n", err) return } // Get all reactions on the message. - getReactionsParams = slack.NewGetReactionsParameters(msgRef) - msgReactions, err = api.GetReactions(getReactionsParams) + msgReactions, err = api.GetReactions(msgRef, slack.NewGetReactionsParameters()) if err != nil { fmt.Printf("Error getting reactions: %s\n", err) return diff --git a/reactions.go b/reactions.go index d2b55ac..9acfcc5 100644 --- a/reactions.go +++ b/reactions.go @@ -23,37 +23,16 @@ Reactions []ItemReaction } -// AddReactionParameters is the inputs to create a new reaction. -type AddReactionParameters struct { - Name string - ItemRef -} - -// NewAddReactionParameters initialies the inputs to react to an item. -func NewAddReactionParameters(name string, ref ItemRef) AddReactionParameters { - return AddReactionParameters{Name: name, ItemRef: ref} -} - -// RemoveReactionParameters is the inputs to remove an existing reaction. -type RemoveReactionParameters struct { - Name string - ItemRef -} - -// NewAddReactionParameters initialies the inputs to react to an item. -func NewRemoveReactionParameters(name string, ref ItemRef) RemoveReactionParameters { - return RemoveReactionParameters{Name: name, ItemRef: ref} -} - // GetReactionsParameters is the inputs to get reactions to an item. type GetReactionsParameters struct { Full bool - ItemRef } // NewGetReactionsParameters initializes the inputs to get reactions to an item. -func NewGetReactionsParameters(ref ItemRef) GetReactionsParameters { - return GetReactionsParameters{ItemRef: ref} +func NewGetReactionsParameters() GetReactionsParameters { + return GetReactionsParameters{ + Full: false, + } } type getReactionsResponseFull struct { @@ -91,20 +70,20 @@ // ListReactionsParameters is the inputs to find all reactions by a user. type ListReactionsParameters struct { - User string - Count int - Page int - Full bool + UserId string + Count int + Page int + Full bool } // NewListReactionsParameters initializes the inputs to find all reactions // performed by a user. -func NewListReactionsParameters(userID string) ListReactionsParameters { +func NewListReactionsParameters() ListReactionsParameters { return ListReactionsParameters{ - User: userID, - Count: DEFAULT_REACTIONS_COUNT, - Page: DEFAULT_REACTIONS_PAGE, - Full: DEFAULT_REACTIONS_FULL, + UserId: DEFAULT_REACTIONS_USERID, + Count: DEFAULT_REACTIONS_COUNT, + Page: DEFAULT_REACTIONS_PAGE, + Full: DEFAULT_REACTIONS_FULL, } } @@ -151,24 +130,24 @@ } // AddReaction adds a reaction emoji to a message, file or file comment. -func (api *Slack) AddReaction(params AddReactionParameters) error { - values := url.Values{ - "token": {api.config.token}, - } - if params.Name != "" { - values.Set("name", params.Name) - } - if params.ChannelId != "" { - values.Set("channel", string(params.ChannelId)) - } - if params.Timestamp != "" { - values.Set("timestamp", string(params.Timestamp)) - } - if params.FileId != "" { - values.Set("file", string(params.FileId)) - } - if params.FileCommentId != "" { - values.Set("file_comment", string(params.FileCommentId)) +func (api *Slack) AddReaction(name string, item ItemRef) error { + values := url.Values{ + "token": {api.config.token}, + } + if name != "" { + values.Set("name", name) + } + if item.ChannelId != "" { + values.Set("channel", string(item.ChannelId)) + } + if item.Timestamp != "" { + values.Set("timestamp", string(item.Timestamp)) + } + if item.FileId != "" { + values.Set("file", string(item.FileId)) + } + if item.FileCommentId != "" { + values.Set("file_comment", string(item.FileCommentId)) } response := &SlackResponse{} if err := parseResponse("reactions.add", values, response, api.debug); err != nil { @@ -181,24 +160,24 @@ } // RemoveReaction removes a reaction emoji from a message, file or file comment. -func (api *Slack) RemoveReaction(params RemoveReactionParameters) error { - values := url.Values{ - "token": {api.config.token}, - } - if params.Name != "" { - values.Set("name", params.Name) - } - if params.ChannelId != "" { - values.Set("channel", string(params.ChannelId)) - } - if params.Timestamp != "" { - values.Set("timestamp", string(params.Timestamp)) - } - if params.FileId != "" { - values.Set("file", string(params.FileId)) - } - if params.FileCommentId != "" { - values.Set("file_comment", string(params.FileCommentId)) +func (api *Slack) RemoveReaction(name string, item ItemRef) error { + values := url.Values{ + "token": {api.config.token}, + } + if name != "" { + values.Set("name", name) + } + if item.ChannelId != "" { + values.Set("channel", string(item.ChannelId)) + } + if item.Timestamp != "" { + values.Set("timestamp", string(item.Timestamp)) + } + if item.FileId != "" { + values.Set("file", string(item.FileId)) + } + if item.FileCommentId != "" { + values.Set("file_comment", string(item.FileCommentId)) } response := &SlackResponse{} if err := parseResponse("reactions.remove", values, response, api.debug); err != nil { @@ -211,21 +190,21 @@ } // GetReactions returns details about the reactions on an item. -func (api *Slack) GetReactions(params GetReactionsParameters) ([]ItemReaction, error) { - values := url.Values{ - "token": {api.config.token}, - } - if params.ChannelId != "" { - values.Set("channel", string(params.ChannelId)) - } - if params.Timestamp != "" { - values.Set("timestamp", string(params.Timestamp)) - } - if params.FileId != "" { - values.Set("file", string(params.FileId)) - } - if params.FileCommentId != "" { - values.Set("file_comment", string(params.FileCommentId)) +func (api *Slack) GetReactions(item ItemRef, params GetReactionsParameters) ([]ItemReaction, error) { + values := url.Values{ + "token": {api.config.token}, + } + if item.ChannelId != "" { + values.Set("channel", string(item.ChannelId)) + } + if item.Timestamp != "" { + values.Set("timestamp", string(item.Timestamp)) + } + if item.FileId != "" { + values.Set("file", string(item.FileId)) + } + if item.FileCommentId != "" { + values.Set("file_comment", string(item.FileCommentId)) } if params.Full != DEFAULT_REACTIONS_FULL { values.Set("full", fmt.Sprintf("%t", params.Full)) @@ -245,8 +224,8 @@ values := url.Values{ "token": {api.config.token}, } - if params.User != DEFAULT_REACTIONS_USERID { - values.Add("user", params.User) + if params.UserId != DEFAULT_REACTIONS_USERID { + values.Add("user", params.UserId) } if params.Count != DEFAULT_REACTIONS_COUNT { values.Add("count", fmt.Sprintf("%d", params.Count)) diff --git a/reactions_test.go b/reactions_test.go index 589f046..c86c009 100644 --- a/reactions_test.go +++ b/reactions_test.go @@ -44,11 +44,13 @@ SLACK_API = "http://" + serverAddr + "/" api := New("testing-token") tests := []struct { - params AddReactionParameters + name string + ref ItemRef wantParams map[string]string }{ { - NewAddReactionParameters("thumbsup", NewRefToMessage("ChannelID", "123")), + "thumbsup", + NewRefToMessage("ChannelID", "123"), map[string]string{ "name": "thumbsup", "channel": "ChannelID", @@ -56,14 +58,16 @@ }, }, { - NewAddReactionParameters("thumbsup", NewRefToFile("FileID")), + "thumbsup", + NewRefToFile("FileID"), map[string]string{ "name": "thumbsup", "file": "FileID", }, }, { - NewAddReactionParameters("thumbsup", NewRefToFileComment("FileCommentID")), + "thumbsup", + NewRefToFileComment("FileCommentID"), map[string]string{ "name": "thumbsup", "file_comment": "FileCommentID", @@ -74,7 +78,7 @@ http.HandleFunc("/reactions.add", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) }) for i, test := range tests { rh = newReactionsHandler() - err := api.AddReaction(test.params) + err := api.AddReaction(test.name, test.ref) if err != nil { t.Fatalf("%d: Unexpected error: %s", i, err) } @@ -89,11 +93,13 @@ SLACK_API = "http://" + serverAddr + "/" api := New("testing-token") tests := []struct { - params RemoveReactionParameters + name string + ref ItemRef wantParams map[string]string }{ { - NewRemoveReactionParameters("thumbsup", NewRefToMessage("ChannelID", "123")), + "thumbsup", + NewRefToMessage("ChannelID", "123"), map[string]string{ "name": "thumbsup", "channel": "ChannelID", @@ -101,14 +107,16 @@ }, }, { - NewRemoveReactionParameters("thumbsup", NewRefToFile("FileID")), + "thumbsup", + NewRefToFile("FileID"), map[string]string{ "name": "thumbsup", "file": "FileID", }, }, { - NewRemoveReactionParameters("thumbsup", NewRefToFileComment("FileCommentID")), + "thumbsup", + NewRefToFileComment("FileCommentID"), map[string]string{ "name": "thumbsup", "file_comment": "FileCommentID", @@ -119,7 +127,7 @@ http.HandleFunc("/reactions.remove", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) }) for i, test := range tests { rh = newReactionsHandler() - err := api.RemoveReaction(test.params) + err := api.RemoveReaction(test.name, test.ref) if err != nil { t.Fatalf("%d: Unexpected error: %s", i, err) } @@ -134,14 +142,15 @@ SLACK_API = "http://" + serverAddr + "/" api := New("testing-token") tests := []struct { + ref ItemRef params GetReactionsParameters wantParams map[string]string json string wantReactions []ItemReaction }{ { - - GetReactionsParameters{ItemRef: NewRefToMessage("ChannelID", "123")}, + NewRefToMessage("ChannelID", "123"), + GetReactionsParameters{}, map[string]string{ "channel": "ChannelID", "timestamp": "123", @@ -168,7 +177,8 @@ }, }, { - GetReactionsParameters{ItemRef: NewRefToFile("FileID"), Full: true}, + NewRefToFile("FileID"), + GetReactionsParameters{Full: true}, map[string]string{ "file": "FileID", "full": "true", @@ -196,7 +206,8 @@ }, { - GetReactionsParameters{ItemRef: NewRefToFileComment("FileCommentID")}, + NewRefToFileComment("FileCommentID"), + GetReactionsParameters{}, map[string]string{ "file_comment": "FileCommentID", }, @@ -228,7 +239,7 @@ for i, test := range tests { rh = newReactionsHandler() rh.response = test.json - got, err := api.GetReactions(test.params) + got, err := api.GetReactions(test.ref, test.params) if err != nil { t.Fatalf("%d: Unexpected error: %s", i, err) } @@ -331,7 +342,8 @@ "page": "2", "full": "true", } - params := NewListReactionsParameters("UserID") + params := NewListReactionsParameters() + params.UserId = "UserID" params.Count = 200 params.Page = 2 params.Full = true