Skip to content

Commit

Permalink
fix: Add required format field to rich text date blocks (#1317)
Browse files Browse the repository at this point in the history
As per [block kit
docs](https://api.slack.com/reference/block-kit/blocks#date-element-type),
the date element requires a format string to be included. Currently,
submitting a block kit with this element results in a slack API error.

Also added the two optional fields `url` and `fallback` for posterity.

##### PR preparation
Run `make pr-prep` from the root of the repository to run formatting,
linting and tests.

##### Should this be an issue instead
- [ ] is it a convenience method? (no new functionality, streamlines
some use case)
- [ ] exposes a previously private type, const, method, etc.
- [ ] is it application specific (caching, retry logic, rate limiting,
etc)
- [ ] is it performance related.

##### API changes

Since API changes have to be maintained they undergo a more detailed
review and are more likely to require changes.

- no tests, if you're adding to the API include at least a single test
of the happy case.
- If you can accomplish your goal without changing the API, then do so.
- dependency changes. updates are okay. adding/removing need
justification.

###### Examples of API changes that do not meet guidelines:
- in library cache for users. caches are use case specific.
- Convenience methods for Sending Messages, update, post, ephemeral,
etc. consider opening an issue instead.
  • Loading branch information
luke-josh authored Sep 17, 2024
1 parent 6c4585b commit cd4e26e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 7 additions & 1 deletion block_rich_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,16 +414,22 @@ func NewRichTextSectionUserGroupElement(usergroupID string) *RichTextSectionUser
type RichTextSectionDateElement struct {
Type RichTextSectionElementType `json:"type"`
Timestamp JSONTime `json:"timestamp"`
Format string `json:"format"`
URL *string `json:"url,omitempty"`
Fallback *string `json:"fallback,omitempty"`
}

func (r RichTextSectionDateElement) RichTextSectionElementType() RichTextSectionElementType {
return r.Type
}

func NewRichTextSectionDateElement(timestamp int64) *RichTextSectionDateElement {
func NewRichTextSectionDateElement(timestamp int64, format string, url *string, fallback *string) *RichTextSectionDateElement {
return &RichTextSectionDateElement{
Type: RTSEDate,
Timestamp: JSONTime(timestamp),
Format: format,
URL: url,
Fallback: fallback,
}
}

Expand Down
7 changes: 5 additions & 2 deletions block_rich_text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,14 @@ func TestRichTextSection_UnmarshalJSON(t *testing.T) {
err error
}{
{
[]byte(`{"elements":[{"type":"unknown","value":10},{"type":"text","text":"hi"},{"type":"date","timestamp":1636961629}]}`),
[]byte(`{"elements":[{"type":"unknown","value":10},{"type":"text","text":"hi"},{"type":"date","timestamp":1636961629,"format":"{date_short_pretty}"},{"type":"date","timestamp":1636961629,"format":"{date_short_pretty}","url":"https://example.com","fallback":"default"}]}`),
RichTextSection{
Type: RTESection,
Elements: []RichTextSectionElement{
&RichTextSectionUnknownElement{Type: RTSEUnknown, Raw: `{"type":"unknown","value":10}`},
&RichTextSectionTextElement{Type: RTSEText, Text: "hi"},
&RichTextSectionDateElement{Type: RTSEDate, Timestamp: JSONTime(1636961629)},
&RichTextSectionDateElement{Type: RTSEDate, Timestamp: JSONTime(1636961629), Format: "{date_short_pretty}"},
&RichTextSectionDateElement{Type: RTSEDate, Timestamp: JSONTime(1636961629), Format: "{date_short_pretty}", URL: strp("https://example.com"), Fallback: strp("default")},
},
},
nil,
Expand Down Expand Up @@ -361,3 +362,5 @@ func TestRichTextQuote_Marshal(t *testing.T) {
}
})
}

func strp(in string) *string { return &in }

0 comments on commit cd4e26e

Please sign in to comment.