Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sends an invitation to a Slack Connect channel #1216

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,53 @@ func (api *Client) InviteUsersToConversationContext(ctx context.Context, channel
return response.Channel, response.Err()
}

// InviteSharedEmailsToConversation invites users to a shared channels by email
func (api *Client) InviteSharedEmailsToConversation(channelID string, emails ...string) (string, bool, error) {
return api.inviteSharedToConversationHelper(context.Background(), channelID, emails, nil)
}

// InviteSharedEmailsToConversationContext invites users to a shared channels by email using context
func (api *Client) InviteSharedEmailsToConversationContext(ctx context.Context, channelID string, emails ...string) (string, bool, error) {
return api.inviteSharedToConversationHelper(ctx, channelID, emails, nil)
}

// InviteSharedUserIDsToConversation invites users to a shared channels by user id
func (api *Client) InviteSharedUserIDsToConversation(channelID string, userIDs ...string) (string, bool, error) {
return api.inviteSharedToConversationHelper(context.Background(), channelID, nil, userIDs)
}

// InviteSharedUserIDsToConversationContext invites users to a shared channels by user id with context
func (api *Client) InviteSharedUserIDsToConversationContext(ctx context.Context, channelID string, userIDs ...string) (string, bool, error) {
return api.inviteSharedToConversationHelper(ctx, channelID, nil, userIDs)
}

// inviteSharedToConversationHelper invites emails or userIDs to a channel with a custom context.
// This is a helper function for InviteSharedEmailsToConversation and InviteSharedUserIDsToConversation.
// It accepts either emails or userIDs, but not both.
func (api *Client) inviteSharedToConversationHelper(ctx context.Context, channelID string, emails []string, userIDs []string) (string, bool, error) {
values := url.Values{
"token": {api.token},
"channel": {channelID},
}
if len(emails) > 0 {
values.Add("emails", strings.Join(emails, ","))
} else if len(userIDs) > 0 {
values.Add("user_ids", strings.Join(userIDs, ","))
}
response := struct {
SlackResponse
InviteID string `json:"invite_id"`
IsLegacySharedChannel bool `json:"is_legacy_shared_channel"`
}{}

err := api.postMethod(ctx, "conversations.inviteShared", values, &response)
if err != nil {
return "", false, err
}

return response.InviteID, response.IsLegacySharedChannel, response.Err()
}

// KickUserFromConversation removes a user from a conversation
func (api *Client) KickUserFromConversation(channelID string, user string) error {
return api.KickUserFromConversationContext(context.Background(), channelID, user)
Expand Down
52 changes: 52 additions & 0 deletions conversation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,20 @@ func okChannelJsonHandler(rw http.ResponseWriter, r *http.Request) {
rw.Write(response)
}

func okInviteSharedJsonHandler(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(struct {
SlackResponse
InviteID string `json:"invite_id"`
IsLegacySharedChannel bool `json:"is_legacy_shared_channel"`
}{
SlackResponse: SlackResponse{Ok: true},
InviteID: "I01234567",
IsLegacySharedChannel: false,
})
rw.Write(response)
}

func TestSetTopicOfConversation(t *testing.T) {
http.HandleFunc("/conversations.setTopic", okChannelJsonHandler)
once.Do(startServer)
Expand Down Expand Up @@ -348,6 +362,44 @@ func TestInviteUsersToConversation(t *testing.T) {
}
}

func TestInviteSharedToConversation(t *testing.T) {
http.HandleFunc("/conversations.inviteShared", okInviteSharedJsonHandler)
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))

t.Run("user_ids", func(t *testing.T) {
userIDs := []string{"UXXXXXXX1", "UXXXXXXX2"}
inviteID, isLegacySharedChannel, err := api.InviteSharedUserIDsToConversation("CXXXXXXXX", userIDs...)
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
if inviteID == "" {
t.Error("invite id should have a value")
return
}
if isLegacySharedChannel {
t.Error("is legacy shared channel should be false")
}
})

t.Run("emails", func(t *testing.T) {
emails := []string{"nopcoder@slack.com", "nopcoder@example.com"}
inviteID, isLegacySharedChannel, err := api.InviteSharedEmailsToConversation("CXXXXXXXX", emails...)
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
if inviteID == "" {
t.Error("invite id should have a value")
return
}
if isLegacySharedChannel {
t.Error("is legacy shared channel should be false")
}
})
}

func TestKickUserFromConversation(t *testing.T) {
http.HandleFunc("/conversations.kick", okJSONHandler)
once.Do(startServer)
Expand Down
6 changes: 6 additions & 0 deletions slacktest/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,9 @@ var renameConversationJSON = fmt.Sprintf(templateConversationJSON, "newName",

var inviteConversationJSON = fmt.Sprintf(templateConversationJSON, defaultConversationName,
nowAsJSONTime(), defaultBotID, defaultConversationName, "", "", 0, "", "", 0, 1)

const inviteSharedResponseJSON = `{
"ok": true,
"invite_id": "I02UKAJ6RJA",
"is_legacy_shared_channel": false
}`
5 changes: 5 additions & 0 deletions slacktest/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ func inviteConversationHandler(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(inviteConversationJSON))
}

// handle conversations.inviteShared
func inviteSharedConversationHandler(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(inviteSharedResponseJSON))
}

// handle groups.list
func listGroupsHandler(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(defaultGroupsListJSON))
Expand Down
1 change: 1 addition & 0 deletions slacktest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func NewTestServer(custom ...Binder) *Server {
s.Handle("/conversations.setPurpose", setConversationPurposeHandler)
s.Handle("/conversations.rename", renameConversationHandler)
s.Handle("/conversations.invite", inviteConversationHandler)
s.Handle("/conversations.inviteShared", inviteSharedConversationHandler)
s.Handle("/users.info", usersInfoHandler)
s.Handle("/users.lookupByEmail", usersInfoHandler)
s.Handle("/bots.info", botsInfoHandler)
Expand Down