Skip to content

Commit

Permalink
Support message attachments legacy fields
Browse files Browse the repository at this point in the history
  • Loading branch information
twouters committed Oct 9, 2023
1 parent 4530ff3 commit ec3b339
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path"
"strconv"
"strings"
"time"

"github.com/slack-go/slack"
"github.com/yuin/goldmark"
Expand Down Expand Up @@ -266,13 +267,76 @@ func (portal *Portal) SlackBlocksToMatrix(blocks slack.Blocks, attachments []sla

htmlText.WriteString(portal.blocksToHtml(blocks, false, userTeam))

if len(attachments) > 0 && htmlText.String() != "" {
htmlText.WriteString("<br>")
}

for _, attachment := range attachments {
if attachment.IsMsgUnfurl {
for _, message_block := range attachment.MessageBlocks {
renderedAttachment := portal.blocksToHtml(message_block.Message.Blocks, true, userTeam)
htmlText.WriteString(fmt.Sprintf("<blockquote><b>%s</b><br>%s<a href=\"%s\"><i>%s</i></a><br></blockquote>",
attachment.AuthorName, renderedAttachment, attachment.FromURL, attachment.Footer))
}
} else {
if len(attachment.Pretext) > 0 {
htmlText.WriteString(fmt.Sprintf("%s<br>", portal.mrkdwnToMatrixHtml(attachment.Pretext)))
}
htmlText.WriteString("<blockquote>")
if len(attachment.AuthorName) > 0 {
if len(attachment.AuthorLink) > 0 {
htmlText.WriteString(fmt.Sprintf("<b><a href=\"%s\">%s</a></b><br>",
attachment.AuthorLink, attachment.AuthorName))
} else {
htmlText.WriteString(fmt.Sprintf("<b>%s</b><br>", attachment.AuthorName))
}
}
if len(attachment.Title) > 0 {
if len(attachment.TitleLink) > 0 {
htmlText.WriteString(fmt.Sprintf("<strong><a href=\"%s\">%s</a></strong><br>",
attachment.TitleLink, portal.mrkdwnToMatrixHtml(attachment.Title)))
} else {
htmlText.WriteString(fmt.Sprintf("<strong>%s</strong><br>", portal.mrkdwnToMatrixHtml(attachment.Title)))
}
}
if len(attachment.Text) > 0 {
htmlText.WriteString(fmt.Sprintf("%s<br>", portal.mrkdwnToMatrixHtml(attachment.Text)))
} else if len(attachment.Fallback) > 0 {
htmlText.WriteString(fmt.Sprintf("%s<br>", portal.mrkdwnToMatrixHtml(attachment.Fallback)))
}
if len(attachment.Fields) > 0 {
htmlText.WriteString("<table>")
var short = false
for _, field := range attachment.Fields {
if !short {
htmlText.WriteString("<tr>")
}
htmlText.WriteString(fmt.Sprintf("<td><strong>%s</strong><br>%s</td>",
field.Title, portal.mrkdwnToMatrixHtml(field.Value)))
short = !short && field.Short
if !short {
htmlText.WriteString("</tr>")
}
}
htmlText.WriteString("</table>")
}
if len(attachment.Footer) > 0 {
htmlText.WriteString(fmt.Sprintf("<sup>%s", portal.mrkdwnToMatrixHtml(attachment.Footer)))
}
if len(attachment.Ts) > 0 {
if len(attachment.Footer) > 0 {
htmlText.WriteString(" | ")
} else {
htmlText.WriteString("<sup>")
}
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("</sup>")
}
htmlText.WriteString("</blockquote>")
}
}

Expand Down

0 comments on commit ec3b339

Please sign in to comment.