| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | ) |
| 6 | |
| 7 | // RichTextBlock defines a new block of type rich_text. |
| 8 | // More Information: https://api.slack.com/changelog/2019-09-what-they-see-is-what-you-get-and-more-and-less |
| 9 | type RichTextBlock struct { |
| 10 | Type MessageBlockType `json:"type"` |
| 11 | BlockID string `json:"block_id,omitempty"` |
| 12 | Elements []RichTextElement `json:"elements"` |
| 13 | } |
| 14 | |
| 15 | func (b RichTextBlock) BlockType() MessageBlockType { |
| 16 | return b.Type |
| 17 | } |
| 18 | |
| 19 | func (e *RichTextBlock) UnmarshalJSON(b []byte) error { |
| 20 | var raw struct { |
| 21 | Type MessageBlockType `json:"type"` |
| 22 | BlockID string `json:"block_id"` |
| 23 | RawElements []json.RawMessage `json:"elements"` |
| 24 | } |
| 25 | if string(b) == "{}" { |
| 26 | return nil |
| 27 | } |
| 28 | if err := json.Unmarshal(b, &raw); err != nil { |
| 29 | return err |
| 30 | } |
| 31 | elems := make([]RichTextElement, 0, len(raw.RawElements)) |
| 32 | for _, r := range raw.RawElements { |
| 33 | var s struct { |
| 34 | Type RichTextElementType `json:"type"` |
| 35 | } |
| 36 | if err := json.Unmarshal(r, &s); err != nil { |
| 37 | return err |
| 38 | } |
| 39 | var elem RichTextElement |
| 40 | switch s.Type { |
| 41 | case RTESection: |
| 42 | elem = &RichTextSection{} |
| 43 | default: |
| 44 | elems = append(elems, &RichTextUnknown{ |
| 45 | Type: s.Type, |
| 46 | Raw: string(r), |
| 47 | }) |
| 48 | continue |
| 49 | } |
| 50 | if err := json.Unmarshal(r, &elem); err != nil { |
| 51 | return err |
| 52 | } |
| 53 | elems = append(elems, elem) |
| 54 | } |
| 55 | *e = RichTextBlock{ |
| 56 | Type: raw.Type, |
| 57 | BlockID: raw.BlockID, |
| 58 | Elements: elems, |
| 59 | } |
| 60 | return nil |
| 61 | } |
| 62 | |
| 63 | // NewRichTextBlock returns a new instance of RichText Block. |
| 64 | func NewRichTextBlock(blockID string, elements ...RichTextElement) *RichTextBlock { |
| 65 | return &RichTextBlock{ |
| 66 | Type: MBTRichText, |
| 67 | BlockID: blockID, |
| 68 | Elements: elements, |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | type RichTextElementType string |
| 73 | |
| 74 | type RichTextElement interface { |
| 75 | RichTextElementType() RichTextElementType |
| 76 | } |
| 77 | |
| 78 | const ( |
| 79 | RTEList RichTextElementType = "rich_text_list" |
| 80 | RTEPreformatted RichTextElementType = "rich_text_preformatted" |
| 81 | RTEQuote RichTextElementType = "rich_text_quote" |
| 82 | RTESection RichTextElementType = "rich_text_section" |
| 83 | RTEUnknown RichTextElementType = "rich_text_unknown" |
| 84 | ) |
| 85 | |
| 86 | type RichTextUnknown struct { |
| 87 | Type RichTextElementType |
| 88 | Raw string |
| 89 | } |
| 90 | |
| 91 | func (u RichTextUnknown) RichTextElementType() RichTextElementType { |
| 92 | return u.Type |
| 93 | } |
| 94 | |
| 95 | type RichTextSection struct { |
| 96 | Type RichTextElementType `json:"type"` |
| 97 | Elements []RichTextSectionElement `json:"elements"` |
| 98 | } |
| 99 | |
| 100 | // ElementType returns the type of the Element |
| 101 | func (s RichTextSection) RichTextElementType() RichTextElementType { |
| 102 | return s.Type |
| 103 | } |
| 104 | |
| 105 | func (e *RichTextSection) UnmarshalJSON(b []byte) error { |
| 106 | var raw struct { |
| 107 | RawElements []json.RawMessage `json:"elements"` |
| 108 | } |
| 109 | if string(b) == "{}" { |
| 110 | return nil |
| 111 | } |
| 112 | if err := json.Unmarshal(b, &raw); err != nil { |
| 113 | return err |
| 114 | } |
| 115 | elems := make([]RichTextSectionElement, 0, len(raw.RawElements)) |
| 116 | for _, r := range raw.RawElements { |
| 117 | var s struct { |
| 118 | Type RichTextSectionElementType `json:"type"` |
| 119 | } |
| 120 | if err := json.Unmarshal(r, &s); err != nil { |
| 121 | return err |
| 122 | } |
| 123 | var elem RichTextSectionElement |
| 124 | switch s.Type { |
| 125 | case RTSEText: |
| 126 | elem = &RichTextSectionTextElement{} |
| 127 | case RTSEChannel: |
| 128 | elem = &RichTextSectionChannelElement{} |
| 129 | case RTSEUser: |
| 130 | elem = &RichTextSectionUserElement{} |
| 131 | case RTSEEmoji: |
| 132 | elem = &RichTextSectionEmojiElement{} |
| 133 | case RTSELink: |
| 134 | elem = &RichTextSectionLinkElement{} |
| 135 | case RTSETeam: |
| 136 | elem = &RichTextSectionTeamElement{} |
| 137 | case RTSEUserGroup: |
| 138 | elem = &RichTextSectionUserGroupElement{} |
| 139 | case RTSEDate: |
| 140 | elem = &RichTextSectionDateElement{} |
| 141 | case RTSEBroadcast: |
| 142 | elem = &RichTextSectionBroadcastElement{} |
| 143 | case RTSEColor: |
| 144 | elem = &RichTextSectionColorElement{} |
| 145 | default: |
| 146 | elems = append(elems, &RichTextSectionUnknownElement{ |
| 147 | Type: s.Type, |
| 148 | Raw: string(r), |
| 149 | }) |
| 150 | continue |
| 151 | } |
| 152 | if err := json.Unmarshal(r, elem); err != nil { |
| 153 | return err |
| 154 | } |
| 155 | elems = append(elems, elem) |
| 156 | } |
| 157 | *e = RichTextSection{ |
| 158 | Type: RTESection, |
| 159 | Elements: elems, |
| 160 | } |
| 161 | return nil |
| 162 | } |
| 163 | |
| 164 | // NewRichTextSectionBlockElement . |
| 165 | func NewRichTextSection(elements ...RichTextSectionElement) *RichTextSection { |
| 166 | return &RichTextSection{ |
| 167 | Type: RTESection, |
| 168 | Elements: elements, |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | type RichTextSectionElementType string |
| 173 | |
| 174 | const ( |
| 175 | RTSEBroadcast RichTextSectionElementType = "broadcast" |
| 176 | RTSEChannel RichTextSectionElementType = "channel" |
| 177 | RTSEColor RichTextSectionElementType = "color" |
| 178 | RTSEDate RichTextSectionElementType = "date" |
| 179 | RTSEEmoji RichTextSectionElementType = "emoji" |
| 180 | RTSELink RichTextSectionElementType = "link" |
| 181 | RTSETeam RichTextSectionElementType = "team" |
| 182 | RTSEText RichTextSectionElementType = "text" |
| 183 | RTSEUser RichTextSectionElementType = "user" |
| 184 | RTSEUserGroup RichTextSectionElementType = "usergroup" |
| 185 | |
| 186 | RTSEUnknown RichTextSectionElementType = "unknown" |
| 187 | ) |
| 188 | |
| 189 | type RichTextSectionElement interface { |
| 190 | RichTextSectionElementType() RichTextSectionElementType |
| 191 | } |
| 192 | |
| 193 | type RichTextSectionTextStyle struct { |
| 194 | Bold bool `json:"bold,omitempty"` |
| 195 | Italic bool `json:"italic,omitempty"` |
| 196 | Strike bool `json:"strike,omitempty"` |
| 197 | Code bool `json:"code,omitempty"` |
| 198 | } |
| 199 | |
| 200 | type RichTextSectionTextElement struct { |
| 201 | Type RichTextSectionElementType `json:"type"` |
| 202 | Text string `json:"text"` |
| 203 | Style *RichTextSectionTextStyle `json:"style,omitempty"` |
| 204 | } |
| 205 | |
| 206 | func (r RichTextSectionTextElement) RichTextSectionElementType() RichTextSectionElementType { |
| 207 | return r.Type |
| 208 | } |
| 209 | |
| 210 | func NewRichTextSectionTextElement(text string, style *RichTextSectionTextStyle) *RichTextSectionTextElement { |
| 211 | return &RichTextSectionTextElement{ |
| 212 | Type: RTSEText, |
| 213 | Text: text, |
| 214 | Style: style, |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | type RichTextSectionChannelElement struct { |
| 219 | Type RichTextSectionElementType `json:"type"` |
| 220 | ChannelID string `json:"channel_id"` |
| 221 | Style *RichTextSectionTextStyle `json:"style,omitempty"` |
| 222 | } |
| 223 | |
| 224 | func (r RichTextSectionChannelElement) RichTextSectionElementType() RichTextSectionElementType { |
| 225 | return r.Type |
| 226 | } |
| 227 | |
| 228 | func NewRichTextSectionChannelElement(channelID string, style *RichTextSectionTextStyle) *RichTextSectionChannelElement { |
| 229 | return &RichTextSectionChannelElement{ |
| 230 | Type: RTSEText, |
| 231 | ChannelID: channelID, |
| 232 | Style: style, |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | type RichTextSectionUserElement struct { |
| 237 | Type RichTextSectionElementType `json:"type"` |
| 238 | UserID string `json:"user_id"` |
| 239 | Style *RichTextSectionTextStyle `json:"style,omitempty"` |
| 240 | } |
| 241 | |
| 242 | func (r RichTextSectionUserElement) RichTextSectionElementType() RichTextSectionElementType { |
| 243 | return r.Type |
| 244 | } |
| 245 | |
| 246 | func NewRichTextSectionUserElement(userID string, style *RichTextSectionTextStyle) *RichTextSectionUserElement { |
| 247 | return &RichTextSectionUserElement{ |
| 248 | Type: RTSEUser, |
| 249 | UserID: userID, |
| 250 | Style: style, |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | type RichTextSectionEmojiElement struct { |
| 255 | Type RichTextSectionElementType `json:"type"` |
| 256 | Name string `json:"name"` |
| 257 | SkinTone int `json:"skin_tone"` |
| 258 | Style *RichTextSectionTextStyle `json:"style,omitempty"` |
| 259 | } |
| 260 | |
| 261 | func (r RichTextSectionEmojiElement) RichTextSectionElementType() RichTextSectionElementType { |
| 262 | return r.Type |
| 263 | } |
| 264 | |
| 265 | func NewRichTextSectionEmojiElement(name string, skinTone int, style *RichTextSectionTextStyle) *RichTextSectionEmojiElement { |
| 266 | return &RichTextSectionEmojiElement{ |
| 267 | Type: RTSEEmoji, |
| 268 | Name: name, |
| 269 | SkinTone: skinTone, |
| 270 | Style: style, |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | type RichTextSectionLinkElement struct { |
| 275 | Type RichTextSectionElementType `json:"type"` |
| 276 | URL string `json:"url"` |
| 277 | Text string `json:"text"` |
| 278 | Style *RichTextSectionTextStyle `json:"style,omitempty"` |
| 279 | } |
| 280 | |
| 281 | func (r RichTextSectionLinkElement) RichTextSectionElementType() RichTextSectionElementType { |
| 282 | return r.Type |
| 283 | } |
| 284 | |
| 285 | func NewRichTextSectionLinkElement(url, text string, style *RichTextSectionTextStyle) *RichTextSectionLinkElement { |
| 286 | return &RichTextSectionLinkElement{ |
| 287 | Type: RTSELink, |
| 288 | URL: url, |
| 289 | Text: text, |
| 290 | Style: style, |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | type RichTextSectionTeamElement struct { |
| 295 | Type RichTextSectionElementType `json:"type"` |
| 296 | TeamID string `json:"team_id"` |
| 297 | Style *RichTextSectionTextStyle `json:"style.omitempty"` |
| 298 | } |
| 299 | |
| 300 | func (r RichTextSectionTeamElement) RichTextSectionElementType() RichTextSectionElementType { |
| 301 | return r.Type |
| 302 | } |
| 303 | |
| 304 | func NewRichTextSectionTeamElement(teamID string, style *RichTextSectionTextStyle) *RichTextSectionTeamElement { |
| 305 | return &RichTextSectionTeamElement{ |
| 306 | Type: RTSETeam, |
| 307 | TeamID: teamID, |
| 308 | Style: style, |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | type RichTextSectionUserGroupElement struct { |
| 313 | Type RichTextSectionElementType `json:"type"` |
| 314 | UsergroupID string `json:"usergroup_id"` |
| 315 | } |
| 316 | |
| 317 | func (r RichTextSectionUserGroupElement) RichTextSectionElementType() RichTextSectionElementType { |
| 318 | return r.Type |
| 319 | } |
| 320 | |
| 321 | func NewRichTextSectionUserGroupElement(usergroupID string) *RichTextSectionUserGroupElement { |
| 322 | return &RichTextSectionUserGroupElement{ |
| 323 | Type: RTSEUserGroup, |
| 324 | UsergroupID: usergroupID, |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | type RichTextSectionDateElement struct { |
| 329 | Type RichTextSectionElementType `json:"type"` |
| 330 | Timestamp string `json:"timestamp"` |
| 331 | } |
| 332 | |
| 333 | func (r RichTextSectionDateElement) RichTextSectionElementType() RichTextSectionElementType { |
| 334 | return r.Type |
| 335 | } |
| 336 | |
| 337 | func NewRichTextSectionDateElement(timestamp string) *RichTextSectionDateElement { |
| 338 | return &RichTextSectionDateElement{ |
| 339 | Type: RTSEDate, |
| 340 | Timestamp: timestamp, |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | type RichTextSectionBroadcastElement struct { |
| 345 | Type RichTextSectionElementType `json:"type"` |
| 346 | Range string `json:"range"` |
| 347 | } |
| 348 | |
| 349 | func (r RichTextSectionBroadcastElement) RichTextSectionElementType() RichTextSectionElementType { |
| 350 | return r.Type |
| 351 | } |
| 352 | |
| 353 | func NewRichTextSectionBroadcastElement(rangeStr string) *RichTextSectionBroadcastElement { |
| 354 | return &RichTextSectionBroadcastElement{ |
| 355 | Type: RTSEBroadcast, |
| 356 | Range: rangeStr, |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | type RichTextSectionColorElement struct { |
| 361 | Type RichTextSectionElementType `json:"type"` |
| 362 | Value string `json:"value"` |
| 363 | } |
| 364 | |
| 365 | func (r RichTextSectionColorElement) RichTextSectionElementType() RichTextSectionElementType { |
| 366 | return r.Type |
| 367 | } |
| 368 | |
| 369 | func NewRichTextSectionColorElement(value string) *RichTextSectionColorElement { |
| 370 | return &RichTextSectionColorElement{ |
| 371 | Type: RTSEColor, |
| 372 | Value: value, |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | type RichTextSectionUnknownElement struct { |
| 377 | Type RichTextSectionElementType `json:"type"` |
| 378 | Raw string |
| 379 | } |
| 380 | |
| 381 | func (r RichTextSectionUnknownElement) RichTextSectionElementType() RichTextSectionElementType { |
| 382 | return r.Type |
| 383 | } |