| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | ) |
| 7 | |
| 8 | // Block Objects are also known as Composition Objects |
| 9 | // |
| 10 | // For more information: https://api.slack.com/reference/messaging/composition-objects |
| 11 | |
| 12 | // BlockObject defines an interface that all block object types should |
| 13 | // implement. |
| 14 | // @TODO: Is this interface needed? |
| 15 | |
| 16 | // blockObject object types |
| 17 | const ( |
| 18 | MarkdownType = "mrkdwn" |
| 19 | PlainTextType = "plain_text" |
| 20 | // The following objects don't actually have types and their corresponding |
| 21 | // const values are just for internal use |
| 22 | motConfirmation = "confirm" |
| 23 | motOption = "option" |
| 24 | motOptionGroup = "option_group" |
| 25 | ) |
| 26 | |
| 27 | type MessageObjectType string |
| 28 | |
| 29 | type blockObject interface { |
| 30 | validateType() MessageObjectType |
| 31 | } |
| 32 | |
| 33 | type BlockObjects struct { |
| 34 | TextObjects []*TextBlockObject |
| 35 | ConfirmationObjects []*ConfirmationBlockObject |
| 36 | OptionObjects []*OptionBlockObject |
| 37 | OptionGroupObjects []*OptionGroupBlockObject |
| 38 | } |
| 39 | |
| 40 | // UnmarshalJSON implements the Unmarshaller interface for BlockObjects, so that any JSON |
| 41 | // unmarshalling is delegated and proper type determination can be made before unmarshal |
| 42 | func (b *BlockObjects) UnmarshalJSON(data []byte) error { |
| 43 | var raw []json.RawMessage |
| 44 | err := json.Unmarshal(data, &raw) |
| 45 | if err != nil { |
| 46 | return err |
| 47 | } |
| 48 | |
| 49 | for _, r := range raw { |
| 50 | var obj map[string]interface{} |
| 51 | err := json.Unmarshal(r, &obj) |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | |
| 56 | blockObjectType := getBlockObjectType(obj) |
| 57 | |
| 58 | switch blockObjectType { |
| 59 | case PlainTextType, MarkdownType: |
| 60 | object, err := unmarshalBlockObject(r, &TextBlockObject{}) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | b.TextObjects = append(b.TextObjects, object.(*TextBlockObject)) |
| 65 | case motConfirmation: |
| 66 | object, err := unmarshalBlockObject(r, &ConfirmationBlockObject{}) |
| 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | b.ConfirmationObjects = append(b.ConfirmationObjects, object.(*ConfirmationBlockObject)) |
| 71 | case motOption: |
| 72 | object, err := unmarshalBlockObject(r, &OptionBlockObject{}) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | b.OptionObjects = append(b.OptionObjects, object.(*OptionBlockObject)) |
| 77 | case motOptionGroup: |
| 78 | object, err := unmarshalBlockObject(r, &OptionGroupBlockObject{}) |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | b.OptionGroupObjects = append(b.OptionGroupObjects, object.(*OptionGroupBlockObject)) |
| 83 | |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | // Ideally would have a better way to identify the block objects for |
| 91 | // type casting at time of unmarshalling, should be adapted if possible |
| 92 | // to accomplish in a more reliable manner. |
| 93 | func getBlockObjectType(obj map[string]interface{}) string { |
| 94 | if t, ok := obj["type"].(string); ok { |
| 95 | return t |
| 96 | } |
| 97 | if _, ok := obj["confirm"].(string); ok { |
| 98 | return "confirm" |
| 99 | } |
| 100 | if _, ok := obj["options"].(string); ok { |
| 101 | return "option_group" |
| 102 | } |
| 103 | if _, ok := obj["text"].(string); ok { |
| 104 | if _, ok := obj["value"].(string); ok { |
| 105 | return "option" |
| 106 | } |
| 107 | } |
| 108 | return "" |
| 109 | } |
| 110 | |
| 111 | func unmarshalBlockObject(r json.RawMessage, object blockObject) (blockObject, error) { |
| 112 | err := json.Unmarshal(r, object) |
| 113 | if err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | return object, nil |
| 117 | } |
| 118 | |
| 119 | // TextBlockObject defines a text element object to be used with blocks |
| 120 | // |
| 121 | // More Information: https://api.slack.com/reference/messaging/composition-objects#text |
| 122 | type TextBlockObject struct { |
| 123 | Type string `json:"type"` |
| 124 | Text string `json:"text"` |
| 125 | Emoji bool `json:"emoji,omitempty"` |
| 126 | Verbatim bool `json:"verbatim,omitempty"` |
| 127 | } |
| 128 | |
| 129 | // validateType enforces block objects for element and block parameters |
| 130 | func (s TextBlockObject) validateType() MessageObjectType { |
| 131 | return MessageObjectType(s.Type) |
| 132 | } |
| 133 | |
| 134 | // validateType enforces block objects for element and block parameters |
| 135 | func (s TextBlockObject) MixedElementType() MixedElementType { |
| 136 | return MixedElementText |
| 137 | } |
| 138 | |
| 139 | // Validate checks if TextBlockObject has valid values |
| 140 | func (s TextBlockObject) Validate() error { |
| 141 | if s.Type != "plain_text" && s.Type != "mrkdwn" { |
| 142 | return errors.New("type must be either of plain_text or mrkdwn") |
| 143 | } |
| 144 | |
| 145 | // https://github.com/slack-go/slack/issues/881 |
| 146 | if s.Type == "mrkdwn" && s.Emoji { |
| 147 | return errors.New("emoji cannot be true in mrkdown") |
| 148 | } |
| 149 | |
| 150 | return nil |
| 151 | } |
| 152 | |
| 153 | // NewTextBlockObject returns an instance of a new Text Block Object |
| 154 | func NewTextBlockObject(elementType, text string, emoji, verbatim bool) *TextBlockObject { |
| 155 | return &TextBlockObject{ |
| 156 | Type: elementType, |
| 157 | Text: text, |
| 158 | Emoji: emoji, |
| 159 | Verbatim: verbatim, |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // BlockType returns the type of the block |
| 164 | func (t TextBlockObject) BlockType() MessageBlockType { |
| 165 | if t.Type == "mrkdwn" { |
| 166 | return MarkdownType |
| 167 | } |
| 168 | return PlainTextType |
| 169 | } |
| 170 | |
| 171 | // ConfirmationBlockObject defines a dialog that provides a confirmation step to |
| 172 | // any interactive element. This dialog will ask the user to confirm their action by |
| 173 | // offering a confirm and deny buttons. |
| 174 | // |
| 175 | // More Information: https://api.slack.com/reference/messaging/composition-objects#confirm |
| 176 | type ConfirmationBlockObject struct { |
| 177 | Title *TextBlockObject `json:"title"` |
| 178 | Text *TextBlockObject `json:"text"` |
| 179 | Confirm *TextBlockObject `json:"confirm"` |
| 180 | Deny *TextBlockObject `json:"deny"` |
| 181 | Style Style `json:"style,omitempty"` |
| 182 | } |
| 183 | |
| 184 | // validateType enforces block objects for element and block parameters |
| 185 | func (s ConfirmationBlockObject) validateType() MessageObjectType { |
| 186 | return motConfirmation |
| 187 | } |
| 188 | |
| 189 | // WithStyle add styling to confirmation object |
| 190 | func (s *ConfirmationBlockObject) WithStyle(style Style) *ConfirmationBlockObject { |
| 191 | s.Style = style |
| 192 | return s |
| 193 | } |
| 194 | |
| 195 | // NewConfirmationBlockObject returns an instance of a new Confirmation Block Object |
| 196 | func NewConfirmationBlockObject(title, text, confirm, deny *TextBlockObject) *ConfirmationBlockObject { |
| 197 | return &ConfirmationBlockObject{ |
| 198 | Title: title, |
| 199 | Text: text, |
| 200 | Confirm: confirm, |
| 201 | Deny: deny, |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // OptionBlockObject represents a single selectable item in a select menu |
| 206 | // |
| 207 | // More Information: https://api.slack.com/reference/messaging/composition-objects#option |
| 208 | type OptionBlockObject struct { |
| 209 | Text *TextBlockObject `json:"text"` |
| 210 | Value string `json:"value"` |
| 211 | Description *TextBlockObject `json:"description,omitempty"` |
| 212 | URL string `json:"url,omitempty"` |
| 213 | } |
| 214 | |
| 215 | // NewOptionBlockObject returns an instance of a new Option Block Element |
| 216 | func NewOptionBlockObject(value string, text, description *TextBlockObject) *OptionBlockObject { |
| 217 | return &OptionBlockObject{ |
| 218 | Text: text, |
| 219 | Value: value, |
| 220 | Description: description, |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // validateType enforces block objects for element and block parameters |
| 225 | func (s OptionBlockObject) validateType() MessageObjectType { |
| 226 | return motOption |
| 227 | } |
| 228 | |
| 229 | // OptionGroupBlockObject Provides a way to group options in a select menu. |
| 230 | // |
| 231 | // More Information: https://api.slack.com/reference/messaging/composition-objects#option-group |
| 232 | type OptionGroupBlockObject struct { |
| 233 | Label *TextBlockObject `json:"label,omitempty"` |
| 234 | Options []*OptionBlockObject `json:"options"` |
| 235 | } |
| 236 | |
| 237 | // validateType enforces block objects for element and block parameters |
| 238 | func (s OptionGroupBlockObject) validateType() MessageObjectType { |
| 239 | return motOptionGroup |
| 240 | } |
| 241 | |
| 242 | // NewOptionGroupBlockElement returns an instance of a new option group block element |
| 243 | func NewOptionGroupBlockElement(label *TextBlockObject, options ...*OptionBlockObject) *OptionGroupBlockObject { |
| 244 | return &OptionGroupBlockObject{ |
| 245 | Label: label, |
| 246 | Options: options, |
| 247 | } |
| 248 | } |