Skip to content

Commit c5ef90e

Browse files
authored
feat: Add support for external_limited option of inviteShared (#1330)
Expose the ability to override the [external_limited option](https://api.slack.com/methods/conversations.inviteShared#arg_external_limited) for inviteShared. Adding the param to all the InviteSharedEmailsToConversation, etc. methods would be a breaking change to those callers, so I opted instead to expose the underlying helper (renamed to InviteSharedToConversation). I feel like the convenience methods (InviteSharedEmailsToConversation/InviteSharedUserIDsToConversation) are not actually that much more convenient than just using the helper, and I think we can eventually remove them in favor of having people call InviteSharedToConversation directly. But that's a future thing. Although it's slightly inconvenient for the caller to use *bool for ExternalLimited, the two alternatives I considered are, I think worse: - Include ExternalLimited as a bool in the InviteSharedParams. I dislike this way because it gives the SDK user of InviteSharedToConversation a different default behavior from inviteShared, since the default value in the API is true. - Add a bool like NonExternalLimited to InviteSharedParams. This way the defaulting is consistent with the API if it's not specified; however, the InviteSharedParams no longer mirror the API args, which I think is confusing.
1 parent 21e61c5 commit c5ef90e

File tree

2 files changed

+65
-15
lines changed

2 files changed

+65
-15
lines changed

conversation.go

+44-15
Original file line numberDiff line numberDiff line change
@@ -329,42 +329,71 @@ func (api *Client) InviteUsersToConversationContext(ctx context.Context, channel
329329
}
330330

331331
// InviteSharedEmailsToConversation invites users to a shared channels by email.
332-
// For more details, see InviteSharedEmailsToConversationContext documentation.
332+
// For more details, see InviteSharedToConversationContext documentation.
333333
func (api *Client) InviteSharedEmailsToConversation(channelID string, emails ...string) (string, bool, error) {
334-
return api.inviteSharedToConversationHelper(context.Background(), channelID, emails, nil)
334+
return api.InviteSharedToConversationContext(context.Background(), InviteSharedToConversationParams{
335+
ChannelID: channelID,
336+
Emails: emails,
337+
})
335338
}
336339

337340
// InviteSharedEmailsToConversationContext invites users to a shared channels by email using context.
338-
// For more details, see inviteSharedToConversationHelper documentation.
341+
// For more details, see InviteSharedToConversationContext documentation.
339342
func (api *Client) InviteSharedEmailsToConversationContext(ctx context.Context, channelID string, emails ...string) (string, bool, error) {
340-
return api.inviteSharedToConversationHelper(ctx, channelID, emails, nil)
343+
return api.InviteSharedToConversationContext(ctx, InviteSharedToConversationParams{
344+
ChannelID: channelID,
345+
Emails: emails,
346+
})
341347
}
342348

343349
// InviteSharedUserIDsToConversation invites users to a shared channels by user id.
344-
// For more details, see InviteSharedUserIDsToConversationContext documentation.
350+
// For more details, see InviteSharedToConversationContext documentation.
345351
func (api *Client) InviteSharedUserIDsToConversation(channelID string, userIDs ...string) (string, bool, error) {
346-
return api.inviteSharedToConversationHelper(context.Background(), channelID, nil, userIDs)
352+
return api.InviteSharedToConversationContext(context.Background(), InviteSharedToConversationParams{
353+
ChannelID: channelID,
354+
UserIDs: userIDs,
355+
})
347356
}
348357

349358
// InviteSharedUserIDsToConversationContext invites users to a shared channels by user id with context.
350-
// For more details, see inviteSharedToConversationHelper documentation.
359+
// For more details, see InviteSharedToConversationContext documentation.
351360
func (api *Client) InviteSharedUserIDsToConversationContext(ctx context.Context, channelID string, userIDs ...string) (string, bool, error) {
352-
return api.inviteSharedToConversationHelper(ctx, channelID, nil, userIDs)
361+
return api.InviteSharedToConversationContext(ctx, InviteSharedToConversationParams{
362+
ChannelID: channelID,
363+
UserIDs: userIDs,
364+
})
353365
}
354366

355-
// inviteSharedToConversationHelper invites emails or userIDs to a channel with a custom context.
367+
// InviteSharedToConversationParams defines the parameters for the InviteSharedToConversation and InviteSharedToConversationContext functions.
368+
type InviteSharedToConversationParams struct {
369+
ChannelID string
370+
Emails []string
371+
UserIDs []string
372+
ExternalLimited *bool
373+
}
374+
375+
// InviteSharedToConversation invites emails or userIDs to a channel.
376+
// For more details, see InviteSharedToConversationContext documentation.
377+
func (api *Client) InviteSharedToConversation(params InviteSharedToConversationParams) (string, bool, error) {
378+
return api.InviteSharedToConversationContext(context.Background(), params)
379+
}
380+
381+
// InviteSharedToConversationContext invites emails or userIDs to a channel with a custom context.
356382
// This is a helper function for InviteSharedEmailsToConversation and InviteSharedUserIDsToConversation.
357383
// It accepts either emails or userIDs, but not both.
358384
// Slack API docs: https://api.slack.com/methods/conversations.inviteShared
359-
func (api *Client) inviteSharedToConversationHelper(ctx context.Context, channelID string, emails []string, userIDs []string) (string, bool, error) {
385+
func (api *Client) InviteSharedToConversationContext(ctx context.Context, params InviteSharedToConversationParams) (string, bool, error) {
360386
values := url.Values{
361387
"token": {api.token},
362-
"channel": {channelID},
388+
"channel": {params.ChannelID},
389+
}
390+
if len(params.Emails) > 0 {
391+
values.Add("emails", strings.Join(params.Emails, ","))
392+
} else if len(params.UserIDs) > 0 {
393+
values.Add("user_ids", strings.Join(params.UserIDs, ","))
363394
}
364-
if len(emails) > 0 {
365-
values.Add("emails", strings.Join(emails, ","))
366-
} else if len(userIDs) > 0 {
367-
values.Add("user_ids", strings.Join(userIDs, ","))
395+
if params.ExternalLimited != nil {
396+
values.Add("external_limited", strconv.FormatBool(*params.ExternalLimited))
368397
}
369398
response := struct {
370399
SlackResponse

conversation_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,27 @@ func TestInviteSharedToConversation(t *testing.T) {
477477
t.Error("is legacy shared channel should be false")
478478
}
479479
})
480+
481+
t.Run("external_limited", func(t *testing.T) {
482+
userIDs := []string{"UXXXXXXX1", "UXXXXXXX2"}
483+
externalLimited := true
484+
inviteID, isLegacySharedChannel, err := api.InviteSharedToConversation(InviteSharedToConversationParams{
485+
ChannelID: "CXXXXXXXX",
486+
UserIDs: userIDs,
487+
ExternalLimited: &externalLimited,
488+
})
489+
if err != nil {
490+
t.Errorf("Unexpected error: %s", err)
491+
return
492+
}
493+
if inviteID == "" {
494+
t.Error("invite id should have a value")
495+
return
496+
}
497+
if isLegacySharedChannel {
498+
t.Error("is legacy shared channel should be false")
499+
}
500+
})
480501
}
481502

482503
func TestKickUserFromConversation(t *testing.T) {

0 commit comments

Comments
 (0)