Repository: slack-go/slack
Pull request #692 (2020-04-04 19:13:46)

add support for multi-users in users.info
**Summary**

- Issue: #691 
- add support for multi-users in users.info


Patch:
diff --git a/users.go b/users.go
index 6001e0f..364958e 100644
--- a/users.go
+++ b/users.go
@@ -5,6 +5,7 @@ import (
 	"encoding/json"
 	"net/url"
 	"strconv"
+	"strings"
 	"time"
 )
 
@@ -184,6 +185,7 @@ type TeamIdentity struct {
 type userResponseFull struct {
 	Members []User `json:"members,omitempty"`
 	User    `json:"user,omitempty"`
+	Users   []User `json:"users,omitempty"`
 	UserPresence
 	SlackResponse
 	Metadata ResponseMetadata `json:"response_metadata"`
@@ -252,6 +254,26 @@ func (api *Client) GetUserInfoContext(ctx context.Context, user string) (*User,
 	return &response.User, nil
 }
 
+// GetUsersInfo will retrieve the complete multi-users information
+func (api *Client) GetUsersInfo(users ...string) (*[]User, error) {
+	return api.GetUsersInfoContext(context.Background(), users...)
+}
+
+// GetUsersInfoContext will retrieve the complete multi-users information with a custom context
+func (api *Client) GetUsersInfoContext(ctx context.Context, users ...string) (*[]User, error) {
+	values := url.Values{
+		"token":          {api.token},
+		"users":          {strings.Join(users, ",")},
+		"include_locale": {strconv.FormatBool(true)},
+	}
+
+	response, err := api.userRequest(ctx, "users.info", values)
+	if err != nil {
+		return nil, err
+	}
+	return &response.Users, nil
+}
+
 // GetUsersOption options for the GetUsers method call.
 type GetUsersOption func(*UserPagination)
 


Test patch:
diff --git a/users_test.go b/users_test.go
index cf85756..ab1697e 100644
--- a/users_test.go
+++ b/users_test.go
@@ -76,6 +76,13 @@ func getTestUser() User {
 	return getTestUserWithId("UXXXXXXXX")
 }
 
+func getTestUsers() []User {
+	return []User{
+		getTestUserWithId("UYYYYYYYY"),
+		getTestUserWithId("UZZZZZZZZ"),
+	}
+}
+
 func getUserIdentity(rw http.ResponseWriter, r *http.Request) {
 	rw.Header().Set("Content-Type", "application/json")
 	response := []byte(`{
@@ -120,6 +127,18 @@ func getUserInfo(rw http.ResponseWriter, r *http.Request) {
 	rw.Write(response)
 }
 
+func getUsersInfo(rw http.ResponseWriter, r *http.Request) {
+	rw.Header().Set("Content-Type", "application/json")
+	response, _ := json.Marshal(struct {
+		Ok    bool   `json:"ok"`
+		Users []User `json:"users"`
+	}{
+		Ok:    true,
+		Users: getTestUsers(),
+	})
+	rw.Write(response)
+}
+
 func getUserByEmail(rw http.ResponseWriter, r *http.Request) {
 	rw.Header().Set("Content-Type", "application/json")
 	response, _ := json.Marshal(struct {
@@ -248,6 +267,24 @@ func TestGetUserInfo(t *testing.T) {
 	}
 }
 
+func TestGetUsersInfo(t *testing.T) {
+	http.DefaultServeMux = new(http.ServeMux)
+	http.HandleFunc("/users.info", getUsersInfo)
+	expectedUsers := getTestUsers()
+
+	once.Do(startServer)
+	api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
+
+	user, err := api.GetUsersInfo("UYYYYYYYY", "UZZZZZZZZ")
+	if err != nil {
+		t.Errorf("Unexpected error: %s", err)
+		return
+	}
+	if !reflect.DeepEqual(expectedUsers, *user) {
+		t.Fatal(ErrIncorrectResponse)
+	}
+}
+
 func TestGetUserByEmail(t *testing.T) {
 	http.HandleFunc("/users.lookupByEmail", getUserByEmail)
 	expectedUser := getTestUser()