Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use native upload when relaying from discord #1977

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions bridge/discord/handlers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package bdiscord

import (
"path"

"github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/helper"
"github.com/bwmarrin/discordgo"
"github.com/davecgh/go-spew/spew"
)
Expand Down Expand Up @@ -98,15 +101,14 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
return
}

// add the url of the attachments to content
if len(m.Attachments) > 0 {
for _, attach := range m.Attachments {
m.Content = m.Content + "\n" + attach.URL
}
rmsg := config.Message{
Account: b.Account,
Avatar: "https://cdn.discordapp.com/avatars/" + m.Author.ID + "/" + m.Author.Avatar + ".jpg",
UserID: m.Author.ID,
ID: m.ID,
Extra: make(map[string][]interface{}),
}

rmsg := config.Message{Account: b.Account, Avatar: "https://cdn.discordapp.com/avatars/" + m.Author.ID + "/" + m.Author.Avatar + ".jpg", UserID: m.Author.ID, ID: m.ID}

b.Log.Debugf("== Receiving event %#v", m.Message)

if m.Content != "" {
Expand Down Expand Up @@ -138,8 +140,14 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
}
}

// no empty messages
if rmsg.Text == "" {
if len(m.Attachments) > 0 {
b.handleDownloadFile(&rmsg, m)
}

hasAttachment := len(rmsg.Extra["file"]) > 0

// no empty messages unless has attachment
if rmsg.Text == "" && !hasAttachment {
return
}

Expand Down Expand Up @@ -279,3 +287,14 @@ func handleEmbed(embed *discordgo.MessageEmbed) string {

return result
}

func (b *Bdiscord) handleDownloadFile(rmsg *config.Message, m *discordgo.MessageCreate) {
for _, attach := range m.Attachments {
data, err := helper.DownloadFile(attach.URL)
if err != nil {
b.Log.Errorf("download %s failed %#v", attach.URL, err)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the pull request and had the issue that images were not being displayed on Element web. The image name still contained the parameters of the image url. I'd maybe add code to remove the parameters from an url before using it as filename.

		urlClean, _, _ := strings.Cut(attach.URL, "?")

		helper.HandleDownloadData(b.Log, rmsg, path.Base(urlClean), rmsg.Text, attach.URL, data, b.General)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm that this works for me.

helper.HandleDownloadData(b.Log, rmsg, path.Base(attach.URL), rmsg.Text, attach.URL, data, b.General)
}
}