Skip to content
Open
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
49 changes: 34 additions & 15 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,49 @@ func (m *Client) GetStatus(userID string) string {
}

func (m *Client) GetStatuses() map[string]string {
var ids []string
statuses := make(map[string]string, len(m.Users))

statuses := make(map[string]string)
const batchSize = 5000
batch := make([]string, 0, batchSize)

for id := range m.Users {
ids = append(ids, id)
}
// Inline helper to handle the API call and mapping for a specific batch
processBatch := func(ids []string) error {
res, _, err := m.Client.GetUsersStatusesByIds(context.TODO(), ids)
if err != nil {
return err
}

res, _, err := m.Client.GetUsersStatusesByIds(context.TODO(), ids)
if err != nil {
return statuses
for _, status := range res {
switch status.Status {
case model.StatusOnline:
statuses[status.UserId] = "online"
case model.StatusAway:
statuses[status.UserId] = "away"
default:
statuses[status.UserId] = "offline"
}
}
return nil
}

for _, status := range res {
statuses[status.UserId] = "offline"
if status.Status == model.StatusAway {
statuses[status.UserId] = "away"
}
for id := range m.Users {
batch = append(batch, id)

if status.Status == model.StatusOnline {
statuses[status.UserId] = "online"
// Once we hit the batch limit, execute the API call
if len(batch) == batchSize {
if err := processBatch(batch); err != nil {
return statuses
}
// Reset the batch slice length to 0, while keeping its underlying capacity
batch = batch[:0]
}
}

// Catch any remaining IDs that didn't cleanly fill up the final batch
if len(batch) > 0 {
_ = processBatch(batch)
}

return statuses
}

Expand Down