From ec3b339c5d069f6dfdfedc336746a62e0d401a1d Mon Sep 17 00:00:00 2001 From: Thomas Wouters Date: Mon, 9 Oct 2023 18:45:41 +0200 Subject: [PATCH] Support message attachments legacy fields --- blocks.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/blocks.go b/blocks.go index e32a445..c3aa2f9 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,65 @@ 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))) + } + htmlText.WriteString("
") + if len(attachment.AuthorName) > 0 { + if len(attachment.AuthorLink) > 0 { + htmlText.WriteString(fmt.Sprintf("%s
", + attachment.AuthorLink, attachment.AuthorName)) + } else { + htmlText.WriteString(fmt.Sprintf("%s
", attachment.AuthorName)) + } + } + if len(attachment.Title) > 0 { + if len(attachment.TitleLink) > 0 { + htmlText.WriteString(fmt.Sprintf("%s
", + attachment.TitleLink, portal.mrkdwnToMatrixHtml(attachment.Title))) + } else { + htmlText.WriteString(fmt.Sprintf("%s
", portal.mrkdwnToMatrixHtml(attachment.Title))) + } + } + if len(attachment.Text) > 0 { + htmlText.WriteString(fmt.Sprintf("%s
", portal.mrkdwnToMatrixHtml(attachment.Text))) + } else if len(attachment.Fallback) > 0 { + htmlText.WriteString(fmt.Sprintf("%s
", portal.mrkdwnToMatrixHtml(attachment.Fallback))) + } + if len(attachment.Fields) > 0 { + htmlText.WriteString("") + var short = false + for _, field := range attachment.Fields { + if !short { + htmlText.WriteString("") + } + htmlText.WriteString(fmt.Sprintf("", + field.Title, portal.mrkdwnToMatrixHtml(field.Value))) + short = !short && field.Short + if !short { + htmlText.WriteString("") + } + } + htmlText.WriteString("
%s
%s
") + } + if len(attachment.Footer) > 0 { + htmlText.WriteString(fmt.Sprintf("%s", portal.mrkdwnToMatrixHtml(attachment.Footer))) + } + if len(attachment.Ts) > 0 { + if len(attachment.Footer) > 0 { + htmlText.WriteString(" | ") + } else { + htmlText.WriteString("") + } + ts, _ := attachment.Ts.Int64() + t := time.Unix(ts, 0) + htmlText.WriteString(fmt.Sprintf("%s", t.Local().Format("Jan 02, 2006 15:04:05"))) + } + if len(attachment.Footer) > 0 || len(attachment.Ts) > 0 { + htmlText.WriteString("") + } + htmlText.WriteString("
") } }