forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_rich_text.go
383 lines (332 loc) · 10.2 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
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{}
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 RichTextSection struct {
Type RichTextElementType `json:"type"`
Elements []RichTextSectionElement `json:"elements"`
}
// ElementType 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 string `json:"timestamp"`
}
func (r RichTextSectionDateElement) RichTextSectionElementType() RichTextSectionElementType {
return r.Type
}
func NewRichTextSectionDateElement(timestamp string) *RichTextSectionDateElement {
return &RichTextSectionDateElement{
Type: RTSEDate,
Timestamp: 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
}