5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-22 20:40:27 +00:00

Restore file comments coming from Slack (#583)

This commit is contained in:
Duco van Amstel 2018-11-12 14:58:00 +00:00 committed by Wim
parent cded603c27
commit e9419f10d3
2 changed files with 25 additions and 29 deletions

View File

@ -3,7 +3,6 @@ package bslack
import ( import (
"fmt" "fmt"
"html" "html"
"regexp"
"time" "time"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
@ -228,33 +227,23 @@ 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. // If we have files attached, download them (in memory) and put a pointer to it in msg.Extra.
for _, f := range ev.Files { for i := range ev.Files {
f := f if err := b.handleDownloadFile(rmsg, &ev.Files[i]); err != nil {
err := b.handleDownloadFile(rmsg, &f)
if err != nil {
b.Log.Errorf("Could not download incoming file: %#v", err) 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) { 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) channelInfo, err := b.getChannelByID(ev.Channel)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &config.Message{
rmsg := config.Message{
Channel: channelInfo.Name, Channel: channelInfo.Name,
Account: b.Account, Account: b.Account,
Event: config.EVENT_USER_TYPING, Event: config.EVENT_USER_TYPING,
} }, nil
return &rmsg, nil
} }
// handleDownloadFile handles file download // handleDownloadFile handles file download
@ -275,11 +264,11 @@ func (b *Bslack) handleDownloadFile(rmsg *config.Message, file *slack.File) erro
return fmt.Errorf("download %s failed %#v", file.URLPrivateDownload, err) return fmt.Errorf("download %s failed %#v", file.URLPrivateDownload, err)
} }
// Add the downloaded data to the message. // If a comment is attached to the file(s) it is in the 'Text' field of the Slack messge event
var comment string // and should be added as comment to only one of the files. We reset the 'Text' field to ensure
if results := commentRE.FindAllStringSubmatch(rmsg.Text, -1); len(results) > 0 { // that the comment is not duplicated.
comment = results[0][1] comment := rmsg.Text
} rmsg.Text = ""
helper.HandleDownloadData(b.Log, rmsg, file.Name, comment, file.URLPrivateDownload, data, b.General) helper.HandleDownloadData(b.Log, rmsg, file.Name, comment, file.URLPrivateDownload, data, b.General)
return nil return nil
} }

View File

@ -207,7 +207,7 @@ func (b *Bslack) Send(msg config.Message) (string, error) {
// sendWebhook uses the configured WebhookURL to send the message // sendWebhook uses the configured WebhookURL to send the message
func (b *Bslack) sendWebhook(msg config.Message) (string, error) { func (b *Bslack) sendWebhook(msg config.Message) (string, error) {
// skip events // Skip events.
if msg.Event != "" { if msg.Event != "" {
return "", nil return "", nil
} }
@ -217,7 +217,7 @@ func (b *Bslack) sendWebhook(msg config.Message) (string, error) {
} }
if msg.Extra != nil { 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) { for _, rmsg := range helper.HandleExtra(&msg, b.General) {
rmsg := rmsg // scopelint rmsg := rmsg // scopelint
iconURL := config.GetIconURL(&rmsg, b.GetString(iconURLConfig)) iconURL := config.GetIconURL(&rmsg, b.GetString(iconURLConfig))
@ -232,16 +232,20 @@ 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"] { 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 != "" { if fi.URL != "" {
msg.Text += " " + 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 var attachs []slack.Attachment
for _, attach := range msg.Extra[sSlackAttachment] { for _, attach := range msg.Extra[sSlackAttachment] {
attachs = append(attachs, attach.([]slack.Attachment)...) attachs = append(attachs, attach.([]slack.Attachment)...)
@ -258,9 +262,8 @@ func (b *Bslack) sendWebhook(msg config.Message) (string, error) {
if msg.Avatar != "" { if msg.Avatar != "" {
matterMessage.IconURL = msg.Avatar matterMessage.IconURL = msg.Avatar
} }
err := b.mh.Send(matterMessage) if err := b.mh.Send(matterMessage); err != nil {
if err != nil { b.Log.Errorf("Failed to send message via webhook: %#v", err)
b.Log.Error(err)
return "", err return "", err
} }
return "", nil return "", nil
@ -373,7 +376,11 @@ func (b *Bslack) postMessage(msg *config.Message, messageParameters *slack.PostM
// uploadFile handles native upload of files // uploadFile handles native upload of files
func (b *Bslack) uploadFile(msg *config.Message, channelID string) { func (b *Bslack) uploadFile(msg *config.Message, channelID string) {
for _, f := range msg.Extra["file"] { 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 { if msg.Text == fi.Comment {
msg.Text = "" msg.Text = ""
} }