Skip to content

Commit

Permalink
Merge pull request #61 from rneatherway/page-users
Browse files Browse the repository at this point in the history
Add paging when fetching users
  • Loading branch information
rneatherway authored Jun 11, 2024
2 parents 2d8d997 + 587dcba commit ecda780
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions internal/slackclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type User struct {
}

type UsersResponse struct {
CursorResponseMetadata
Ok bool
Members []User
}
Expand Down Expand Up @@ -262,20 +263,32 @@ func (c *SlackClient) conversations() ([]Channel, error) {
return channels, nil
}

func (c *SlackClient) users(params map[string]string) (*UsersResponse, error) {
body, err := c.get("users.list", nil)
if err != nil {
return nil, err
}
func (c *SlackClient) users() ([]User, error) {
users := make([]User, 0, 100)
resp := &UsersResponse{}
for {
body, err := c.get("users.list", map[string]string{
"cursor": resp.ResponseMetadata.NextCursor,
"limit": "1000",
})
if err != nil {
return nil, err
}

users := &UsersResponse{}
err = json.Unmarshal(body, users)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, resp)
if err != nil {
return nil, err
}

if !users.Ok {
return nil, fmt.Errorf("users response not OK: %s", body)
if !resp.Ok {
return nil, fmt.Errorf("users response not OK: %s", body)
}

users = append(users, resp.Members...)

if resp.ResponseMetadata.NextCursor == "" {
break
}
}

return users, nil
Expand Down Expand Up @@ -380,13 +393,13 @@ func (c *SlackClient) UsernameForID(id string) (string, error) {
return name, nil
}

ur, err := c.users(nil)
ur, err := c.users()
if err != nil {
return "", err
}

c.cache.Users = make(map[string]string)
for _, ch := range ur.Members {
for _, ch := range ur {
c.cache.Users[ch.ID] = ch.Name
}

Expand Down

0 comments on commit ecda780

Please sign in to comment.