forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_rich_text.go
528 lines (465 loc) · 14.1 KB
/
block_rich_text.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
package slack
import (
"encoding/json"
)
// RichTextBlock defines a new block of type rich_text.
// More Information: https://api.slack.com/changelog/2019-09-what-they-see-is-what-you-get-and-more-and-less
type RichTextBlock struct {
Type MessageBlockType `json:"type"`
BlockID string `json:"block_id,omitempty"`
Elements []RichTextElement `json:"elements"`
}
func (b RichTextBlock) BlockType() MessageBlockType {
return b.Type
}
func (e *RichTextBlock) UnmarshalJSON(b []byte) error {
var raw struct {
Type MessageBlockType `json:"type"`
BlockID string `json:"block_id"`
RawElements []json.RawMessage `json:"elements"`
}
if string(b) == "{}" {
return nil
}
if err := json.Unmarshal(b, &raw); err != nil {
return err
}
elems := make([]RichTextElement, 0, len(raw.RawElements))
for _, r := range raw.RawElements {
var s struct {
Type RichTextElementType `json:"type"`
}
if err := json.Unmarshal(r, &s); err != nil {
return err
}
var elem RichTextElement
switch s.Type {
case RTESection:
elem = &RichTextSection{}
case RTEList:
elem = &RichTextList{}
case RTEQuote:
elem = &RichTextQuote{}
case RTEPreformatted:
elem = &RichTextPreformatted{}
default:
elems = append(elems, &RichTextUnknown{
Type: s.Type,
Raw: string(r),
})
continue
}
if err := json.Unmarshal(r, &elem); err != nil {
return err
}
elems = append(elems, elem)
}
*e = RichTextBlock{
Type: raw.Type,
BlockID: raw.BlockID,
Elements: elems,
}
return nil
}
// NewRichTextBlock returns a new instance of RichText Block.
func NewRichTextBlock(blockID string, elements ...RichTextElement) *RichTextBlock {
return &RichTextBlock{
Type: MBTRichText,
BlockID: blockID,
Elements: elements,
}
}
type RichTextElementType string
type RichTextElement interface {
RichTextElementType() RichTextElementType
}
const (
RTEList RichTextElementType = "rich_text_list"
RTEPreformatted RichTextElementType = "rich_text_preformatted"
RTEQuote RichTextElementType = "rich_text_quote"
RTESection RichTextElementType = "rich_text_section"
RTEUnknown RichTextElementType = "rich_text_unknown"
)
type RichTextUnknown struct {
Type RichTextElementType
Raw string
}
func (u RichTextUnknown) RichTextElementType() RichTextElementType {
return u.Type
}
type RichTextListElementType string
const (
RTEListOrdered RichTextListElementType = "ordered"
RTEListBullet RichTextListElementType = "bullet"
)
type RichTextList struct {
Type RichTextElementType `json:"type"`
Elements []RichTextElement `json:"elements"`
Style RichTextListElementType `json:"style"`
Indent int `json:"indent"`
}
// NewRichTextList returns a new rich text list element.
func NewRichTextList(style RichTextListElementType, indent int, elements ...RichTextElement) *RichTextList {
return &RichTextList{
Type: RTEList,
Elements: elements,
Style: style,
Indent: indent,
}
}
// ElementType returns the type of the Element
func (s RichTextList) RichTextElementType() RichTextElementType {
return s.Type
}
func (e *RichTextList) UnmarshalJSON(b []byte) error {
var raw struct {
RawElements []json.RawMessage `json:"elements"`
Style RichTextListElementType `json:"style"`
Indent int `json:"indent"`
}
if string(b) == "{}" {
return nil
}
if err := json.Unmarshal(b, &raw); err != nil {
return err
}
elems := make([]RichTextElement, 0, len(raw.RawElements))
for _, r := range raw.RawElements {
var s struct {
Type RichTextElementType `json:"type"`
}
if err := json.Unmarshal(r, &s); err != nil {
return err
}
var elem RichTextElement
switch s.Type {
case RTESection:
elem = &RichTextSection{}
case RTEList:
elem = &RichTextList{}
case RTEQuote:
elem = &RichTextQuote{}
case RTEPreformatted:
elem = &RichTextPreformatted{}
default:
elems = append(elems, &RichTextUnknown{
Type: s.Type,
Raw: string(r),
})
continue
}
if err := json.Unmarshal(r, elem); err != nil {
return err
}
elems = append(elems, elem)
}
*e = RichTextList{
Type: RTEList,
Elements: elems,
Style: raw.Style,
Indent: raw.Indent,
}
return nil
}
type RichTextSection struct {
Type RichTextElementType `json:"type"`
Elements []RichTextSectionElement `json:"elements"`
}
// RichTextElementType returns the type of the Element
func (s RichTextSection) RichTextElementType() RichTextElementType {
return s.Type
}
func (e *RichTextSection) UnmarshalJSON(b []byte) error {
var raw struct {
RawElements []json.RawMessage `json:"elements"`
}
if string(b) == "{}" {
return nil
}
if err := json.Unmarshal(b, &raw); err != nil {
return err
}
elems := make([]RichTextSectionElement, 0, len(raw.RawElements))
for _, r := range raw.RawElements {
var s struct {
Type RichTextSectionElementType `json:"type"`
}
if err := json.Unmarshal(r, &s); err != nil {
return err
}
var elem RichTextSectionElement
switch s.Type {
case RTSEText:
elem = &RichTextSectionTextElement{}
case RTSEChannel:
elem = &RichTextSectionChannelElement{}
case RTSEUser:
elem = &RichTextSectionUserElement{}
case RTSEEmoji:
elem = &RichTextSectionEmojiElement{}
case RTSELink:
elem = &RichTextSectionLinkElement{}
case RTSETeam:
elem = &RichTextSectionTeamElement{}
case RTSEUserGroup:
elem = &RichTextSectionUserGroupElement{}
case RTSEDate:
elem = &RichTextSectionDateElement{}
case RTSEBroadcast:
elem = &RichTextSectionBroadcastElement{}
case RTSEColor:
elem = &RichTextSectionColorElement{}
default:
elems = append(elems, &RichTextSectionUnknownElement{
Type: s.Type,
Raw: string(r),
})
continue
}
if err := json.Unmarshal(r, elem); err != nil {
return err
}
elems = append(elems, elem)
}
*e = RichTextSection{
Type: RTESection,
Elements: elems,
}
return nil
}
// NewRichTextSectionBlockElement .
func NewRichTextSection(elements ...RichTextSectionElement) *RichTextSection {
return &RichTextSection{
Type: RTESection,
Elements: elements,
}
}
type RichTextSectionElementType string
const (
RTSEBroadcast RichTextSectionElementType = "broadcast"
RTSEChannel RichTextSectionElementType = "channel"
RTSEColor RichTextSectionElementType = "color"
RTSEDate RichTextSectionElementType = "date"
RTSEEmoji RichTextSectionElementType = "emoji"
RTSELink RichTextSectionElementType = "link"
RTSETeam RichTextSectionElementType = "team"
RTSEText RichTextSectionElementType = "text"
RTSEUser RichTextSectionElementType = "user"
RTSEUserGroup RichTextSectionElementType = "usergroup"
RTSEUnknown RichTextSectionElementType = "unknown"
)
type RichTextSectionElement interface {
RichTextSectionElementType() RichTextSectionElementType
}
type RichTextSectionTextStyle struct {
Bold bool `json:"bold,omitempty"`
Italic bool `json:"italic,omitempty"`
Strike bool `json:"strike,omitempty"`
Code bool `json:"code,omitempty"`
}
type RichTextSectionTextElement struct {
Type RichTextSectionElementType `json:"type"`
Text string `json:"text"`
Style *RichTextSectionTextStyle `json:"style,omitempty"`
}
func (r RichTextSectionTextElement) RichTextSectionElementType() RichTextSectionElementType {
return r.Type
}
func NewRichTextSectionTextElement(text string, style *RichTextSectionTextStyle) *RichTextSectionTextElement {
return &RichTextSectionTextElement{
Type: RTSEText,
Text: text,
Style: style,
}
}
type RichTextSectionChannelElement struct {
Type RichTextSectionElementType `json:"type"`
ChannelID string `json:"channel_id"`
Style *RichTextSectionTextStyle `json:"style,omitempty"`
}
func (r RichTextSectionChannelElement) RichTextSectionElementType() RichTextSectionElementType {
return r.Type
}
func NewRichTextSectionChannelElement(channelID string, style *RichTextSectionTextStyle) *RichTextSectionChannelElement {
return &RichTextSectionChannelElement{
Type: RTSEText,
ChannelID: channelID,
Style: style,
}
}
type RichTextSectionUserElement struct {
Type RichTextSectionElementType `json:"type"`
UserID string `json:"user_id"`
Style *RichTextSectionTextStyle `json:"style,omitempty"`
}
func (r RichTextSectionUserElement) RichTextSectionElementType() RichTextSectionElementType {
return r.Type
}
func NewRichTextSectionUserElement(userID string, style *RichTextSectionTextStyle) *RichTextSectionUserElement {
return &RichTextSectionUserElement{
Type: RTSEUser,
UserID: userID,
Style: style,
}
}
type RichTextSectionEmojiElement struct {
Type RichTextSectionElementType `json:"type"`
Name string `json:"name"`
SkinTone int `json:"skin_tone"`
Style *RichTextSectionTextStyle `json:"style,omitempty"`
}
func (r RichTextSectionEmojiElement) RichTextSectionElementType() RichTextSectionElementType {
return r.Type
}
func NewRichTextSectionEmojiElement(name string, skinTone int, style *RichTextSectionTextStyle) *RichTextSectionEmojiElement {
return &RichTextSectionEmojiElement{
Type: RTSEEmoji,
Name: name,
SkinTone: skinTone,
Style: style,
}
}
type RichTextSectionLinkElement struct {
Type RichTextSectionElementType `json:"type"`
URL string `json:"url"`
Text string `json:"text"`
Style *RichTextSectionTextStyle `json:"style,omitempty"`
}
func (r RichTextSectionLinkElement) RichTextSectionElementType() RichTextSectionElementType {
return r.Type
}
func NewRichTextSectionLinkElement(url, text string, style *RichTextSectionTextStyle) *RichTextSectionLinkElement {
return &RichTextSectionLinkElement{
Type: RTSELink,
URL: url,
Text: text,
Style: style,
}
}
type RichTextSectionTeamElement struct {
Type RichTextSectionElementType `json:"type"`
TeamID string `json:"team_id"`
Style *RichTextSectionTextStyle `json:"style,omitempty"`
}
func (r RichTextSectionTeamElement) RichTextSectionElementType() RichTextSectionElementType {
return r.Type
}
func NewRichTextSectionTeamElement(teamID string, style *RichTextSectionTextStyle) *RichTextSectionTeamElement {
return &RichTextSectionTeamElement{
Type: RTSETeam,
TeamID: teamID,
Style: style,
}
}
type RichTextSectionUserGroupElement struct {
Type RichTextSectionElementType `json:"type"`
UsergroupID string `json:"usergroup_id"`
}
func (r RichTextSectionUserGroupElement) RichTextSectionElementType() RichTextSectionElementType {
return r.Type
}
func NewRichTextSectionUserGroupElement(usergroupID string) *RichTextSectionUserGroupElement {
return &RichTextSectionUserGroupElement{
Type: RTSEUserGroup,
UsergroupID: usergroupID,
}
}
type RichTextSectionDateElement struct {
Type RichTextSectionElementType `json:"type"`
Timestamp JSONTime `json:"timestamp"`
}
func (r RichTextSectionDateElement) RichTextSectionElementType() RichTextSectionElementType {
return r.Type
}
func NewRichTextSectionDateElement(timestamp int64) *RichTextSectionDateElement {
return &RichTextSectionDateElement{
Type: RTSEDate,
Timestamp: JSONTime(timestamp),
}
}
type RichTextSectionBroadcastElement struct {
Type RichTextSectionElementType `json:"type"`
Range string `json:"range"`
}
func (r RichTextSectionBroadcastElement) RichTextSectionElementType() RichTextSectionElementType {
return r.Type
}
func NewRichTextSectionBroadcastElement(rangeStr string) *RichTextSectionBroadcastElement {
return &RichTextSectionBroadcastElement{
Type: RTSEBroadcast,
Range: rangeStr,
}
}
type RichTextSectionColorElement struct {
Type RichTextSectionElementType `json:"type"`
Value string `json:"value"`
}
func (r RichTextSectionColorElement) RichTextSectionElementType() RichTextSectionElementType {
return r.Type
}
func NewRichTextSectionColorElement(value string) *RichTextSectionColorElement {
return &RichTextSectionColorElement{
Type: RTSEColor,
Value: value,
}
}
type RichTextSectionUnknownElement struct {
Type RichTextSectionElementType `json:"type"`
Raw string
}
func (r RichTextSectionUnknownElement) RichTextSectionElementType() RichTextSectionElementType {
return r.Type
}
// RichTextQuote represents rich_text_quote element type.
type RichTextQuote RichTextSection
// RichTextElementType returns the type of the Element
func (s *RichTextQuote) RichTextElementType() RichTextElementType {
return s.Type
}
func (s *RichTextQuote) UnmarshalJSON(b []byte) error {
// reusing the RichTextSection struct, as it's the same as RichTextQuote.
var rts RichTextSection
if err := json.Unmarshal(b, &rts); err != nil {
return err
}
*s = RichTextQuote(rts)
s.Type = RTEQuote
return nil
}
// RichTextPreformatted represents rich_text_quote element type.
type RichTextPreformatted struct {
RichTextSection
Border int `json:"border"`
}
// RichTextElementType returns the type of the Element
func (s *RichTextPreformatted) RichTextElementType() RichTextElementType {
return s.Type
}
func (s *RichTextPreformatted) UnmarshalJSON(b []byte) error {
var rts RichTextSection
if err := json.Unmarshal(b, &rts); err != nil {
return err
}
// we define standalone fields because we need to unmarshal the border
// field. We can not directly unmarshal the data into
// RichTextPreformatted because it will cause an infinite loop. We also
// can not define a struct with embedded RichTextSection and Border fields
// because the json package will not unmarshal the data into the
// standalone fields, once it sees UnmarshalJSON method on the embedded
// struct. The drawback is that we have to process the data twice, and
// have to define a standalone struct with the same set of fields as the
// original struct, which may become a maintenance burden (i.e. update the
// fields in two places, should it ever change).
var standalone struct {
Border int `json:"border"`
}
if err := json.Unmarshal(b, &standalone); err != nil {
return err
}
*s = RichTextPreformatted{
RichTextSection: rts,
Border: standalone.Border,
}
s.Type = RTEPreformatted
return nil
}