@@ -22,13 +22,11 @@ import (
2222type ChannelSender struct {
2323 cfg Config
2424
25- teamID string
26- token string
27-
2825 chanCache * ttlCache [string , * Channel ]
2926 listCache * ttlCache [string , []Channel ]
3027 ugCache * ttlCache [string , []slack.UserGroup ]
3128
29+ botInfoCache * ttlCache [string , * slack.AuthTestResponse ]
3230 teamInfoCache * ttlCache [string , * slack.TeamInfo ]
3331 userInfoCache * ttlCache [string , * slack.User ]
3432 ugInfoCache * ttlCache [string , UserGroup ]
@@ -61,6 +59,7 @@ func NewChannelSender(ctx context.Context, cfg Config) (*ChannelSender, error) {
6159 chanCache : newTTLCache [string , * Channel ](1000 , 15 * time .Minute ),
6260 ugCache : newTTLCache [string , []slack.UserGroup ](1000 , time .Minute ),
6361
62+ botInfoCache : newTTLCache [string , * slack.AuthTestResponse ](1 , 15 * time .Minute ),
6463 teamInfoCache : newTTLCache [string , * slack.TeamInfo ](1 , 15 * time .Minute ),
6564 userInfoCache : newTTLCache [string , * slack.User ](1000 , 15 * time .Minute ),
6665 ugInfoCache : newTTLCache [string , UserGroup ](1000 , 15 * time .Minute ),
@@ -237,23 +236,12 @@ func (s *ChannelSender) Team(ctx context.Context, id string) (t *Team, err error
237236}
238237
239238func (s * ChannelSender ) TeamID (ctx context.Context ) (string , error ) {
240- cfg := config .FromContext (ctx )
241-
242- s .teamMx .Lock ()
243- defer s .teamMx .Unlock ()
244- if s .teamID == "" || s .token != cfg .Slack .AccessToken {
245- // teamID missing or token changed
246- id , err := s .lookupTeamIDForToken (ctx , cfg .Slack .AccessToken )
247- if err != nil {
248- return "" , err
249- }
250-
251- // update teamID and token after fetching succeeds
252- s .teamID = id
253- s .token = cfg .Slack .AccessToken
239+ info , err := s .tokenInfo (ctx )
240+ if err != nil {
241+ return "" , fmt .Errorf ("lookup team ID for token: %w" , err )
254242 }
255243
256- return s . teamID , nil
244+ return info . TeamID , nil
257245}
258246
259247func (s * ChannelSender ) loadChannel (ctx context.Context , channelID string ) (* Channel , error ) {
@@ -525,44 +513,40 @@ func (s *ChannelSender) SendMessage(ctx context.Context, msg notification.Messag
525513 }, nil
526514}
527515
528- func (s * ChannelSender ) lookupTeamIDForToken (ctx context.Context , token string ) (string , error ) {
529- var teamID string
516+ func (s * ChannelSender ) tokenInfo (ctx context.Context ) (* slack. AuthTestResponse , error ) {
517+ cfg := config . FromContext ( ctx )
530518
531- err := s .withClient (ctx , func (c * slack.Client ) error {
532- info , err := c .AuthTestContext (ctx )
519+ s .teamMx .Lock ()
520+ defer s .teamMx .Unlock ()
521+
522+ info , ok := s .botInfoCache .Get (cfg .Slack .AccessToken )
523+ if ok {
524+ return info , nil
525+ }
526+
527+ var err error
528+ err = s .withClient (ctx , func (c * slack.Client ) error {
529+ info , err = c .AuthTestContext (ctx )
533530 if err != nil {
534531 return err
535532 }
536533
537- teamID = info .TeamID
538-
534+ s .botInfoCache .Add (cfg .Slack .AccessToken , info )
539535 return nil
540536 })
541537 if err != nil {
542- return " " , err
538+ return nil , fmt . Errorf ( "lookup bot info: %w " , err )
543539 }
544540
545- return teamID , nil
541+ return info , nil
546542}
547543
548544// BotName returns the bot name from Slack's auth.test API
549545func (s * ChannelSender ) BotName (ctx context.Context ) (string , error ) {
550- var botName string
551-
552- err := s .withClient (ctx , func (c * slack.Client ) error {
553- info , err := c .AuthTestContext (ctx )
554- if err != nil {
555- return err
556- }
557-
558- // Use the User field from auth.test which contains the bot name
559- botName = info .User
560-
561- return nil
562- })
546+ info , err := s .tokenInfo (ctx )
563547 if err != nil {
564- return "" , err
548+ return "" , fmt . Errorf ( "lookup team ID for token: %w" , err )
565549 }
566550
567- return botName , nil
551+ return info . User , nil
568552}
0 commit comments