-
Notifications
You must be signed in to change notification settings - Fork 0
/
composition.go
594 lines (492 loc) · 13.5 KB
/
composition.go
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
package flocksofblocks
import "net/url"
type Composition interface {
compositeRender() string
}
type CompositionType string
// stringer
func (c CompositionType) String() string {
return string(c)
}
func (c CompositionText) Section() Section {
return NewSection().SetText(c)
}
const (
PlainText CompositionType = "plain_text"
Mrkdwn CompositionType = "mrkdwn"
)
///////////////////////////////
// CompositionText
type CompositionText struct {
slackType CompositionType
text string
emoji bool
verbatim bool
}
// NewPlainText Create a new markdown object
func NewPlainText(text string) CompositionText {
return newText("plain_text", text)
}
// NewMrkdwnText Create a new markdown compositiontext object
func NewMrkdwnText(text string) CompositionText {
return newText("mrkdwn", text)
}
func newText(slackType CompositionType, text string) CompositionText {
return CompositionText{
slackType: slackType,
text: text,
}
}
// SetEmoji set the emoji flag
func (m *CompositionText) setEmoji(emoji bool) {
m.emoji = emoji
}
// EnableEmoji enable the emoji flag
func (m CompositionText) EnableEmoji() CompositionText {
m.setEmoji(true)
return m
}
// SetVerbatim set the verbatim flag
func (m *CompositionText) SetVerbatim(verbatim bool) {
m.verbatim = verbatim
}
// Stringer that returns the compositiontext
func (m CompositionText) String() string {
return m.text
}
// compositionTextAbstraction is used to render the composition compositiontext
type compositionTextAbstraction struct {
Type string
Text string
Emoji bool
Verbatim bool
}
// abstraction for the text
func (m CompositionText) abstraction() compositionTextAbstraction {
return compositionTextAbstraction{
Type: m.slackType.String(),
Text: m.text,
Emoji: m.emoji,
Verbatim: m.verbatim,
}
}
func (m compositionTextAbstraction) Template() string {
return `{
"type": "{{.Type}}",
"text": "{{.Text}}"{{if .Emoji}},
"emoji": {{.Emoji}}{{end}}{{if .Verbatim}},
"verbatim": {{.Verbatim}}{{end}}
}`
}
// Render the compositiontext
func (m CompositionText) Render() string {
return Render(m.abstraction())
}
///////////////////////////////
// ConfirmationDialog
type ConfirmationDialog struct {
title CompositionText
// text is a CompositionText object which can be either a PlainText or a MarkdownText
text CompositionText
confirm CompositionText
deny CompositionText
style ColorSchema
optionals ConfirmationDialogOptions
}
// todo make abstraction for this
// ConfirmationDialogOptions struct
type ConfirmationDialogOptions struct {
Style bool
}
// NewConfirmationDialog creates a new confirmation dialog
// todo: might consider making better input names
func NewConfirmationDialog(title string, text string, confirm string, deny string) ConfirmationDialog {
return ConfirmationDialog{
title: NewPlainText(title),
text: NewPlainText(text),
confirm: NewPlainText(confirm),
deny: NewPlainText(deny),
optionals: ConfirmationDialogOptions{
Style: false,
},
}
}
// set the style
func (c *ConfirmationDialog) setStyle(style ColorSchema) {
c.style = style
c.optionals.Style = true
}
// set the style public
func (c ConfirmationDialog) SetStyle(style ColorSchema) ConfirmationDialog {
c.setStyle(style)
return c
}
// confirmationDialogAbstraction is used to render the confirmation dialog
type confirmationDialogAbstraction struct {
Title CompositionText
Text CompositionText
Confirm CompositionText
Deny CompositionText
Style string
Optional ConfirmationDialogOptions
}
// create an abstraction for the template
func (c ConfirmationDialog) abstraction() confirmationDialogAbstraction {
return confirmationDialogAbstraction{
Title: c.title,
Text: c.text,
Confirm: c.confirm,
Deny: c.deny,
Style: c.style.String(),
Optional: c.optionals,
}
}
// create the template
func (c confirmationDialogAbstraction) Template() string {
return `{
"title": {{.Title.Render}},
"text": {{.Text.Render}},
"confirm": {{.Confirm.Render}},
"deny": {{.Deny.Render}}
{{if .Optionals.Style}},
"style": "{{.Style}}"
{{end}}
}`
}
// Render the template
func (c ConfirmationDialog) Render() string {
raw := Render(c.abstraction())
return Pretty(raw)
}
///////////////////////////////
// DispatchActionTypes
type DispatchActionTypes string
const (
OnEnterPressed DispatchActionTypes = "on_enter_pressed"
OnCharacterEntered DispatchActionTypes = "on_character_entered"
)
type DispatchActionConfig struct {
triggerActionsOn []DispatchActionTypes
}
type abstractionDispatchActionConfig struct {
TriggerActionsOn []string
}
func NewDispatchActionConfig() DispatchActionConfig {
return DispatchActionConfig{
triggerActionsOn: []DispatchActionTypes{},
}
}
func (d *DispatchActionConfig) setTriggerActionsOn(triggerActionsOn DispatchActionTypes) {
d.triggerActionsOn = append(d.triggerActionsOn, triggerActionsOn)
}
func (d *DispatchActionConfig) removeTriggerActionsOn() {
d.triggerActionsOn = []DispatchActionTypes{}
}
// OnEnterPressed chain function to add on_enter_pressed to an existing dispatch action config
func (d DispatchActionConfig) OnEnterPressed() DispatchActionConfig {
d.setTriggerActionsOn(OnEnterPressed)
return d
}
// OnCharacterEntered chain function to add on_character_entered to an existing dispatch action config
func (d DispatchActionConfig) OnCharacterEntered() DispatchActionConfig {
d.setTriggerActionsOn(OnCharacterEntered)
return d
}
// RemoveTriggerActionsOn remove add trigger actions on from dispatch action config
func (d DispatchActionConfig) RemoveTriggerActions() DispatchActionConfig {
d.removeTriggerActionsOn()
return d
}
// Template generates the template for the block
func (d abstractionDispatchActionConfig) Template() string {
return `{
"trigger_actions_on": [{{range $index, $element := .TriggerActionsOn}}{{if $index}}, {{end}}"{{$element}}"{{end}}]
}`
}
// abstraction
func (d DispatchActionConfig) abstraction() abstractionDispatchActionConfig {
return abstractionDispatchActionConfig{
TriggerActionsOn: removeDuplicateStr(d.triggerActionsOn),
}
}
// Render the block
func (d DispatchActionConfig) Render() string {
output := Render(d.abstraction())
return Pretty(output)
}
func removeDuplicateStr(strSlice []DispatchActionTypes) []string {
allKeys := make(map[string]bool)
list := []string{}
for _, item := range strSlice {
item := string(item)
if _, value := allKeys[item]; !value {
allKeys[item] = true
list = append(list, item)
}
}
return list
}
///////////////////////////////
// Filter
type Filter struct {
include []string
excludeExternalSharedChannels bool
excludeBotUsers bool
optionals filterOptions
}
type filterOptions struct {
Include bool
ExcludeExternalSharedChannels bool
ExcludeBotUsers bool
}
func NewFilter() Filter {
return Filter{
include: []string{},
excludeExternalSharedChannels: false,
excludeBotUsers: false,
optionals: filterOptions{
Include: false,
ExcludeExternalSharedChannels: false,
ExcludeBotUsers: false,
},
}
}
// abstracted type
type filterAbstraction struct {
Include []string
ExcludeExternalSharedChannels bool
ExcludeBotUsers bool
Optionals filterOptions
}
// add include to filter
func (f *Filter) addInclude(include string) {
f.include = append(f.include, include)
f.optionals.Include = true
}
// AddInclude add filter string
func (f Filter) AddInclude(include string) Filter {
f.addInclude(include)
return f
}
// IncludeIM Add IM to include filter
func (f Filter) IncludeIM() Filter {
return f.AddInclude("im")
}
// IncludeMPIM Add MPIM to include filter
func (f Filter) IncludeMPIM() Filter {
return f.AddInclude("mpim")
}
// IncludePrivate Add private to include filter
func (f Filter) IncludePrivate() Filter {
return f.AddInclude("private")
}
// IncludePublic Add public to include filter
func (f Filter) IncludePublic() Filter {
return f.AddInclude("public")
}
// ClearInclude clear include
func (f *Filter) clearInclude() {
f.include = []string{}
f.optionals.Include = false
}
// ClearInclude clear include
func (f Filter) ClearInclude() Filter {
f.clearInclude()
return f
}
// set exclude external shared channels
func (f *Filter) setExcludeExternalSharedChannels(excludeExternalSharedChannels bool) {
f.excludeExternalSharedChannels = excludeExternalSharedChannels
f.optionals.ExcludeExternalSharedChannels = excludeExternalSharedChannels
}
// ExcludeExternalSharedChannels exclude external shared channels
func (f Filter) ExcludeExternalSharedChannels() Filter {
f.setExcludeExternalSharedChannels(true)
return f
}
// UnsetExcludeExternalSharedChannels unset exclude external shared channels
func (f Filter) UnsetExcludeExternalSharedChannels() Filter {
f.setExcludeExternalSharedChannels(false)
return f
}
// set exclude bot users
func (f *Filter) setExcludeBotUsers(excludeBotUsers bool) {
f.excludeBotUsers = excludeBotUsers
f.optionals.ExcludeBotUsers = excludeBotUsers
}
// ExcludeBotUsers exclude bot users
func (f Filter) ExcludeBotUsers() Filter {
f.setExcludeBotUsers(true)
return f
}
// UnsetExcludeBotUsers unset exclude bot users
func (f Filter) UnsetExcludeBotUsers() Filter {
f.setExcludeBotUsers(false)
return f
}
// abstraction
func (f Filter) abstraction() filterAbstraction {
return filterAbstraction{
Include: removeDuplicateString(f.include),
ExcludeExternalSharedChannels: f.excludeExternalSharedChannels,
ExcludeBotUsers: f.excludeBotUsers,
Optionals: f.optionals,
}
}
func (f filterAbstraction) empty() bool {
if f.Optionals.Include {
return false
}
if f.Optionals.ExcludeExternalSharedChannels {
return false
}
if f.Optionals.ExcludeBotUsers {
return false
}
return true
}
// template
func (f filterAbstraction) Template() string {
if f.empty() {
return ""
}
return `"filter": {
{{if .Optionals.Include}}"include": [{{range $index, $include := .Include}}{{if $index}},{{end}}"{{ $include}}"{{end}}]{{if .Optionals.ExcludeExternalSharedChannels}},{{end}}{{end}}{{if .Optionals.ExcludeExternalSharedChannels}}
"exclude_external_shared_channels": {{.ExcludeExternalSharedChannels}}{{if .Optionals.ExcludeBotUsers }},{{end}}{{end}}{{if .Optionals.ExcludeBotUsers }}
"exclude_bot_users": {{.ExcludeBotUsers}}{{end}}
}`
}
// Render method
func (f Filter) Render() string {
return Render(f.abstraction())
}
// im, mpim, private, and public.
type Option struct {
// Required
text CompositionText
value string
// Optionals
description CompositionText
url url.URL
optionals optional
}
type optional struct {
Description bool
Url bool
}
type optionOption func(*Option)
// NewOption creates a new option.
func NewOption(text string, value string) Option {
return Option{
text: NewPlainText(text),
value: value,
}
}
func (o *Option) setDescription(description CompositionText) {
o.description = description
o.optionals.Description = true
}
func (o *Option) setUrl(url url.URL) {
o.url = url
o.optionals.Url = true
}
func (o Option) SetDescription(description CompositionText) Option {
o.setDescription(description)
return o
}
func (o *Option) SetUrl(url url.URL) *Option {
o.setUrl(url)
return o
}
func (o Option) RemoveDescription() Option {
o.optionals.Description = false
return o
}
func (o Option) RemoveUrl() Option {
o.optionals.Url = false
return o
}
// optionAbstraction is used to render the option
type optionAbstraction struct {
Text CompositionText
Value string
Description CompositionText
Url string
Optionals optional
}
// create an option abstraction for rendering
func (o Option) abstraction() optionAbstraction {
url := ""
if o.optionals.Url {
url = o.url.String()
}
return optionAbstraction{
Text: o.text,
Value: o.value,
Description: o.description,
Url: url,
Optionals: o.optionals,
}
}
func (o optionAbstraction) Template() string {
return `{
"text": {{ .Text.Render}},
"value": "{{.Value}}"{{if .Optionals.Description}},
"description": {{.Description.Render}}{{end}}{{if .Optionals.Url}},
"url": "{{.Url}}"{{end}}
}`
}
// Render renders the option to a string.
func (o Option) Render() string {
return Render(o.abstraction())
}
/////////////////////////
// OptionGroup
type OptionGroup struct {
label CompositionText
options []Option
}
func NewOptionGroup(label string) OptionGroup {
return OptionGroup{
label: NewPlainText(label),
options: []Option{},
}
}
// setLabel sets the label for the block.
func (o *OptionGroup) setLabel(label CompositionText) {
o.label = label
}
// SetLabel sets the label for the block.
func (o OptionGroup) SetLabel(label string) OptionGroup {
o.setLabel(NewPlainText(label))
return o
}
func (o OptionGroup) AddOption(option Option) OptionGroup {
o.options = append(o.options, option)
return o
}
// compositionOptionAbstraction is used to render the composition option
type optionGroupAbstraction struct {
Label CompositionText
Options []Option
}
// generate the abstraction for the block
func (o OptionGroup) abstraction() optionGroupAbstraction {
return optionGroupAbstraction{
Label: o.label,
Options: o.options,
}
}
// Render renders the block to a string.
func (o OptionGroup) Render() string {
return Render(o.abstraction())
}
// Template returns the template for the block.
func (o optionGroupAbstraction) Template() string {
return `{
"label": {{.Label.Render}},
"options": [
{{range $index, $option := .Options}}{{if $index}},{{end}}{{ $option.Render}}{{end}}
]
}`
}