From 54ed6320c2cc4a9748f9faec5780603aeee3de8c Mon Sep 17 00:00:00 2001 From: Wim Date: Mon, 10 Feb 2020 00:06:54 +0100 Subject: Add support for avatars from matrix. #984 (#1007) --- bridge/matrix/matrix.go | 23 ++++++++++++++++++++--- go.mod | 2 +- go.sum | 2 ++ vendor/github.com/matterbridge/gomatrix/client.go | 15 +++++++++++++++ vendor/modules.txt | 2 +- 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/bridge/matrix/matrix.go b/bridge/matrix/matrix.go index 16562c79..824870cf 100644 --- a/bridge/matrix/matrix.go +++ b/bridge/matrix/matrix.go @@ -172,10 +172,15 @@ func (b *Bmatrix) handleEvent(ev *matrix.Event) { return } - // TODO download avatar - // Create our message - rmsg := config.Message{Username: ev.Sender[1:], Channel: channel, Account: b.Account, UserID: ev.Sender, ID: ev.ID} + rmsg := config.Message{ + Username: ev.Sender[1:], + Channel: channel, + Account: b.Account, + UserID: ev.Sender, + ID: ev.ID, + Avatar: b.getAvatarURL(ev.Sender), + } // Text must be a string if rmsg.Text, ok = ev.Content["body"].(string); !ok { @@ -358,3 +363,15 @@ func (b *Bmatrix) containsAttachment(content map[string]interface{}) bool { } return true } + +// getAvatarURL returns the avatar URL of the specified sender +func (b *Bmatrix) getAvatarURL(sender string) string { + mxcURL, err := b.mc.GetSenderAvatarURL(sender) + if err != nil { + b.Log.Errorf("getAvatarURL failed: %s", err) + return "" + } + url := strings.ReplaceAll(mxcURL, "mxc://", b.GetString("Server")+"/_matrix/media/r0/thumbnail/") + url += "?width=37&height=37&method=crop" + return url +} diff --git a/go.mod b/go.mod index 6e349924..2f19f2e1 100644 --- a/go.mod +++ b/go.mod @@ -25,7 +25,7 @@ require ( github.com/matterbridge/Rocket.Chat.Go.SDK v0.0.0-20190210153444-cc9d05784d5d github.com/matterbridge/emoji v2.1.1-0.20191117213217-af507f6b02db+incompatible github.com/matterbridge/go-xmpp v0.0.0-20180529212104-cd19799fba91 - github.com/matterbridge/gomatrix v0.0.0-20200209221412-326bea8d866e + github.com/matterbridge/gomatrix v0.0.0-20200209224845-c2104d7936a6 github.com/matterbridge/gozulipbot v0.0.0-20190212232658-7aa251978a18 github.com/matterbridge/logrus-prefixed-formatter v0.0.0-20180806162718-01618749af61 github.com/mattermost/mattermost-server v5.5.0+incompatible diff --git a/go.sum b/go.sum index ce9b3bea..9232baaa 100644 --- a/go.sum +++ b/go.sum @@ -134,6 +134,8 @@ github.com/matterbridge/go-xmpp v0.0.0-20180529212104-cd19799fba91 h1:KzDEcy8eDb github.com/matterbridge/go-xmpp v0.0.0-20180529212104-cd19799fba91/go.mod h1:ECDRehsR9TYTKCAsRS8/wLeOk6UUqDydw47ln7wG41Q= github.com/matterbridge/gomatrix v0.0.0-20200209221412-326bea8d866e h1:i1wiGiwjRZ1Yy0JbERoDeikLzOnM6tkkQTT20b3Yy1E= github.com/matterbridge/gomatrix v0.0.0-20200209221412-326bea8d866e/go.mod h1:+jWeaaUtXQbBRdKYWfjW6JDDYiI2XXE+3NnTjW5kg8g= +github.com/matterbridge/gomatrix v0.0.0-20200209224845-c2104d7936a6 h1:Kl65VJv38HjYFnnwH+MP6Z8hcJT5UHuSpHVU5vW1HH0= +github.com/matterbridge/gomatrix v0.0.0-20200209224845-c2104d7936a6/go.mod h1:+jWeaaUtXQbBRdKYWfjW6JDDYiI2XXE+3NnTjW5kg8g= github.com/matterbridge/gozulipbot v0.0.0-20190212232658-7aa251978a18 h1:fLhwXtWGtfTgZVxHG1lcKjv+re7dRwyyuYFNu69xdho= github.com/matterbridge/gozulipbot v0.0.0-20190212232658-7aa251978a18/go.mod h1:yAjnZ34DuDyPHMPHHjOsTk/FefW4JJjoMMCGt/8uuQA= github.com/matterbridge/logrus-prefixed-formatter v0.0.0-20180806162718-01618749af61 h1:R/MgM/eUyRBQx2FiH6JVmXck8PaAuKfe2M1tWIzW7nE= diff --git a/vendor/github.com/matterbridge/gomatrix/client.go b/vendor/github.com/matterbridge/gomatrix/client.go index 09e62d24..6dda4bc4 100644 --- a/vendor/github.com/matterbridge/gomatrix/client.go +++ b/vendor/github.com/matterbridge/gomatrix/client.go @@ -475,6 +475,21 @@ func (cli *Client) GetAvatarURL() (string, error) { return s.AvatarURL, nil } +// GetAvatarURL gets the user's avatar URL. See http://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-profile-userid-avatar-url +func (cli *Client) GetSenderAvatarURL(sender string) (string, error) { + urlPath := cli.BuildURL("profile", sender, "avatar_url") + s := struct { + AvatarURL string `json:"avatar_url"` + }{} + + err := cli.MakeRequest("GET", urlPath, nil, &s) + if err != nil { + return "", err + } + + return s.AvatarURL, nil +} + // SetAvatarURL sets the user's avatar URL. See http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-profile-userid-avatar-url func (cli *Client) SetAvatarURL(url string) error { urlPath := cli.BuildURL("profile", cli.UserID, "avatar_url") diff --git a/vendor/modules.txt b/vendor/modules.txt index 85b7f089..1d3a2f21 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -105,7 +105,7 @@ github.com/matterbridge/Rocket.Chat.Go.SDK/rest github.com/matterbridge/emoji # github.com/matterbridge/go-xmpp v0.0.0-20180529212104-cd19799fba91 github.com/matterbridge/go-xmpp -# github.com/matterbridge/gomatrix v0.0.0-20200209221412-326bea8d866e +# github.com/matterbridge/gomatrix v0.0.0-20200209224845-c2104d7936a6 github.com/matterbridge/gomatrix # github.com/matterbridge/gozulipbot v0.0.0-20190212232658-7aa251978a18 github.com/matterbridge/gozulipbot -- cgit v1.2.3