From 95e86c5c754fda5ea00ab0966f55646a8d2929ee Mon Sep 17 00:00:00 2001 From: Yousef Mansy Date: Sat, 18 Feb 2023 16:41:19 -0800 Subject: [PATCH] Add option for using a naitive upload when relaying from discord Resolves #1965 --- bridge/config/config.go | 1 + bridge/discord/handlers.go | 48 +++++++++++++++++++++++++++++++------- matterbridge.toml.sample | 3 +++ 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/bridge/config/config.go b/bridge/config/config.go index 18c6092082..a35220186a 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -166,6 +166,7 @@ type Protocol struct { URL string // mattermost, slack // DEPRECATED UseAPI bool // mattermost, slack UseLocalAvatar []string // discord + UseNaitiveUpload bool // discord UseSASL bool // IRC UseTLS bool // IRC UseDiscriminator bool // discord diff --git a/bridge/discord/handlers.go b/bridge/discord/handlers.go index 34cef55480..952e78f3b5 100644 --- a/bridge/discord/handlers.go +++ b/bridge/discord/handlers.go @@ -1,7 +1,11 @@ package bdiscord import ( + "fmt" + "path" + "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/bridge/helper" "github.com/bwmarrin/discordgo" "github.com/davecgh/go-spew/spew" ) @@ -98,15 +102,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 != "" { @@ -138,8 +141,21 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat } } - // no empty messages - if rmsg.Text == "" { + if len(m.Attachments) > 0 { + if b.Config.GetBool("UseNaitiveUpload") { + b.handleDownloadFile(&rmsg, m) + } else { + // add the url of the attachments to content + for _, attach := range m.Attachments { + m.Content = m.Content + "\n" + attach.URL + } + } + } + + hasAttachment := len(rmsg.Extra["file"]) > 0 + + // no empty messages unless has attachment + if rmsg.Text == "" && !hasAttachment { return } @@ -279,3 +295,17 @@ func handleEmbed(embed *discordgo.MessageEmbed) string { return result } + +func (b *Bdiscord) handleDownloadFile(rmsg *config.Message, m *discordgo.MessageCreate) error { + for _, attach := range m.Attachments { + data, err := helper.DownloadFile(attach.URL) + + if err != nil { + return fmt.Errorf("download %s failed %#v", attach.URL, err) + } + + helper.HandleDownloadData(b.Log, rmsg, path.Base(attach.URL), rmsg.Text, attach.URL, data, b.General) + } + + return nil +} diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index 96104501e3..28ae64f835 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -907,6 +907,9 @@ ShowEmbeds=false # Example: ["irc"] UseLocalAvatar=[] +# UseNaitiveUpload will use a naitive upload when relaying from discord to other platforms rather than using public links +UseNaitiveUpload=false + # UseUserName shows the username instead of the server nickname UseUserName=false