From 2bb1377363e0b7b598d41c817abd18280ed6d689 Mon Sep 17 00:00:00 2001 From: Johan McGwire Date: Wed, 20 Oct 2021 20:27:29 -0400 Subject: [PATCH 1/7] Add include_num_members support to conversation.info request --- conversation.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/conversation.go b/conversation.go index 299362601..586430452 100644 --- a/conversation.go +++ b/conversation.go @@ -358,16 +358,17 @@ func (api *Client) CreateConversationContext(ctx context.Context, channelName st } // GetConversationInfo retrieves information about a conversation -func (api *Client) GetConversationInfo(channelID string, includeLocale bool) (*Channel, error) { - return api.GetConversationInfoContext(context.Background(), channelID, includeLocale) +func (api *Client) GetConversationInfo(channelID string, includeLocale, includeNumMembers bool) (*Channel, error) { + return api.GetConversationInfoContext(context.Background(), channelID, includeLocale, includeNumMembers) } // GetConversationInfoContext retrieves information about a conversation with a custom context -func (api *Client) GetConversationInfoContext(ctx context.Context, channelID string, includeLocale bool) (*Channel, error) { +func (api *Client) GetConversationInfoContext(ctx context.Context, channelID string, includeLocale, includeNumMembers bool) (*Channel, error) { values := url.Values{ - "token": {api.token}, - "channel": {channelID}, - "include_locale": {strconv.FormatBool(includeLocale)}, + "token": {api.token}, + "channel": {channelID}, + "include_locale": {strconv.FormatBool(includeLocale)}, + "include_num_members": {strconv.FormatBool(includeNumMembers)}, } response, err := api.channelRequest(ctx, "conversations.info", values) if err != nil { From 094f09633ed44bfea71e782ad02e55729b7f178c Mon Sep 17 00:00:00 2001 From: Johan McGwire Date: Wed, 20 Oct 2021 20:31:29 -0400 Subject: [PATCH 2/7] Fix test --- conversation_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversation_test.go b/conversation_test.go index 61a4e92b1..f1a4a0dd9 100644 --- a/conversation_test.go +++ b/conversation_test.go @@ -400,7 +400,7 @@ func TestGetConversationInfo(t *testing.T) { http.HandleFunc("/conversations.info", okChannelJsonHandler) once.Do(startServer) api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) - channel, err := api.GetConversationInfo("CXXXXXXXX", false) + channel, err := api.GetConversationInfo("CXXXXXXXX", false, false) if err != nil { t.Errorf("Unexpected error: %s", err) return From e4955fd97d47d31cf9532944a864e61879a4b4e5 Mon Sep 17 00:00:00 2001 From: Johan McGwire Date: Wed, 24 Nov 2021 10:50:24 -0500 Subject: [PATCH 3/7] Swap to struct input Signed-off-by: Johan McGwire --- conversation.go | 23 +++++++++++++++++------ conversation_test.go | 4 +++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/conversation.go b/conversation.go index 586430452..afde30ff0 100644 --- a/conversation.go +++ b/conversation.go @@ -357,18 +357,29 @@ func (api *Client) CreateConversationContext(ctx context.Context, channelName st return &response.Channel, nil } +// GetConversationInfoInput Defines the parameters of a GetConversationInfo and GetConversationInfoContext function +type GetConversationInfoInput struct { + ChannelID string + IncludeLocale bool + IncludeNumMembers bool +} + // GetConversationInfo retrieves information about a conversation -func (api *Client) GetConversationInfo(channelID string, includeLocale, includeNumMembers bool) (*Channel, error) { - return api.GetConversationInfoContext(context.Background(), channelID, includeLocale, includeNumMembers) +func (api *Client) GetConversationInfo(input *GetConversationInfoInput) (*Channel, error) { + return api.GetConversationInfoContext(context.Background(), input) } // GetConversationInfoContext retrieves information about a conversation with a custom context -func (api *Client) GetConversationInfoContext(ctx context.Context, channelID string, includeLocale, includeNumMembers bool) (*Channel, error) { +func (api *Client) GetConversationInfoContext(ctx context.Context, input *GetConversationInfoInput) (*Channel, error) { + if input == nil { + input = &GetConversationInfoInput{} + } + values := url.Values{ "token": {api.token}, - "channel": {channelID}, - "include_locale": {strconv.FormatBool(includeLocale)}, - "include_num_members": {strconv.FormatBool(includeNumMembers)}, + "channel": {input.ChannelID}, + "include_locale": {strconv.FormatBool(input.IncludeLocale)}, + "include_num_members": {strconv.FormatBool(input.IncludeNumMembers)}, } response, err := api.channelRequest(ctx, "conversations.info", values) if err != nil { diff --git a/conversation_test.go b/conversation_test.go index f1a4a0dd9..fedba8f8b 100644 --- a/conversation_test.go +++ b/conversation_test.go @@ -400,7 +400,9 @@ func TestGetConversationInfo(t *testing.T) { http.HandleFunc("/conversations.info", okChannelJsonHandler) once.Do(startServer) api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) - channel, err := api.GetConversationInfo("CXXXXXXXX", false, false) + channel, err := api.GetConversationInfo(&GetConversationInfoInput{ + ChannelID: "CXXXXXXXX", + }) if err != nil { t.Errorf("Unexpected error: %s", err) return From fd9eee81d4bf0c922ec4ce3c72c0db2fea1e6fe4 Mon Sep 17 00:00:00 2001 From: Johan McGwire Date: Sun, 13 Feb 2022 14:23:30 -0500 Subject: [PATCH 4/7] gofmt --- conversation.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conversation.go b/conversation.go index afde30ff0..861d86aae 100644 --- a/conversation.go +++ b/conversation.go @@ -359,8 +359,8 @@ func (api *Client) CreateConversationContext(ctx context.Context, channelName st // GetConversationInfoInput Defines the parameters of a GetConversationInfo and GetConversationInfoContext function type GetConversationInfoInput struct { - ChannelID string - IncludeLocale bool + ChannelID string + IncludeLocale bool IncludeNumMembers bool } From 439edc315288787a9532d9461d3fa112da8c2f76 Mon Sep 17 00:00:00 2001 From: Johan McGwire Date: Mon, 2 May 2022 19:30:27 -0400 Subject: [PATCH 5/7] Return errors upon missing parameters --- conversation.go | 8 +++++++- conversation_test.go | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/conversation.go b/conversation.go index 861d86aae..d3f94a6c0 100644 --- a/conversation.go +++ b/conversation.go @@ -5,6 +5,8 @@ import ( "net/url" "strconv" "strings" + + "github.com/pkg/errors" ) // Conversation is the foundation for IM and BaseGroupConversation @@ -372,7 +374,11 @@ func (api *Client) GetConversationInfo(input *GetConversationInfoInput) (*Channe // GetConversationInfoContext retrieves information about a conversation with a custom context func (api *Client) GetConversationInfoContext(ctx context.Context, input *GetConversationInfoInput) (*Channel, error) { if input == nil { - input = &GetConversationInfoInput{} + return nil, errors.New("GetConversationInfoInput must not be nil") + } + + if input.ChannelID == "" { + return nil, errors.New("ChannelID must be defined") } values := url.Values{ diff --git a/conversation_test.go b/conversation_test.go index fedba8f8b..990f72f68 100644 --- a/conversation_test.go +++ b/conversation_test.go @@ -413,6 +413,28 @@ func TestGetConversationInfo(t *testing.T) { } } +func TestGetConversationInfoNilError(t *testing.T) { + http.HandleFunc("/conversations.info", okChannelJsonHandler) + once.Do(startServer) + api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) + _, err := api.GetConversationInfo(nil) + if err == nil { + t.Errorf("Unexpected pass where there should have been nil input error") + return + } +} + +func TestGetConversationInfoMissingChannelError(t *testing.T) { + http.HandleFunc("/conversations.info", okChannelJsonHandler) + once.Do(startServer) + api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) + _, err := api.GetConversationInfo(&GetConversationInfoInput{}) + if err == nil { + t.Errorf("Unexpected pass where there should have been missing channel error") + return + } +} + func leaveConversationHandler(rw http.ResponseWriter, r *http.Request) { rw.Header().Set("Content-Type", "application/json") response, _ := json.Marshal(struct { From 6ee76f42bb1b37511fe07058730e3ffefd29499f Mon Sep 17 00:00:00 2001 From: Johan McGwire Date: Tue, 3 May 2022 10:37:18 -0400 Subject: [PATCH 6/7] Use built-in errors package --- conversation.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/conversation.go b/conversation.go index d3f94a6c0..c41be4ef7 100644 --- a/conversation.go +++ b/conversation.go @@ -2,11 +2,10 @@ package slack import ( "context" + "errors" "net/url" "strconv" "strings" - - "github.com/pkg/errors" ) // Conversation is the foundation for IM and BaseGroupConversation From ba92d3451f35a8ec4aa794920d0543612faa4c85 Mon Sep 17 00:00:00 2001 From: Johan McGwire Date: Sun, 8 May 2022 16:35:20 -0400 Subject: [PATCH 7/7] combine tests to handle http handler registration --- conversation_test.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/conversation_test.go b/conversation_test.go index 990f72f68..29f4b7742 100644 --- a/conversation_test.go +++ b/conversation_test.go @@ -411,24 +411,18 @@ func TestGetConversationInfo(t *testing.T) { t.Error("channel should not be nil") return } -} -func TestGetConversationInfoNilError(t *testing.T) { - http.HandleFunc("/conversations.info", okChannelJsonHandler) - once.Do(startServer) - api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) - _, err := api.GetConversationInfo(nil) + // Nil Input Error + api = New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) + _, err = api.GetConversationInfo(nil) if err == nil { t.Errorf("Unexpected pass where there should have been nil input error") return } -} -func TestGetConversationInfoMissingChannelError(t *testing.T) { - http.HandleFunc("/conversations.info", okChannelJsonHandler) - once.Do(startServer) - api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) - _, err := api.GetConversationInfo(&GetConversationInfoInput{}) + // No Channel Error + api = New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) + _, err = api.GetConversationInfo(&GetConversationInfoInput{}) if err == nil { t.Errorf("Unexpected pass where there should have been missing channel error") return