Skip to content

Commit

Permalink
go conversations: add support for conversations.inviteShared
Browse files Browse the repository at this point in the history
Taken from slack-go#1195.
  • Loading branch information
AChelikani committed Aug 7, 2023
1 parent c4095cb commit 43395f7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
50 changes: 50 additions & 0 deletions conversation.go
Expand Up @@ -690,3 +690,53 @@ func (api *Client) MarkConversationContext(ctx context.Context, channel, ts stri
}
return response.Err()
}

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
}

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, ","))
}
// 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, ","))
}

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)
}
16 changes: 16 additions & 0 deletions conversation_test.go
Expand Up @@ -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
}
}

0 comments on commit 43395f7

Please sign in to comment.