Skip to content

Commit

Permalink
Add support for message attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
hloeung committed Feb 16, 2024
1 parent a531f26 commit cfeafd5
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions bridge/mattermost/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,16 @@ func (m *Mattermost) handleWsActionPost(rmsg *model.WebSocketEvent) {
msg = strings.TrimLeft(msg, "*")
msg = strings.TrimRight(msg, "*")
msg = "\x01ACTION " + msg + " \x01"
} else if data.Type == "slack_attachment" {
attachmentMsg := parseSlackAttachmentMsg(data.Attachments())
if attachmentMsg == "" {
break
}
if msg == "" {
msg = attachmentMsg
} else {
msg = msg + attachmentMsg

Check failure on line 1042 in bridge/mattermost/mattermost.go

View workflow job for this annotation

GitHub Actions / golangci-lint

assignOp: replace `msg = msg + attachmentMsg` with `msg += attachmentMsg` (gocritic)
}
} else if data.Type == "custom_matterpoll" {
pollMsg := parseMatterpollToMsg(data.Attachments())
if pollMsg == "" {
Expand Down Expand Up @@ -1503,6 +1513,50 @@ func parseMatterpollToMsg(attachments []*model.SlackAttachment) string {
return msg
}

func parseSlackAttachmentMsg(attachments []*model.SlackAttachment) string {
msg := ""
for _, attachment := range attachments {
prefix := "| "
// XXX: Figure out how to use mIRC codes here without it being
// stripped further down. With that, also support hex color codes.
if attachment.Color == "danger" {
prefix = "\033[31m| \033[0m"
} else if attachment.Color == "good" {
prefix = "\033[32m| \033[0m"
}

if attachment.Text == "" {
continue
}
if attachment.AuthorName != "" {
msg += prefix + attachment.AuthorName
if attachment.AuthorLink != "" {
msg += " (" + attachment.AuthorLink + ")"
}
msg += "\n"
}
if attachment.Title != "" {
msg += prefix + attachment.Title
if attachment.TitleLink != "" {
msg += attachment.TitleLink
}
msg += "\n"
}
lines := strings.Split(attachment.Text, "\n")
for _, text := range lines {
msg += prefix + text + "\n"
}
if attachment.ImageURL != "" {
msg += prefix + attachment.ImageURL + "\n"
}
for _, field := range attachment.Fields {
msg += prefix + field.Title + ": " + field.Value.(string) + "\n"

Check failure on line 1553 in bridge/mattermost/mattermost.go

View workflow job for this annotation

GitHub Actions / golangci-lint

type assertion must be checked (forcetypeassert)
}
}

return msg
}

func (m *Mattermost) GetLastSentMsgs() []string {
data := make([]string, 0)

Expand Down

0 comments on commit cfeafd5

Please sign in to comment.