From cd4e26e5ec35543cf2e17dffc2fc447aa88e6468 Mon Sep 17 00:00:00 2001 From: Luke Josh <92695731+luke-josh@users.noreply.github.com> Date: Wed, 18 Sep 2024 05:57:39 +1000 Subject: [PATCH] fix: Add required `format` field to rich text date blocks (#1317) 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. --- block_rich_text.go | 8 +++++++- block_rich_text_test.go | 7 +++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/block_rich_text.go b/block_rich_text.go index b6a4b4ce4..611763ad4 100644 --- a/block_rich_text.go +++ b/block_rich_text.go @@ -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, } } diff --git a/block_rich_text_test.go b/block_rich_text_test.go index a9f04b7de..dec73ad9f 100644 --- a/block_rich_text_test.go +++ b/block_rich_text_test.go @@ -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, @@ -361,3 +362,5 @@ func TestRichTextQuote_Marshal(t *testing.T) { } }) } + +func strp(in string) *string { return &in }