From 03070e8339dabd35cfc281a2bc229c9049c9aae1 Mon Sep 17 00:00:00 2001 From: Thomas Wouters Date: Mon, 9 Oct 2023 18:45:41 +0200 Subject: [PATCH 1/5] Support message attachments legacy fields --- blocks.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/blocks.go b/blocks.go index e32a445..8673e07 100644 --- a/blocks.go +++ b/blocks.go @@ -8,6 +8,7 @@ import ( "path" "strconv" "strings" + "time" "github.com/slack-go/slack" "github.com/yuin/goldmark" @@ -266,6 +267,10 @@ func (portal *Portal) SlackBlocksToMatrix(blocks slack.Blocks, attachments []sla htmlText.WriteString(portal.blocksToHtml(blocks, false, userTeam)) + if len(attachments) > 0 && htmlText.String() != "" { + htmlText.WriteString("
") + } + for _, attachment := range attachments { if attachment.IsMsgUnfurl { for _, message_block := range attachment.MessageBlocks { @@ -273,6 +278,61 @@ func (portal *Portal) SlackBlocksToMatrix(blocks slack.Blocks, attachments []sla htmlText.WriteString(fmt.Sprintf("
%s
%s%s
", attachment.AuthorName, renderedAttachment, attachment.FromURL, attachment.Footer)) } + } else { + if len(attachment.Pretext) > 0 { + htmlText.WriteString(fmt.Sprintf("%s
", portal.mrkdwnToMatrixHtml(attachment.Pretext))) + } + var attachParts []string + if len(attachment.AuthorName) > 0 { + if len(attachment.AuthorLink) > 0 { + attachParts = append(attachParts, fmt.Sprintf("%s", + attachment.AuthorLink, attachment.AuthorName)) + } else { + attachParts = append(attachParts, fmt.Sprintf("%s", attachment.AuthorName)) + } + } + if len(attachment.Title) > 0 { + if len(attachment.TitleLink) > 0 { + attachParts = append(attachParts, fmt.Sprintf("%s", + attachment.TitleLink, portal.mrkdwnToMatrixHtml(attachment.Title))) + } else { + attachParts = append(attachParts, fmt.Sprintf("%s", portal.mrkdwnToMatrixHtml(attachment.Title))) + } + } + if len(attachment.Text) > 0 { + attachParts = append(attachParts, portal.mrkdwnToMatrixHtml(attachment.Text)) + } else if len(attachment.Fallback) > 0 { + attachParts = append(attachParts, portal.mrkdwnToMatrixHtml(attachment.Fallback)) + } + if len(attachment.Fields) > 0 { + var fieldBody string + var short = false + for _, field := range attachment.Fields { + if !short { + fieldBody += "" + } + fieldBody += fmt.Sprintf("%s
%s", + field.Title, portal.mrkdwnToMatrixHtml(field.Value)) + short = !short && field.Short + if !short { + fieldBody += "" + } + } + attachParts = append(attachParts, fmt.Sprintf("%s
", fieldBody)) + } + var footerParts []string + if len(attachment.Footer) > 0 { + footerParts = append(footerParts, portal.mrkdwnToMatrixHtml(attachment.Footer)) + } + if len(attachment.Ts) > 0 { + ts, _ := attachment.Ts.Int64() + t := time.Unix(ts, 0) + footerParts = append(footerParts, t.Local().Format("Jan 02, 2006 15:04:05 MST")) + } + if len(footerParts) > 0 { + attachParts = append(attachParts, fmt.Sprintf("%s", strings.Join(footerParts, " | "))) + } + htmlText.WriteString(fmt.Sprintf("
%s
", strings.Join(attachParts, "
"))) } } From 0f5eedc2ba78696659c8819691b4921e5b472806 Mon Sep 17 00:00:00 2001 From: Thomas Wouters Date: Tue, 10 Oct 2023 15:50:19 +0200 Subject: [PATCH 2/5] Add support for section block fields --- blocks.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/blocks.go b/blocks.go index 8673e07..b90b752 100644 --- a/blocks.go +++ b/blocks.go @@ -175,12 +175,26 @@ func (portal *Portal) renderSlackBlock(block slack.Block, userTeam *database.Use case *slack.DividerBlock: return "
", false case *slack.SectionBlock: + var htmlParts []string if b.Text != nil { - return portal.renderSlackTextBlock(*b.Text), false - } else { - portal.log.Debugln("Unsupported Slack block: section block without a text object") - return "Slack message contains unsupported elements.", true + htmlParts = append(htmlParts, portal.renderSlackTextBlock(*b.Text)) + } + if len(b.Fields) > 0 { + var fieldTable strings.Builder + fieldTable.WriteString("") + for i, field := range b.Fields { + if i%2 == 0 { + fieldTable.WriteString("") + } + fieldTable.WriteString(fmt.Sprintf("", portal.mrkdwnToMatrixHtml(field.Text))) + if i%2 != 0 || i == len(b.Fields)-1 { + fieldTable.WriteString("") + } + } + fieldTable.WriteString("
%s
") + htmlParts = append(htmlParts, fieldTable.String()) } + return strings.Join(htmlParts, "
"), false case *slack.RichTextBlock: var htmlText strings.Builder for _, element := range b.Elements { From 48212435d0b271134394501be8080b5acee5faf1 Mon Sep 17 00:00:00 2001 From: Thomas Wouters Date: Tue, 10 Oct 2023 17:00:01 +0200 Subject: [PATCH 3/5] Add support for ContextBlocks --- blocks.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/blocks.go b/blocks.go index b90b752..c181aad 100644 --- a/blocks.go +++ b/blocks.go @@ -201,6 +201,19 @@ func (portal *Portal) renderSlackBlock(block slack.Block, userTeam *database.Use htmlText.WriteString(portal.renderSlackRichTextElement(len(b.Elements), element, userTeam)) } return format.UnwrapSingleParagraph(htmlText.String()), false + case *slack.ContextBlock: + var htmlText strings.Builder + var unsupported bool = false + for _, element := range b.ContextElements.Elements { + if mrkdwnElem, ok := element.(*slack.TextBlockObject); ok { + htmlText.WriteString(fmt.Sprintf("%s", portal.mrkdwnToMatrixHtml(mrkdwnElem.Text))) + } else { + portal.log.Debugfln("Unsupported Slack block element: %s", element.MixedElementType()) + htmlText.WriteString("Slack message contains unsupported elements.") + unsupported = true + } + } + return htmlText.String(), unsupported default: portal.log.Debugfln("Unsupported Slack block: %s", b.BlockType()) return "Slack message contains unsupported elements.", true From 67f3912abe7bb49cd7d856f3c5664a63a8b64e3a Mon Sep 17 00:00:00 2001 From: Thomas Wouters Date: Mon, 16 Oct 2023 14:21:12 +0200 Subject: [PATCH 4/5] Support Blocks in message attachments --- blocks.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/blocks.go b/blocks.go index c181aad..5726a93 100644 --- a/blocks.go +++ b/blocks.go @@ -305,6 +305,11 @@ func (portal *Portal) SlackBlocksToMatrix(blocks slack.Blocks, attachments []sla htmlText.WriteString(fmt.Sprintf("
%s
%s%s
", attachment.AuthorName, renderedAttachment, attachment.FromURL, attachment.Footer)) } + } else if len(attachment.Blocks.BlockSet) > 0 { + for _, message_block := range attachment.Blocks.BlockSet { + renderedAttachment, _ := portal.renderSlackBlock(message_block, userTeam) + htmlText.WriteString(fmt.Sprintf("
%s
", renderedAttachment)) + } } else { if len(attachment.Pretext) > 0 { htmlText.WriteString(fmt.Sprintf("%s
", portal.mrkdwnToMatrixHtml(attachment.Pretext))) From 24884c08df697e0ddd14b9ec6e42a8a35097deff Mon Sep 17 00:00:00 2001 From: Thomas Wouters Date: Mon, 16 Oct 2023 14:23:00 +0200 Subject: [PATCH 5/5] Reformat legacy fields to avoid empty linebreaks --- blocks.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/blocks.go b/blocks.go index 5726a93..e217fa9 100644 --- a/blocks.go +++ b/blocks.go @@ -312,7 +312,7 @@ func (portal *Portal) SlackBlocksToMatrix(blocks slack.Blocks, attachments []sla } } else { if len(attachment.Pretext) > 0 { - htmlText.WriteString(fmt.Sprintf("%s
", portal.mrkdwnToMatrixHtml(attachment.Pretext))) + htmlText.WriteString(fmt.Sprintf("

%s

", portal.mrkdwnToMatrixHtml(attachment.Pretext))) } var attachParts []string if len(attachment.AuthorName) > 0 { @@ -325,10 +325,10 @@ func (portal *Portal) SlackBlocksToMatrix(blocks slack.Blocks, attachments []sla } if len(attachment.Title) > 0 { if len(attachment.TitleLink) > 0 { - attachParts = append(attachParts, fmt.Sprintf("%s", + attachParts = append(attachParts, fmt.Sprintf("%s", attachment.TitleLink, portal.mrkdwnToMatrixHtml(attachment.Title))) } else { - attachParts = append(attachParts, fmt.Sprintf("%s", portal.mrkdwnToMatrixHtml(attachment.Title))) + attachParts = append(attachParts, fmt.Sprintf("%s", portal.mrkdwnToMatrixHtml(attachment.Title))) } } if len(attachment.Text) > 0 { @@ -336,6 +336,7 @@ func (portal *Portal) SlackBlocksToMatrix(blocks slack.Blocks, attachments []sla } else if len(attachment.Fallback) > 0 { attachParts = append(attachParts, portal.mrkdwnToMatrixHtml(attachment.Fallback)) } + htmlText.WriteString(fmt.Sprintf("
%s", strings.Join(attachParts, "
"))) if len(attachment.Fields) > 0 { var fieldBody string var short = false @@ -350,7 +351,9 @@ func (portal *Portal) SlackBlocksToMatrix(blocks slack.Blocks, attachments []sla fieldBody += "" } } - attachParts = append(attachParts, fmt.Sprintf("%s
", fieldBody)) + htmlText.WriteString(fmt.Sprintf("%s
", fieldBody)) + } else { + htmlText.WriteString("
") } var footerParts []string if len(attachment.Footer) > 0 { @@ -362,9 +365,9 @@ func (portal *Portal) SlackBlocksToMatrix(blocks slack.Blocks, attachments []sla footerParts = append(footerParts, t.Local().Format("Jan 02, 2006 15:04:05 MST")) } if len(footerParts) > 0 { - attachParts = append(attachParts, fmt.Sprintf("%s", strings.Join(footerParts, " | "))) + htmlText.WriteString(fmt.Sprintf("%s", strings.Join(footerParts, " | "))) } - htmlText.WriteString(fmt.Sprintf("
%s
", strings.Join(attachParts, "
"))) + htmlText.WriteString("
") } }