From 44411b47890da51e7c8829e72376167106efffa7 Mon Sep 17 00:00:00 2001 From: other8026 <146984625+other8026@users.noreply.github.com> Date: Mon, 12 Feb 2024 15:53:27 +0800 Subject: [PATCH 1/6] move logic for getting url to its own function --- bridge/matrix/matrix.go | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/bridge/matrix/matrix.go b/bridge/matrix/matrix.go index 49fc33b3e7..487a831f40 100644 --- a/bridge/matrix/matrix.go +++ b/bridge/matrix/matrix.go @@ -556,6 +556,21 @@ func (b *Bmatrix) handleEvent(ev *matrix.Event) { } } +// getMessageFileUrl changes the Matrix mxc:// uploads and changes them to a regular url +func (b *Bmatrix) getMessageFileUrl(content map[string]interface{}) (string, error) { + var ( + ok bool + url string + ) + + if url, ok = content["url"].(string); !ok { + return "", fmt.Errorf("url isn't a %T", url) + } + url = strings.Replace(url, "mxc://", b.GetString("Server")+"/_matrix/media/v1/download/", -1) + + return url, nil +} + // handleDownloadFile handles file download func (b *Bmatrix) handleDownloadFile(rmsg *config.Message, content map[string]interface{}) error { var ( @@ -566,10 +581,17 @@ func (b *Bmatrix) handleDownloadFile(rmsg *config.Message, content map[string]in ) rmsg.Extra = make(map[string][]interface{}) - if url, ok = content["url"].(string); !ok { - return fmt.Errorf("url isn't a %T", url) + + //// moved to getMessageFileUrl + //if url, ok = content["url"].(string); !ok { + // return fmt.Errorf("url isn't a %T", url) + //} + //url = strings.Replace(url, "mxc://", b.GetString("Server")+"/_matrix/media/v1/download/", -1) + + url, err := b.getMessageFileUrl(content) + if err != nil { + return err } - url = strings.Replace(url, "mxc://", b.GetString("Server")+"/_matrix/media/v1/download/", -1) if info, ok = content["info"].(map[string]interface{}); !ok { return fmt.Errorf("info isn't a %T", info) @@ -601,7 +623,7 @@ func (b *Bmatrix) handleDownloadFile(rmsg *config.Message, content map[string]in } // check if the size is ok - err := helper.HandleDownloadSize(b.Log, rmsg, name, int64(size), b.General) + err = helper.HandleDownloadSize(b.Log, rmsg, name, int64(size), b.General) if err != nil { return err } From 2f12fa3c6188de1be3b6920abdbe7e5f918889a0 Mon Sep 17 00:00:00 2001 From: other8026 <146984625+other8026@users.noreply.github.com> Date: Mon, 12 Feb 2024 16:05:16 +0800 Subject: [PATCH 2/6] add to Matrix config to send url not a file --- bridge/config/config.go | 1 + matterbridge.toml.sample | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/bridge/config/config.go b/bridge/config/config.go index 18c6092082..6fa2715478 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -146,6 +146,7 @@ type Protocol struct { ReplaceNicks [][]string // all protocols RemoteNickFormat string // all protocols RunCommands []string // IRC + SendUrlNotFile bool // matrix Server string // IRC,mattermost,XMPP,discord,matrix SessionFile string // msteams,whatsapp ShowJoinPart bool // all protocols diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index 5932b269a3..b78461eea8 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -1329,6 +1329,10 @@ StripNick=false #OPTIONAL (default false) ShowTopicChange=false +#Enable to send a URL rather than an image +#(the default behavior in Matterbridge is to download and send a file) +SendUrlNotFile=true + ################################################################### #steam section ################################################################### From fd38b8d54a9f9034bbd662b1be2f00fe6a6f4eb4 Mon Sep 17 00:00:00 2001 From: other8026 <146984625+other8026@users.noreply.github.com> Date: Mon, 12 Feb 2024 16:10:53 +0800 Subject: [PATCH 3/6] send URL for files if config is set to send URLs --- bridge/matrix/matrix.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/bridge/matrix/matrix.go b/bridge/matrix/matrix.go index 487a831f40..2b805214dc 100644 --- a/bridge/matrix/matrix.go +++ b/bridge/matrix/matrix.go @@ -540,9 +540,18 @@ func (b *Bmatrix) handleEvent(ev *matrix.Event) { // Do we have attachments if b.containsAttachment(ev.Content) { - err := b.handleDownloadFile(&rmsg, ev.Content) - if err != nil { - b.Log.Errorf("download failed: %#v", err) + if b.GetBool("SendUrlNotFile") { + // replace the text of the message (which is set to the filename above) with the url + var err error + rmsg.Text, err = b.getMessageFileUrl(ev.Content) + if err != nil { + b.Log.Errorf("get url for uploaded file failed: %#v", err) + } + } else { + err := b.handleDownloadFile(&rmsg, ev.Content) + if err != nil { + b.Log.Errorf("download failed: %#v", err) + } } } From 2b9916cf3465b0e0f5578d192cddc3e7d536d56e Mon Sep 17 00:00:00 2001 From: other8026 <146984625+other8026@users.noreply.github.com> Date: Mon, 12 Feb 2024 22:44:29 +0800 Subject: [PATCH 4/6] Update matterbridge.toml.sample comment out for pull request --- matterbridge.toml.sample | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index b78461eea8..31d8603ef8 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -1331,7 +1331,7 @@ ShowTopicChange=false #Enable to send a URL rather than an image #(the default behavior in Matterbridge is to download and send a file) -SendUrlNotFile=true +#SendUrlNotFile=true ################################################################### #steam section From e96d6e346ad8a81e6c43526901b14fae4ebf26c2 Mon Sep 17 00:00:00 2001 From: other8026 <146984625+other8026@users.noreply.github.com> Date: Mon, 12 Feb 2024 22:47:12 +0800 Subject: [PATCH 5/6] clarify comment for sample configuration file --- matterbridge.toml.sample | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index 31d8603ef8..91ab309325 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -1330,7 +1330,8 @@ StripNick=false ShowTopicChange=false #Enable to send a URL rather than an image -#(the default behavior in Matterbridge is to download and send a file) +#The default behavior is to download and send/upload the file. +#OPTIONAL (default false) #SendUrlNotFile=true ################################################################### From 7f209248e3bf9612be76e0e0dfd7e3153b184716 Mon Sep 17 00:00:00 2001 From: other8026 <146984625+other8026@users.noreply.github.com> Date: Wed, 14 Feb 2024 15:36:43 +0800 Subject: [PATCH 6/6] fix a couple of comments --- bridge/matrix/matrix.go | 6 ------ matterbridge.toml.sample | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/bridge/matrix/matrix.go b/bridge/matrix/matrix.go index 2b805214dc..9e3073a3df 100644 --- a/bridge/matrix/matrix.go +++ b/bridge/matrix/matrix.go @@ -591,12 +591,6 @@ func (b *Bmatrix) handleDownloadFile(rmsg *config.Message, content map[string]in rmsg.Extra = make(map[string][]interface{}) - //// moved to getMessageFileUrl - //if url, ok = content["url"].(string); !ok { - // return fmt.Errorf("url isn't a %T", url) - //} - //url = strings.Replace(url, "mxc://", b.GetString("Server")+"/_matrix/media/v1/download/", -1) - url, err := b.getMessageFileUrl(content) if err != nil { return err diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index 91ab309325..367f9837a3 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -1329,7 +1329,7 @@ StripNick=false #OPTIONAL (default false) ShowTopicChange=false -#Enable to send a URL rather than an image +#Enable to send a URL rather than a file #The default behavior is to download and send/upload the file. #OPTIONAL (default false) #SendUrlNotFile=true