slacknotifier,livenote: fix slack user lookup and add one time mention

This commit is contained in:
c9s 2024-11-08 13:12:23 +08:00
parent 4b24e9a480
commit d2b971d75e
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 75 additions and 17 deletions

View File

@ -2,11 +2,30 @@ package livenote
type Option interface{} type Option interface{}
type Mention struct { type OptionCompare struct {
User string Value bool
} }
type Comment struct { func CompareObject(value bool) *OptionCompare {
return &OptionCompare{Value: value}
}
type OptionOneTimeMention struct {
Users []string
}
func OneTimeMention(users ...string) *OptionOneTimeMention {
return &OptionOneTimeMention{Users: users}
}
type OptionComment struct {
Text string Text string
Users []string Users []string
} }
func Comment(text string, users ...string) *OptionComment {
return &OptionComment{
Text: text,
Users: users,
}
}

View File

@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"regexp" "regexp"
"strings"
"time" "time"
"golang.org/x/time/rate" "golang.org/x/time/rate"
@ -108,6 +109,20 @@ var userIdRegExp = regexp.MustCompile(`^<@(.+?)>$`)
// groupIdRegExp matches strings like <!subteam^ID> // groupIdRegExp matches strings like <!subteam^ID>
var groupIdRegExp = regexp.MustCompile(`^<!subteam\^(.+?)>$`) var groupIdRegExp = regexp.MustCompile(`^<!subteam\^(.+?)>$`)
func (n *Notifier) translateHandles(ctx context.Context, handles []string) ([]string, error) {
var tags []string
for _, handle := range handles {
tag, err := n.translateHandle(ctx, handle)
if err != nil {
return nil, err
}
tags = append(tags, tag)
}
return tags, nil
}
func (n *Notifier) translateHandle(ctx context.Context, handle string) (string, error) { func (n *Notifier) translateHandle(ctx context.Context, handle string) (string, error) {
if handle == "" { if handle == "" {
return "", errors.New("handle is empty") return "", errors.New("handle is empty")
@ -133,7 +148,7 @@ func (n *Notifier) translateHandle(ctx context.Context, handle string) (string,
slackUser, err := n.client.GetUserInfoContext(ctx, handle) slackUser, err := n.client.GetUserInfoContext(ctx, handle)
if err != nil { if err != nil {
return "", err return "", fmt.Errorf("user handle %s not found: %v", handle, err)
} }
if slackUser != nil { if slackUser != nil {
@ -144,14 +159,6 @@ func (n *Notifier) translateHandle(ctx context.Context, handle string) (string,
return "", fmt.Errorf("handle %s not found", handle) return "", fmt.Errorf("handle %s not found", handle)
} }
func toUserHandle(id string) string {
return fmt.Sprintf("<@%s>", id)
}
func toSubteamHandle(id string) string {
return fmt.Sprintf("<!subteam^%s>", id)
}
func (n *Notifier) PostLiveNote(obj livenote.Object, opts ...livenote.Option) error { func (n *Notifier) PostLiveNote(obj livenote.Object, opts ...livenote.Option) error {
note := n.liveNotePool.Update(obj) note := n.liveNotePool.Update(obj)
ctx := context.Background() ctx := context.Background()
@ -171,20 +178,31 @@ func (n *Notifier) PostLiveNote(obj livenote.Object, opts ...livenote.Option) er
var slackOpts []slack.MsgOption var slackOpts []slack.MsgOption
slackOpts = append(slackOpts, slack.MsgOptionAttachments(attachment)) slackOpts = append(slackOpts, slack.MsgOptionAttachments(attachment))
var userIds []string var firstTimeHandles []string
var mentions []*livenote.OptionMention var commentHandles []string
var mentions []*livenote.OptionOneTimeMention
var comments []*livenote.OptionComment var comments []*livenote.OptionComment
for _, opt := range opts { for _, opt := range opts {
switch val := opt.(type) { switch val := opt.(type) {
case *livenote.OptionMention: case *livenote.OptionOneTimeMention:
mentions = append(mentions, val) mentions = append(mentions, val)
userIds = append(userIds, val.Users...) firstTimeHandles = append(firstTimeHandles, val.Users...)
case *livenote.OptionComment: case *livenote.OptionComment:
comments = append(comments, val) comments = append(comments, val)
userIds = append(userIds, val.Users...) commentHandles = append(commentHandles, val.Users...)
} }
} }
firstTimeTags, err := n.translateHandles(context.Background(), firstTimeHandles)
if err != nil {
return err
}
commentTags, err := n.translateHandles(context.Background(), commentHandles)
if err != nil {
return err
}
// format: mention slack user // format: mention slack user
// <@U012AB3CD> // <@U012AB3CD>
@ -197,6 +215,8 @@ func (n *Notifier) PostLiveNote(obj livenote.Object, opts ...livenote.Option) er
return err return err
} }
_ = commentTags
} else { } else {
respCh, respTs, err := n.client.PostMessageContext(ctx, channel, slackOpts...) respCh, respTs, err := n.client.PostMessageContext(ctx, channel, slackOpts...)
if err != nil { if err != nil {
@ -208,6 +228,13 @@ func (n *Notifier) PostLiveNote(obj livenote.Object, opts ...livenote.Option) er
note.SetChannelID(respCh) note.SetChannelID(respCh)
note.SetMessageID(respTs) note.SetMessageID(respTs)
_, _, err = n.client.PostMessageContext(ctx, channel,
slack.MsgOptionText(joinTags(firstTimeTags), false),
slack.MsgOptionTS(respTs))
if err != nil {
return err
}
} }
return nil return nil
@ -310,3 +337,15 @@ func (n *Notifier) SendPhoto(buffer *bytes.Buffer) {
func (n *Notifier) SendPhotoTo(channel string, buffer *bytes.Buffer) { func (n *Notifier) SendPhotoTo(channel string, buffer *bytes.Buffer) {
// TODO // TODO
} }
func toUserHandle(id string) string {
return fmt.Sprintf("<@%s>", id)
}
func toSubteamHandle(id string) string {
return fmt.Sprintf("<!subteam^%s>", id)
}
func joinTags(tags []string) string {
return strings.Join(tags, " ")
}