diff options
author | Tadeo Kondrak <me@tadeo.ca> | 2021-02-15 14:34:14 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-15 22:34:14 +0100 |
commit | c147ba1da1d4d5f181c4de796e414a72248447cc (patch) | |
tree | c1ca3e66f1a29c5820b8c17da098570344a9caa7 /bridge/rocketchat | |
parent | 10f044c3dd971868523459b708a5bcbf81611df7 (diff) | |
download | matterbridge-msglm-c147ba1da1d4d5f181c4de796e414a72248447cc.tar.gz matterbridge-msglm-c147ba1da1d4d5f181c4de796e414a72248447cc.tar.bz2 matterbridge-msglm-c147ba1da1d4d5f181c4de796e414a72248447cc.zip |
Handle Rocket.Chat attachments (#1395)
Diffstat (limited to 'bridge/rocketchat')
-rw-r--r-- | bridge/rocketchat/handlers.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/bridge/rocketchat/handlers.go b/bridge/rocketchat/handlers.go index 80adb31e..03b66eac 100644 --- a/bridge/rocketchat/handlers.go +++ b/bridge/rocketchat/handlers.go @@ -1,7 +1,10 @@ package brocketchat import ( + "fmt" + "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/bridge/helper" "github.com/matterbridge/Rocket.Chat.Go.SDK/models" ) @@ -58,6 +61,7 @@ func (b *Brocketchat) handleStatusEvent(ev models.Message, rmsg *config.Message) func (b *Brocketchat) handleRocketClient(messages chan *config.Message) { for message := range b.messageChan { + message := message // skip messages with same ID, apparently messages get duplicated for an unknown reason if _, ok := b.cache.Get(message.ID); ok { continue @@ -76,8 +80,11 @@ func (b *Brocketchat) handleRocketClient(messages chan *config.Message) { Account: b.Account, UserID: message.User.ID, ID: message.ID, + Extra: make(map[string][]interface{}), } + b.handleAttachments(&message, rmsg) + // handleStatusEvent returns false if the message should be dropped // in that case it is probably some modification to the channel we do not want to relay if b.handleStatusEvent(m, rmsg) { @@ -86,6 +93,38 @@ func (b *Brocketchat) handleRocketClient(messages chan *config.Message) { } } +func (b *Brocketchat) handleAttachments(message *models.Message, rmsg *config.Message) { + if rmsg.Text == "" { + for _, attachment := range message.Attachments { + if attachment.Title != "" { + rmsg.Text = attachment.Title + "\n" + } + if attachment.Title != "" && attachment.Text != "" { + rmsg.Text += "\n" + } + if attachment.Text != "" { + rmsg.Text += attachment.Text + } + } + } + + for i := range message.Attachments { + if err := b.handleDownloadFile(rmsg, &message.Attachments[i]); err != nil { + b.Log.Errorf("Could not download incoming file: %#v", err) + } + } +} + +func (b *Brocketchat) handleDownloadFile(rmsg *config.Message, file *models.Attachment) error { + downloadURL := b.GetString("server") + file.TitleLink + data, err := helper.DownloadFileAuthRocket(downloadURL, b.user.Token, b.user.ID) + if err != nil { + return fmt.Errorf("download %s failed %#v", downloadURL, err) + } + helper.HandleDownloadData(b.Log, rmsg, file.Title, rmsg.Text, downloadURL, data, b.General) + return nil +} + func (b *Brocketchat) handleUploadFile(msg *config.Message) error { for _, f := range msg.Extra["file"] { fi := f.(config.FileInfo) |