mirror of
https://github.com/cwinfo/matterbridge.git
synced 2024-11-22 02:20:28 +00:00
Handle Rocket.Chat attachments (#1395)
This commit is contained in:
parent
10f044c3dd
commit
c147ba1da1
@ -51,6 +51,30 @@ func DownloadFileAuth(url string, auth string) (*[]byte, error) {
|
|||||||
return &data, nil
|
return &data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DownloadFileAuthRocket downloads the given URL using the specified Rocket user ID and authentication token.
|
||||||
|
func DownloadFileAuthRocket(url, token, userID string) (*[]byte, error) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
client := &http.Client{
|
||||||
|
Timeout: time.Second * 5,
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
|
|
||||||
|
req.Header.Add("X-Auth-Token", token)
|
||||||
|
req.Header.Add("X-User-Id", userID)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
_, err = io.Copy(&buf, resp.Body)
|
||||||
|
data := buf.Bytes()
|
||||||
|
return &data, err
|
||||||
|
}
|
||||||
|
|
||||||
// GetSubLines splits messages in newline-delimited lines. If maxLineLength is
|
// GetSubLines splits messages in newline-delimited lines. If maxLineLength is
|
||||||
// specified as non-zero GetSubLines will also clip long lines to the maximum
|
// specified as non-zero GetSubLines will also clip long lines to the maximum
|
||||||
// length and insert a warning marker that the line was clipped.
|
// length and insert a warning marker that the line was clipped.
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
package brocketchat
|
package brocketchat
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
|
"github.com/42wim/matterbridge/bridge/helper"
|
||||||
"github.com/matterbridge/Rocket.Chat.Go.SDK/models"
|
"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) {
|
func (b *Brocketchat) handleRocketClient(messages chan *config.Message) {
|
||||||
for message := range b.messageChan {
|
for message := range b.messageChan {
|
||||||
|
message := message
|
||||||
// skip messages with same ID, apparently messages get duplicated for an unknown reason
|
// skip messages with same ID, apparently messages get duplicated for an unknown reason
|
||||||
if _, ok := b.cache.Get(message.ID); ok {
|
if _, ok := b.cache.Get(message.ID); ok {
|
||||||
continue
|
continue
|
||||||
@ -76,8 +80,11 @@ func (b *Brocketchat) handleRocketClient(messages chan *config.Message) {
|
|||||||
Account: b.Account,
|
Account: b.Account,
|
||||||
UserID: message.User.ID,
|
UserID: message.User.ID,
|
||||||
ID: message.ID,
|
ID: message.ID,
|
||||||
|
Extra: make(map[string][]interface{}),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b.handleAttachments(&message, rmsg)
|
||||||
|
|
||||||
// handleStatusEvent returns false if the message should be dropped
|
// 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
|
// in that case it is probably some modification to the channel we do not want to relay
|
||||||
if b.handleStatusEvent(m, rmsg) {
|
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 {
|
func (b *Brocketchat) handleUploadFile(msg *config.Message) error {
|
||||||
for _, f := range msg.Extra["file"] {
|
for _, f := range msg.Extra["file"] {
|
||||||
fi := f.(config.FileInfo)
|
fi := f.(config.FileInfo)
|
||||||
|
Loading…
Reference in New Issue
Block a user