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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package slack

import (
	"context"
	"fmt"
	"net/url"
	"strings"
	"sync"
	"time"

	"github.com/pkg/errors"
	"github.com/slack-go/slack"
	"github.com/slack-go/slack/slackutilsx"
	"github.com/target/goalert/config"
	"github.com/target/goalert/notification"
	"github.com/target/goalert/notification/nfydest"
	"github.com/target/goalert/permission"
	"github.com/target/goalert/util/log"
	"github.com/target/goalert/validation"
)

type ChannelSender struct {
	cfg Config

	chanCache *ttlCache[string, *Channel]
	listCache *ttlCache[string, []Channel]
	ugCache   *ttlCache[string, []slack.UserGroup]

	botInfoCache  *ttlCache[string, *slack.AuthTestResponse]
	teamInfoCache *ttlCache[string, *slack.TeamInfo]
	userInfoCache *ttlCache[string, *slack.User]
	ugInfoCache   *ttlCache[string, UserGroup]

	listMx sync.Mutex
	chanMx sync.Mutex
	teamMx sync.Mutex

	teamInfoMx sync.Mutex

	recv notification.Receiver
}

const (
	colorClosed  = "#218626"
	colorUnacked = "#862421"
	colorAcked   = "#867321"
)

var (
	_ nfydest.MessageSender       = &ChannelSender{}
	_ notification.ReceiverSetter = &ChannelSender{}
)

func NewChannelSender(ctx context.Context, cfg Config) (*ChannelSender, error) {
	return &ChannelSender{
		cfg: cfg,

		listCache: newTTLCache[string, []Channel](250, time.Minute),
		chanCache: newTTLCache[string, *Channel](1000, 15*time.Minute),
		ugCache:   newTTLCache[string, []slack.UserGroup](1000, time.Minute),

		botInfoCache:  newTTLCache[string, *slack.AuthTestResponse](1, 15*time.Minute),
		teamInfoCache: newTTLCache[string, *slack.TeamInfo](1, 15*time.Minute),
		userInfoCache: newTTLCache[string, *slack.User](1000, 15*time.Minute),
		ugInfoCache:   newTTLCache[string, UserGroup](1000, 15*time.Minute),
	}, nil
}

func (s *ChannelSender) SetReceiver(r notification.Receiver) {
	s.recv = r
}

// Channel contains information about a Slack channel.
type Channel struct {
	ID     string
	Name   string
	TeamID string

	IsArchived bool
}

func (c Channel) AsField() nfydest.FieldValue {
	return nfydest.FieldValue{
		Value: c.ID,
		Label: c.Name,
	}
}

// User contains information about a Slack user.
type User struct {
	ID     string
	Name   string
	TeamID string
}

// Team contains information about a Slack team.
type Team struct {
	ID      string
	Domain  string
	Name    string
	IconURL string
}

func (t Team) ChannelLink(id string) string {
	var u url.URL

	u.Host = t.Domain + ".slack.com"
	u.Scheme = "https"
	u.Path = "/archives/" + url.PathEscape(id)

	return u.String()
}

func (t Team) UserLink(id string) string {
	var u url.URL

	u.Host = t.Domain + ".slack.com"
	u.Scheme = "https"
	u.Path = "/team/" + url.PathEscape(id)

	return u.String()
}

func rootMsg(err error) string {
	if err == nil {
		return ""
	}
	unwrapped := errors.Unwrap(err)
	if unwrapped == nil {
		return err.Error()
	}

	return rootMsg(unwrapped)
}

func mapError(ctx context.Context, err error) error {
	switch rootMsg(err) {
	case "channel_not_found":
		return validation.NewFieldError("ChannelID", "Channel does not exist, is archived, or is private (invite goalert bot).")
	case "missing_scope", "invalid_auth", "account_inactive", "token_revoked", "not_authed":
		log.Log(ctx, err)
		return validation.NewFieldError("ChannelID", "Permission Denied.")
	}

	return err
}

func (s *ChannelSender) ValidateChannel(ctx context.Context, id string) error {
	err := permission.LimitCheckAny(ctx, permission.User, permission.System)
	if err != nil {
		return err
	}

	if id == "" {
		return validation.NewGenericError("Channel is required.")
	}

	s.chanMx.Lock()
	defer s.chanMx.Unlock()
	res, ok := s.chanCache.Get(id)
	if !ok {
		res, err = s.loadChannel(ctx, id)
		if err != nil {
			if rootMsg(err) == "channel_not_found" {
				return validation.NewGenericError("Channel does not exist, is archived, or is private (invite goalert bot).")
			}

			return err
		}
		s.chanCache.Add(id, res)
	}

	if res.IsArchived {
		return validation.NewGenericError("Channel is archived.")
	}

	return nil
}

// Channel will lookup a single Slack channel for the bot.
func (s *ChannelSender) Channel(ctx context.Context, channelID string) (*Channel, error) {
	err := permission.LimitCheckAny(ctx, permission.User, permission.System)
	if err != nil {
		return nil, err
	}

	s.chanMx.Lock()
	defer s.chanMx.Unlock()
	res, ok := s.chanCache.Get(channelID)
	if !ok {
		ch, err := s.loadChannel(ctx, channelID)
		if err != nil {
			return nil, mapError(ctx, err)
		}
		s.chanCache.Add(channelID, ch)
		return ch, nil
	}

	return res, nil
}

func (s *ChannelSender) Team(ctx context.Context, id string) (t *Team, err error) {
	s.teamInfoMx.Lock()
	defer s.teamInfoMx.Unlock()

	info, ok := s.teamInfoCache.Get(id)
	if ok {
		url, _ := info.Icon["image_44"].(string)
		return &Team{
			ID:      info.ID,
			Name:    info.Name,
			IconURL: url,
			Domain:  info.Domain,
		}, nil
	}

	err = s.withClient(ctx, func(c *slack.Client) error {
		info, err := c.GetTeamInfoContext(ctx)
		if err != nil {
			return err
		}

		url, _ := info.Icon["image_44"].(string)
		t = &Team{
			ID:      info.ID,
			Name:    info.Name,
			IconURL: url,
			Domain:  info.Domain,
		}

		s.teamInfoCache.Add(id, info)
		return nil
	})

	return t, err
}

func (s *ChannelSender) TeamID(ctx context.Context) (string, error) {
	info, err := s.tokenInfo(ctx)
	if err != nil {
		return "", fmt.Errorf("lookup team ID for token: %w", err)
	}

	return info.TeamID, nil
}

func (s *ChannelSender) loadChannel(ctx context.Context, channelID string) (*Channel, error) {
	teamID, err := s.TeamID(ctx)
	if err != nil {
		return nil, fmt.Errorf("lookup team ID: %w", err)
	}

	ch := &Channel{TeamID: teamID}
	err = s.withClient(ctx, func(c *slack.Client) error {
		resp, err := c.GetConversationInfoContext(ctx,
			&slack.GetConversationInfoInput{
				ChannelID: channelID,
			})
		if err != nil {
			return err
		}

		ch.ID = resp.ID
		ch.Name = "#" + resp.Name
		ch.IsArchived = resp.IsArchived

		return nil
	})
	if err != nil {
		return nil, fmt.Errorf("lookup conversation info: %w", err)
	}

	return ch, nil
}

// ListChannels will return a list of channels visible to the slack bot.
func (s *ChannelSender) ListChannels(ctx context.Context) ([]Channel, error) {
	err := permission.LimitCheckAny(ctx, permission.User, permission.System)
	if err != nil {
		return nil, err
	}

	cfg := config.FromContext(ctx)
	s.listMx.Lock()
	defer s.listMx.Unlock()
	res, ok := s.listCache.Get(cfg.Slack.AccessToken)
	if !ok {
		chs, err := s.loadChannels(ctx)
		if err != nil {
			return nil, mapError(ctx, err)
		}
		ch2 := make([]Channel, len(chs))
		copy(ch2, chs)
		s.listCache.Add(cfg.Slack.AccessToken, ch2)
		return chs, nil
	}

	cpy := make([]Channel, len(res))
	copy(cpy, res)

	return cpy, nil
}

func (s *ChannelSender) loadChannels(ctx context.Context) ([]Channel, error) {
	teamID, err := s.TeamID(ctx)
	if err != nil {
		return nil, fmt.Errorf("lookup team ID: %w", err)
	}

	n := 0
	var channels []Channel
	var cursor string
	for {
		n++
		if n > 10 {
			return nil, errors.New("abort after > 10 pages of Slack channels")
		}

		err = s.withClient(ctx, func(c *slack.Client) error {
			respChan, nextCursor, err := c.GetConversationsForUserContext(ctx, &slack.GetConversationsForUserParameters{
				ExcludeArchived: true,
				Types:           []string{"private_channel", "public_channel"},
				Limit:           200,
				Cursor:          cursor,
			})
			if err != nil {
				return err
			}

			cursor = nextCursor

			for _, rCh := range respChan {
				ch := Channel{
					ID:     rCh.ID,
					Name:   "#" + rCh.Name,
					TeamID: teamID,
				}
				channels = append(channels, ch)
				s.chanCache.Add(ch.ID, &ch)
			}

			return nil
		})
		if err != nil {
			return nil, fmt.Errorf("list channels: %w", err)
		}

		if cursor == "" {
			break
		}
	}

	return channels, nil
}

func alertLink(ctx context.Context, id int, summary string) string {
	cfg := config.FromContext(ctx)
	path := fmt.Sprintf("/alerts/%d", id)
	return fmt.Sprintf("<%s|Alert #%d: %s>", cfg.CallbackURL(path), id, slackutilsx.EscapeMessage(summary))
}

const (
	alertResponseBlockID = "block_alert_response"
	alertCloseActionID   = "action_alert_close"
	alertAckActionID     = "action_alert_ack"
	linkActActionID      = "action_link_account"
)

// alertMsgOption will return the slack.MsgOption for an alert-type message (e.g., notification or status update).
func alertMsgOption(ctx context.Context, callbackID string, id int, summary, logEntry string, state notification.AlertState) slack.MsgOption {
	blocks := []slack.Block{
		slack.NewSectionBlock(
			slack.NewTextBlockObject("mrkdwn", alertLink(ctx, id, summary), false, false), nil, nil),
	}

	var color string
	var actions []slack.Block
	switch state {
	case notification.AlertStateAcknowledged:
		color = colorAcked
		actions = []slack.Block{
			slack.NewDividerBlock(),
			slack.NewActionBlock(alertResponseBlockID,
				slack.NewButtonBlockElement(alertCloseActionID, callbackID, slack.NewTextBlockObject("plain_text", "Close", false, false)),
			),
		}
	case notification.AlertStateUnacknowledged:
		color = colorUnacked
		actions = []slack.Block{
			slack.NewDividerBlock(),
			slack.NewActionBlock(alertResponseBlockID,
				slack.NewButtonBlockElement(alertAckActionID, callbackID, slack.NewTextBlockObject("plain_text", "Acknowledge", false, false)),
				slack.NewButtonBlockElement(alertCloseActionID, callbackID, slack.NewTextBlockObject("plain_text", "Close", false, false)),
			),
		}
	case notification.AlertStateClosed:
		color = colorClosed
	}

	blocks = append(blocks,
		slack.NewContextBlock("", slack.NewTextBlockObject("plain_text", logEntry, false, false)),
	)
	cfg := config.FromContext(ctx)
	if len(actions) > 0 && cfg.Slack.InteractiveMessages {
		blocks = append(blocks, actions...)
	}

	return slack.MsgOptionAttachments(
		slack.Attachment{
			Color:    color,
			Fallback: fmt.Sprintf("Alert #%d: %s", id, slackutilsx.EscapeMessage(summary)),
			Blocks:   slack.Blocks{BlockSet: blocks},
		},
	)
}

func chanTS(origChannelID, externalID string) (channelID, ts string) {
	ts = externalID
	if strings.Contains(ts, ":") {
		// DMs have a channel ID and timestamp separated by a colon,
		// so we need to split them out. Trying to update a message
		// with a user ID will fail.
		channelID, ts, _ = strings.Cut(ts, ":")
	} else {
		channelID = origChannelID
	}

	return channelID, ts
}

func (s *ChannelSender) SendMessage(ctx context.Context, msg notification.Message) (*notification.SentMessage, error) {
	cfg := config.FromContext(ctx)

	// Note: We don't use cfg.ApplicationName() here since that is configured in the Slack app as the bot name.

	var opts []slack.MsgOption
	var isUpdate bool
	channelID := msg.DestArg(FieldSlackChannelID)
	if msg.DestType() == DestTypeSlackDirectMessage {
		// DMs are sent to the user ID, not the channel ID.
		channelID = msg.DestArg(FieldSlackUserID)
	}

	switch t := msg.(type) {
	case notification.Test:
		opts = append(opts, slack.MsgOptionText("This is a test message.", false))
	case notification.Verification:
		opts = append(opts, slack.MsgOptionText(fmt.Sprintf("Your verification code is: %s", t.Code), false))
	case notification.Alert:
		if t.OriginalStatus != nil {
			var ts string
			channelID, ts = chanTS(channelID, t.OriginalStatus.ProviderMessageID.ExternalID)

			// Reply in thread if we already sent a message for this alert.
			opts = append(opts,
				slack.MsgOptionTS(ts),
				slack.MsgOptionBroadcast(),
				slack.MsgOptionText(alertLink(ctx, t.AlertID, t.Summary), false),
			)
			break
		}

		opts = append(opts, alertMsgOption(ctx, t.MsgID(), t.AlertID, t.Summary, "Unacknowledged", notification.AlertStateUnacknowledged))
	case notification.AlertStatus:
		isUpdate = true
		var ts string
		channelID, ts = chanTS(channelID, t.OriginalStatus.ProviderMessageID.ExternalID)
		opts = append(opts,
			slack.MsgOptionUpdate(ts),
			alertMsgOption(ctx, t.OriginalStatus.ID, t.AlertID, t.Summary, t.LogEntry, t.NewAlertState),
		)
	case notification.AlertBundle:
		opts = append(opts, slack.MsgOptionText(
			fmt.Sprintf("Service '%s' has %d unacknowledged alerts.\n\n<%s>", slackutilsx.EscapeMessage(t.ServiceName), t.Count, cfg.CallbackURL("/services/"+t.ServiceID+"/alerts")),
			false))
	case notification.SignalMessage:
		opts = append(opts, slack.MsgOptionText(t.Param("message"), false))
	case notification.ScheduleOnCallUsers:
		opts = append(opts, slack.MsgOptionText(s.onCallNotificationText(ctx, t), false))
	default:
		return nil, errors.Errorf("unsupported message type: %T", t)
	}

	var externalID string
	err := s.withClient(ctx, func(c *slack.Client) error {
		msgChan, msgTS, err := c.PostMessageContext(ctx, channelID, opts...)
		if err != nil {
			return err
		}
		if msgChan != channelID {
			// DMs have a generated channel ID that we need to store
			// along with the timestamp that does not match the original
			// in order to update the message.
			externalID = fmt.Sprintf("%s:%s", msgChan, msgTS)
		} else {
			// For other channels, we can just store the timestamp,
			// to preserve compatibility with older versions of GoAlert.
			externalID = msgTS
		}
		return nil
	})
	if err != nil {
		return nil, err
	}

	if isUpdate {
		externalID = ""
	}

	return &notification.SentMessage{
		ExternalID: externalID,
		State:      notification.StateDelivered,
	}, nil
}

func (s *ChannelSender) tokenInfo(ctx context.Context) (*slack.AuthTestResponse, error) {
	cfg := config.FromContext(ctx)

	s.teamMx.Lock()
	defer s.teamMx.Unlock()

	info, ok := s.botInfoCache.Get(cfg.Slack.AccessToken)
	if ok {
		return info, nil
	}

	var err error
	err = s.withClient(ctx, func(c *slack.Client) error {
		info, err = c.AuthTestContext(ctx)
		if err != nil {
			return err
		}

		s.botInfoCache.Add(cfg.Slack.AccessToken, info)
		return nil
	})
	if err != nil {
		return nil, fmt.Errorf("lookup bot info: %w", err)
	}

	return info, nil
}

// BotName returns the bot name from Slack's auth.test API
func (s *ChannelSender) BotName(ctx context.Context) (string, error) {
	info, err := s.tokenInfo(ctx)
	if err != nil {
		return "", fmt.Errorf("lookup team ID for token: %w", err)
	}

	return info.User, nil
}