Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reporting real name for Slack interactive events #1039

Merged
merged 1 commit into from
Apr 12, 2023
Merged
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
39 changes: 28 additions & 11 deletions pkg/bot/socketslack.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type SocketSlack struct {
botMentionRegex *regexp.Regexp
commGroupName string
renderer *SlackRenderer
realNamesForID map[string]string
}

type socketSlackMessage struct {
Expand Down Expand Up @@ -96,6 +97,7 @@ func NewSocketSlack(log logrus.FieldLogger, commGroupName string, cfg config.Soc
commGroupName: commGroupName,
renderer: NewSlackRenderer(),
botMentionRegex: botMentionRegex,
realNamesForID: map[string]string{},
}, nil
}

Expand Down Expand Up @@ -143,23 +145,16 @@ func (b *SocketSlack) Start(ctx context.Context) error {
switch ev := innerEvent.Data.(type) {
case *slackevents.AppMentionEvent:
b.log.Debugf("Got app mention %s", formatx.StructDumper().Sdump(innerEvent))
userName := b.getRealNameWithFallbackToUserID(ctx, ev.User)
msg := socketSlackMessage{
Text: ev.Text,
Channel: ev.Channel,
ThreadTimeStamp: ev.ThreadTimeStamp,
UserID: ev.User,
UserName: ev.User, // set to user ID if we can't get the real name
UserName: userName,
CommandOrigin: command.TypedOrigin,
}

user, err := b.client.GetUserInfoContext(ctx, ev.User)
if err != nil {
b.log.Errorf("while getting user info: %s", err.Error())
}
if user != nil && user.RealName != "" {
msg.UserName = user.RealName
}

if err := b.handleMessage(ctx, msg); err != nil {
b.log.Errorf("while handling message: %s", err.Error())
}
Expand Down Expand Up @@ -211,13 +206,14 @@ func (b *SocketSlack) Start(ctx context.Context) error {

state := removeBotNameFromIDs(b.BotName(), callback.BlockActionState)

userName := b.getRealNameWithFallbackToUserID(ctx, callback.User.ID)
msg := socketSlackMessage{
Text: cmd,
Channel: channelID,
ThreadTimeStamp: threadTs,
TriggerID: callback.TriggerID,
UserID: callback.User.ID,
UserName: callback.User.RealName,
UserName: userName,
CommandOrigin: cmdOrigin,
State: state,
ResponseURL: callback.ResponseURL,
Expand All @@ -234,11 +230,12 @@ func (b *SocketSlack) Start(ctx context.Context) error {
act.ActionID = actID // normalize event

cmd, cmdOrigin := resolveBlockActionCommand(act)
userName := b.getRealNameWithFallbackToUserID(ctx, callback.User.ID)
msg := socketSlackMessage{
Text: cmd,
Channel: callback.View.PrivateMetadata,
UserID: callback.User.ID,
UserName: callback.User.RealName,
UserName: userName,
CommandOrigin: cmdOrigin,
}

Expand Down Expand Up @@ -565,3 +562,23 @@ func (b *SocketSlack) getThreadOptionIfNeeded(event socketSlackMessage, file *sl

return nil
}

func (b *SocketSlack) getRealNameWithFallbackToUserID(ctx context.Context, userID string) string {
realName, exists := b.realNamesForID[userID]
if exists {
return realName
}

user, err := b.client.GetUserInfoContext(ctx, userID)
if err != nil {
b.log.Errorf("while getting user info: %s", err.Error())
return userID
}

if user == nil || user.RealName == "" {
return userID
}

b.realNamesForID[userID] = user.RealName
return user.RealName
}