Skip to content

Commit

Permalink
🚤 reduce user fetch calls for slack conversation members (#3168)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock authored Jan 31, 2024
1 parent 0cacfbd commit 7e5ec58
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions providers/slack/resources/conversations.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,24 @@ func (s *mqlSlackConversation) members() ([]interface{}, error) {
return nil, errors.New("cannot retrieve new data while using a mock connection")
}

// before we fetch the members, we need to fetch all users so that we save the time for individual lookups
users, err := CreateResource(s.MqlRuntime, "slack.users", nil)
if err != nil {
return nil, err
}
mqlUsers := users.(*mqlSlackUsers)
userEntries := mqlUsers.GetList()

findUser := func(id string) *mqlSlackUser {
for i := range userEntries.Data {
user := userEntries.Data[i].(*mqlSlackUser)
if user.Id.Data == id {
return user
}
}
return nil
}

var list []interface{}
isChannel := s.IsChannel.Data
if !isChannel {
Expand All @@ -194,11 +212,10 @@ func (s *mqlSlackConversation) members() ([]interface{}, error) {
}

for i := range members {
user, err := NewResource(s.MqlRuntime, "slack.user", map[string]*llx.RawData{
"id": llx.StringData(members[i]),
})
if err != nil {
return nil, err

user := findUser(members[i])
if user == nil {
return nil, errors.New("could not find user " + members[i])
}
list = append(list, user)
}
Expand Down

0 comments on commit 7e5ec58

Please sign in to comment.