From e96b3ac5c14031c3bf0789c58b2a752211afed8a Mon Sep 17 00:00:00 2001 From: Aenimus Date: Thu, 13 Apr 2023 22:43:55 +0100 Subject: [PATCH 1/4] feat: implement conversations.inviteShared --- conversation.go | 43 +++++++++++++++++++++++++++++++++++++++++++ conversation_test.go | 16 ++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/conversation.go b/conversation.go index 44f06ea93..2fc7fac66 100644 --- a/conversation.go +++ b/conversation.go @@ -690,3 +690,46 @@ func (api *Client) MarkConversationContext(ctx context.Context, channel, ts stri } return response.Err() } + +type InviteSharedParams struct { + Emails []string + ExternalLimited bool + UserIDs []string +} + +var InvalidSharedInviteParamsError = errors.New("one of Emails or UserIDs must be supplied to InviteSharedParams") + +// InviteSharedContext sends array(s) of user IDs and/or emails an invitation to a Slack Connect channel with a custom context +// At least one array of either emails or user IDs must be supplied to invite the recipient +func (api *Client) InviteSharedContext(ctx context.Context, channelID string, params InviteSharedParams) error { + if len(params.UserIDs) < 1 && len(params.Emails) < 1 { + return InvalidSharedInviteParamsError + } + + values := url.Values{ + "channel": {channelID}, + } + if params.Emails != nil { + values.Add("emails", strings.Join(params.Emails, "'")) + } + if params.ExternalLimited == false { + values.Add("external_limited", "false") + } + if params.UserIDs != nil { + values.Add("user_ids", strings.Join(params.Emails, "'")) + } + + response := &SlackResponse{} + err := api.getMethod(ctx, "conversations.inviteShared", api.token, values, &response) + if err != nil { + return err + } + + return response.Err() +} + +// InviteShared sends array(s) of user IDs and/or emails an invitation to a Slack Connect channel +// At least one array of either emails or user IDs must be supplied to invite the recipient +func (api *Client) InviteShared(channelID string, params InviteSharedParams) error { + return api.InviteSharedContext(context.Background(), channelID, params) +} diff --git a/conversation_test.go b/conversation_test.go index 85011d5a6..f5bba7720 100644 --- a/conversation_test.go +++ b/conversation_test.go @@ -591,3 +591,19 @@ func TestMarkConversation(t *testing.T) { return } } + +func TestInviteShared(t *testing.T) { + http.HandleFunc("/conversations.inviteShared", okJSONHandler) + once.Do(startServer) + api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) + params := &InviteSharedParams{} + err := api.InviteShared("CXXXXXXXX", *params) + assert.EqualError(t, err, InvalidSharedInviteParamsError.Error()) + + params.Emails = []string{"fake@email.com", "another@email.com"} + err = api.InviteShared("CXXXXXXXX", *params) + if err != nil { + t.Errorf("Unexpected error: %s", err) + return + } +} From cf4a573dbc2a555fc003878c98e696a12484e75e Mon Sep 17 00:00:00 2001 From: Aenimus Date: Tue, 25 Apr 2023 18:49:08 +0100 Subject: [PATCH 2/4] fix: booleans default to false so use a pointer instead --- conversation.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/conversation.go b/conversation.go index 2fc7fac66..cc2341047 100644 --- a/conversation.go +++ b/conversation.go @@ -692,9 +692,15 @@ func (api *Client) MarkConversationContext(ctx context.Context, channel, ts stri } type InviteSharedParams struct { - Emails []string - ExternalLimited bool - UserIDs []string + // Emails are the emails to be invited to the Slack Connect + Emails []string + // ExternalLimited determines whether the invitee is an external limited member. + // External limited members cannot invite other cannot invite others, export channel history, + // change channel visibility, topic, or name, and the channel will be set as private in the guest workspace. + // Defaults to true + ExternalLimited *bool + // UserIDs are the Slack IDs to be invited to the Slack Connect + UserIDs []string } var InvalidSharedInviteParamsError = errors.New("one of Emails or UserIDs must be supplied to InviteSharedParams") @@ -712,7 +718,8 @@ func (api *Client) InviteSharedContext(ctx context.Context, channelID string, pa if params.Emails != nil { values.Add("emails", strings.Join(params.Emails, "'")) } - if params.ExternalLimited == false { + // the externalLimited parameter defaults to true; ignore it unless it is explicitly false + if params.ExternalLimited != nil && *params.ExternalLimited == false { values.Add("external_limited", "false") } if params.UserIDs != nil { From 2451c7335ade6fa0f9f36db40d4c2dcddfc8ec91 Mon Sep 17 00:00:00 2001 From: Aenimus Date: Tue, 25 Apr 2023 19:29:11 +0100 Subject: [PATCH 3/4] Revert "fix: booleans default to false so use a pointer instead" This reverts commit cf4a573dbc2a555fc003878c98e696a12484e75e. --- conversation.go | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/conversation.go b/conversation.go index cc2341047..2fc7fac66 100644 --- a/conversation.go +++ b/conversation.go @@ -692,15 +692,9 @@ func (api *Client) MarkConversationContext(ctx context.Context, channel, ts stri } type InviteSharedParams struct { - // Emails are the emails to be invited to the Slack Connect - Emails []string - // ExternalLimited determines whether the invitee is an external limited member. - // External limited members cannot invite other cannot invite others, export channel history, - // change channel visibility, topic, or name, and the channel will be set as private in the guest workspace. - // Defaults to true - ExternalLimited *bool - // UserIDs are the Slack IDs to be invited to the Slack Connect - UserIDs []string + Emails []string + ExternalLimited bool + UserIDs []string } var InvalidSharedInviteParamsError = errors.New("one of Emails or UserIDs must be supplied to InviteSharedParams") @@ -718,8 +712,7 @@ func (api *Client) InviteSharedContext(ctx context.Context, channelID string, pa if params.Emails != nil { values.Add("emails", strings.Join(params.Emails, "'")) } - // the externalLimited parameter defaults to true; ignore it unless it is explicitly false - if params.ExternalLimited != nil && *params.ExternalLimited == false { + if params.ExternalLimited == false { values.Add("external_limited", "false") } if params.UserIDs != nil { From da25eaf6ec3beb500d7572244889d7e2a6fed8b4 Mon Sep 17 00:00:00 2001 From: Aenimus Date: Tue, 25 Apr 2023 20:12:50 +0100 Subject: [PATCH 4/4] fix: enable boolean true default with pointer; add comments --- conversation.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/conversation.go b/conversation.go index 2fc7fac66..0916898b6 100644 --- a/conversation.go +++ b/conversation.go @@ -692,9 +692,15 @@ func (api *Client) MarkConversationContext(ctx context.Context, channel, ts stri } type InviteSharedParams struct { - Emails []string - ExternalLimited bool - UserIDs []string + // Emails are the emails to be invited to the Slack Connect + Emails []string + // ExternalLimited determines whether the invitee is an external limited member. + // External limited members cannot invite other cannot invite others, export channel history, + // change channel visibility, topic, or name, and the channel will be set as private in the guest workspace. + // Defaults to true + ExternalLimited *bool + // UserIDs are the Slack IDs to be invited to the Slack Connect + UserIDs []string } var InvalidSharedInviteParamsError = errors.New("one of Emails or UserIDs must be supplied to InviteSharedParams") @@ -710,13 +716,14 @@ func (api *Client) InviteSharedContext(ctx context.Context, channelID string, pa "channel": {channelID}, } if params.Emails != nil { - values.Add("emails", strings.Join(params.Emails, "'")) + values.Add("emails", strings.Join(params.Emails, ",")) } - if params.ExternalLimited == false { + // the externalLimited parameter defaults to true; ignore it unless it is explicitly false + if params.ExternalLimited != nil && !*params.ExternalLimited { values.Add("external_limited", "false") } if params.UserIDs != nil { - values.Add("user_ids", strings.Join(params.Emails, "'")) + values.Add("user_ids", strings.Join(params.Emails, ",")) } response := &SlackResponse{}