Skip to content

Commit

Permalink
Merge branch 'main' into enhance/ci/deps
Browse files Browse the repository at this point in the history
  • Loading branch information
wass3rw3rk committed Jun 20, 2023
2 parents 013973b + 81b39ee commit e2ff0c9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 61 deletions.
1 change: 0 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"type": "go",
"request": "launch",
"mode": "exec",
"remotePath": "",
"host": "127.0.0.1",
"program": "${workspaceRoot}/debug",
"preLaunchTask": "build-debug",
Expand Down
95 changes: 38 additions & 57 deletions remote/slack/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"strconv"
"strings"
"time"

"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -268,11 +269,15 @@ func getSlackUsers(api *slack.Client, message models.Message) ([]slack.User, err
slackUsers := []slack.User{}
// grab list of users to message if 'output_to_users' was specified
if len(message.OutputToUsers) > 0 {
start := time.Now()

res, err := api.GetUsers()
if err != nil {
return []slack.User{}, fmt.Errorf("did not find any users listed in 'output_to_users': %w", err)
}

log.Info().Msgf("fetched %d users in %s", len(res), time.Since(start).String())

slackUsers = res
}

Expand Down Expand Up @@ -326,8 +331,11 @@ func handleNonDirectMessage(api *slack.Client, users []slack.User, message model
}
// Is output to users set?
if len(message.OutputToUsers) > 0 {
// this assumes output to users is an email?? that's mentioned nowhere
for _, u := range message.OutputToUsers {
// Get users Slack user ID
// Get users Slack user ID based on username supplied in output to users
// which assumes that email follows certain pattern, ie. firstname.lastname -> firstname.lastname@example.com
// note: requires email scope
userID := getUserID(u, users)
if userID != "" {
// If 'direct_message_only' is 'false' but the user listed himself in the 'output_to_users'
Expand All @@ -354,19 +362,6 @@ func handleNonDirectMessage(api *slack.Client, users []slack.User, message model
return nil
}

// populateUsers populates slack users.
func populateUsers(su []slack.User, bot *models.Bot) {
users := make(map[string]string)

// create a map of users
for _, user := range su {
users[user.Name] = user.ID
}

// add users to bot
bot.Users = users
}

// populateUserGroups populates slack user groups.
func populateUserGroups(sug []slack.UserGroup, bot *models.Bot) {
userGroups := make(map[string]string)
Expand Down Expand Up @@ -451,27 +446,8 @@ func populateMessage(message models.Message, msgType models.MessageType, channel
// readFromEventsAPI utilizes the Slack API client to read event-based messages.
// This method of reading is preferred over the RTM method.
func readFromEventsAPI(api *slack.Client, vToken string, inputMsgs chan<- models.Message, bot *models.Bot) {
// get the current users
users, err := api.GetUsers()
if err != nil {
log.Error().Msgf("error getting users: %v", err)
}

// add users to the bot
if users != nil {
populateUsers(users, bot)
}

// get the user groups
usergroups, err := api.GetUserGroups()
if err != nil {
log.Error().Msgf("error getting user groups: %v", err)
}

// add user groups to the bot
if usergroups != nil {
populateUserGroups(usergroups, bot)
}
// populate user groups
go getUserGroups(api, bot)

// Create router for the events server
router := mux.NewRouter()
Expand Down Expand Up @@ -501,7 +477,7 @@ func readFromEventsAPI(api *slack.Client, vToken string, inputMsgs chan<- models
//
// https://api.slack.com/apis/connections/socket
//
//nolint:gocyclo,funlen // needs refactor
//nolint:gocyclo // needs refactor
func readFromSocketMode(sm *slack.Client, inputMsgs chan<- models.Message, bot *models.Bot) {
// setup the client
client := socketmode.New(sm)
Expand Down Expand Up @@ -613,27 +589,8 @@ func readFromSocketMode(sm *slack.Client, inputMsgs chan<- models.Message, bot *
case socketmode.EventTypeConnected:
log.Info().Msg("connected to slack with socket mode")

// get users
users, err := sm.GetUsers()
if err != nil {
log.Error().Msgf("unable to get users: %v", err)
}

// add users to bot
if users != nil {
populateUsers(users, bot)
}

// get user groups
usergroups, err := sm.GetUserGroups()
if err != nil {
log.Error().Msgf("unable to get user groups: %v", err)
}

// add user groups to bot
if usergroups != nil {
populateUserGroups(usergroups, bot)
}
// populate usergroups
go getUserGroups(sm, bot)
default:
log.Warn().Msgf("unhandled event type received: %s", evt.Type)
}
Expand All @@ -648,6 +605,9 @@ func readFromSocketMode(sm *slack.Client, inputMsgs chan<- models.Message, bot *

// send - handles the sending logic of a message going to Slack.
func send(api *slack.Client, message models.Message) {
// TODO: potentially long running call depending on workspace size
// only needed for output_to_users functionality which makes some
// unsound and restricting assumptions. refactor!
users, err := getSlackUsers(api, message)
if err != nil {
log.Error().Msgf("problem sending message: %v", err)
Expand Down Expand Up @@ -711,3 +671,24 @@ func sendMessage(api *slack.Client, ephemeral bool, channel, userID, text, threa

return err
}

// getUserGroups is a helper function to retrieve all usergroups from the workspace
// and populate the usergroup lookup on the bot object.
// this operation can take a long time on large workspaces and should be
// run in a go routine.
func getUserGroups(client *slack.Client, bot *models.Bot) {
start := time.Now()

// get the user groups
usergroups, err := client.GetUserGroups()
if err != nil {
log.Error().Msgf("error getting user groups: %v", err)
}

// add user groups to the bot
if usergroups != nil {
populateUserGroups(usergroups, bot)
}

log.Info().Msgf("fetched %d usergroups in %s", len(usergroups), time.Since(start).String())
}
15 changes: 12 additions & 3 deletions remote/slack/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package slack

import (
"time"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/slack-go/slack"
Expand Down Expand Up @@ -87,9 +89,6 @@ func (c *Client) Read(inputMsgs chan<- models.Message, _ map[string]models.Rule,
// init api client
api := c.new()

// get bot rooms
bot.Rooms = getRooms(api)

// get bot id
rat, err := api.AuthTest()
if err != nil {
Expand All @@ -98,6 +97,16 @@ func (c *Client) Read(inputMsgs chan<- models.Message, _ map[string]models.Rule,
return
}

// fetch rooms async
go func(b *models.Bot) {
start := time.Now()

// get bot rooms
b.Rooms = getRooms(api)

log.Info().Msgf("fetched %d rooms in %s", len(b.Rooms), time.Since(start).String())
}(bot)

// set the bot ID
bot.ID = rat.UserID

Expand Down

0 comments on commit e2ff0c9

Please sign in to comment.