Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 000b69d9e4c33e4c6f75b93e5a3c94ced054f6c8 Author: Andy <flashgame73@gmail.com> Date: Tue May 10 00:56:19 2022 +0000 Improve Slack attachments formatting (slack) (#1807) * Improve Slack attachments formatting (slack) * Add TitleLink * Add Footer * Fix linter issues diff --git a/bridge/slack/handlers.go b/bridge/slack/handlers.go index c469b9a..225931d 100644 --- a/bridge/slack/handlers.go +++ b/bridge/slack/handlers.go @@ -2826 +28213 @@ func (b *Bslack) handleStatusEvent(ev *slack.MessageEvent, rmsg *config.Message)   return false  }   +func getMessageTitle(attach *slack.Attachment) string { + if attach.TitleLink != "" { + return fmt.Sprintf("[%s](%s)\n", attach.Title, attach.TitleLink) + } + return attach.Title +} +  func (b *Bslack) handleAttachments(ev *slack.MessageEvent, rmsg *config.Message) {   // File comments are set by the system (because there is no username given).   if ev.SubType == sFileComment { @@ -29012 +29715 @@ func (b *Bslack) handleAttachments(ev *slack.MessageEvent, rmsg *config.Message)     // See if we have some text in the attachments.   if rmsg.Text == "" { - for _, attach := range ev.Attachments { + for i, attach := range ev.Attachments {   if attach.Text != "" {   if attach.Title != "" { - rmsg.Text = attach.Title + "\n" + rmsg.Text = getMessageTitle(&ev.Attachments[i])   }   rmsg.Text += attach.Text + if attach.Footer != "" { + rmsg.Text += "\n\n" + attach.Footer + }   } else {   rmsg.Text = attach.Fallback   } diff --git a/bridge/slack/helpers.go b/bridge/slack/helpers.go index bdacd7e..e46e272 100644 --- a/bridge/slack/helpers.go +++ b/bridge/slack/helpers.go @@ -1277 +1277 @@ var (   mentionRE = regexp.MustCompile(`<@([a-zA-Z0-9]+)>`)   channelRE = regexp.MustCompile(`<#[a-zA-Z0-9]+\|(.+?)>`)   variableRE = regexp.MustCompile(`<!((?:subteam\^)?[a-zA-Z0-9]+)(?:\|@?(.+?))?>`) - urlRE = regexp.MustCompile(`<(.*?)(\|.*?)?>`) + urlRE = regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`)   codeFenceRE = regexp.MustCompile(`(?m)^` + "```" + `\w+$`)   topicOrPurposeRE = regexp.MustCompile(`(?s)(@.+) (cleared|set)(?: the)? channel (topic|purpose)(?:: (.*))?`)  ) @@ -18114 +1817 @@ func (b *Bslack) replaceVariable(text string) string {    // @see https://api.slack.com/docs/message-formatting#linking_to_urls  func (b *Bslack) replaceURL(text string) string { - for _, r := range urlRE.FindAllStringSubmatch(text, -1) { - if len(strings.TrimSpace(r[2])) == 1 { // A display text separator was found, but the text was blank - text = strings.Replace(text, r[0], "", 1) - } else { - text = strings.Replace(text, r[0], r[1], 1) - } - } - return text + return urlRE.ReplaceAllString(text, "[${2}](${1})")  }    func (b *Bslack) replaceb0rkedMarkDown(text string) string {