Thumbnail

rani/matterbridge.git

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

commit 85ecbdf7cbcf9f98b0f99e87e1a9a7881fb512ca Author: Duco van Amstel <duco.vanamstel@gmail.com> Date: Mon Nov 12 14:58:00 2018 +0000 Restore file comments coming from Slack (#583) diff --git a/bridge/slack/handlers.go b/bridge/slack/handlers.go index 61f572f..c4c6896 100644 --- a/bridge/slack/handlers.go +++ b/bridge/slack/handlers.go @@ -37 +36 @@ package bslack  import (   "fmt"   "html" - "regexp"   "time"     "github.com/42wim/matterbridge/bridge/config" @@ -22833 +22723 @@ func (b *Bslack) handleAttachments(ev *slack.MessageEvent, rmsg *config.Message)   }     // If we have files attached, download them (in memory) and put a pointer to it in msg.Extra. - for _, f := range ev.Files { - f := f - err := b.handleDownloadFile(rmsg, &f) - if err != nil { + for i := range ev.Files { + if err := b.handleDownloadFile(rmsg, &ev.Files[i]); err != nil {   b.Log.Errorf("Could not download incoming file: %#v", err)   }   }  }   -var commentRE = regexp.MustCompile(`.*?commented: (.*)`) -  func (b *Bslack) handleTypingEvent(ev *slack.UserTypingEvent) (*config.Message, error) { - var err error - // use our own func because rtm.GetChannelInfo doesn't work for private channels   channelInfo, err := b.getChannelByID(ev.Channel)   if err != nil {   return nil, err   } - - rmsg := config.Message{ + return &config.Message{   Channel: channelInfo.Name,   Account: b.Account,   Event: config.EVENT_USER_TYPING, - } - - return &rmsg, nil - + }, nil  }    // handleDownloadFile handles file download @@ -27511 +26411 @@ func (b *Bslack) handleDownloadFile(rmsg *config.Message, file *slack.File) erro   return fmt.Errorf("download %s failed %#v", file.URLPrivateDownload, err)   }   - // Add the downloaded data to the message. - var comment string - if results := commentRE.FindAllStringSubmatch(rmsg.Text, -1); len(results) > 0 { - comment = results[0][1] - } + // If a comment is attached to the file(s) it is in the 'Text' field of the Slack messge event + // and should be added as comment to only one of the files. We reset the 'Text' field to ensure + // that the comment is not duplicated. + comment := rmsg.Text + rmsg.Text = ""   helper.HandleDownloadData(b.Log, rmsg, file.Name, comment, file.URLPrivateDownload, data, b.General)   return nil  } diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index 038e0d9..ecf5828 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -2077 +2077 @@ func (b *Bslack) Send(msg config.Message) (string, error) {    // sendWebhook uses the configured WebhookURL to send the message  func (b *Bslack) sendWebhook(msg config.Message) (string, error) { - // skip events + // Skip events.   if msg.Event != "" {   return "", nil   } @@ -2177 +2177 @@ func (b *Bslack) sendWebhook(msg config.Message) (string, error) {   }     if msg.Extra != nil { - // this sends a message only if we received a config.EVENT_FILE_FAILURE_SIZE + // This sends a message only if we received a config.EVENT_FILE_FAILURE_SIZE.   for _, rmsg := range helper.HandleExtra(&msg, b.General) {   rmsg := rmsg // scopelint   iconURL := config.GetIconURL(&rmsg, b.GetString(iconURLConfig)) @@ -23216 +23220 @@ func (b *Bslack) sendWebhook(msg config.Message) (string, error) {   }   }   - // webhook doesn't support file uploads, so we add the url manually + // Webhook doesn't support file uploads, so we add the URL manually.   for _, f := range msg.Extra["file"] { - fi := f.(config.FileInfo) + fi, ok := f.(config.FileInfo) + if !ok { + b.Log.Errorf("Received a file with unexpected content: %#v", f) + continue + }   if fi.URL != "" {   msg.Text += " " + fi.URL   }   }   }   - // if we have native slack_attachments add them + // If we have native slack_attachments add them.   var attachs []slack.Attachment   for _, attach := range msg.Extra[sSlackAttachment] {   attachs = append(attachs, attach.([]slack.Attachment)...) @@ -2589 +2628 @@ func (b *Bslack) sendWebhook(msg config.Message) (string, error) {   if msg.Avatar != "" {   matterMessage.IconURL = msg.Avatar   } - err := b.mh.Send(matterMessage) - if err != nil { - b.Log.Error(err) + if err := b.mh.Send(matterMessage); err != nil { + b.Log.Errorf("Failed to send message via webhook: %#v", err)   return "", err   }   return "", nil @@ -3737 +37611 @@ func (b *Bslack) postMessage(msg *config.Message, messageParameters *slack.PostM  // uploadFile handles native upload of files  func (b *Bslack) uploadFile(msg *config.Message, channelID string) {   for _, f := range msg.Extra["file"] { - fi := f.(config.FileInfo) + fi, ok := f.(config.FileInfo) + if !ok { + b.Log.Errorf("Received a file with unexpected content: %#v", f) + continue + }   if msg.Text == fi.Comment {   msg.Text = ""   }