aboutsummaryrefslogtreecommitdiff
path: root/mxlib/client.go
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2020-02-21 18:08:40 +0100
committerAlex Auvolat <alex@adnab.me>2020-02-21 18:08:40 +0100
commitfd768a10be36ec31f674fa291fcbe77b78a2855c (patch)
treebb9e736bfc9425e1ce5ee22379e8d46470af4d18 /mxlib/client.go
parentddd5936fb1f92432123a9a30d1d3a1fa644a4f8e (diff)
downloadeasybridge-fd768a10be36ec31f674fa291fcbe77b78a2855c.tar.gz
easybridge-fd768a10be36ec31f674fa291fcbe77b78a2855c.zip
Mattermost media objects in both ways + user/team profile pictures from MM to Matrix
Diffstat (limited to 'mxlib/client.go')
-rw-r--r--mxlib/client.go40
1 files changed, 37 insertions, 3 deletions
diff --git a/mxlib/client.go b/mxlib/client.go
index d8237d1..e07a67a 100644
--- a/mxlib/client.go
+++ b/mxlib/client.go
@@ -137,10 +137,9 @@ func (mx *Client) ProfileAvatar(userid string, m connector.MediaObject) error {
}
mxc = mxm
}
- mxc_uri := fmt.Sprintf("mxc://%s/%s", mxc.MxcServer, mxc.MxcMediaId)
req := ProfileAvatarUrl{
- AvatarUrl: mxc_uri,
+ AvatarUrl: mxc.MxcUri(),
}
var rep struct{}
err := mx.PutApiCall(fmt.Sprintf("/_matrix/client/r0/profile/%s/avatar_url?user_id=%s",
@@ -272,6 +271,21 @@ func (mx *Client) RoomNameAs(room string, name string, as_user string) error {
return mx.PutStateAs(room, "m.room.name", "", content, as_user)
}
+func (mx *Client) RoomAvatarAs(room string, pic connector.MediaObject, as_user string) error {
+ mo, err := mx.UploadMedia(pic)
+ if err != nil {
+ return err
+ }
+ content := map[string]interface{}{
+ "url": mo.MxcUri(),
+ "info": map[string]interface{}{
+ "mimetype": mo.Mimetype(),
+ "size": mo.Size(),
+ },
+ }
+ return mx.PutStateAs(room, "m.room.avatar", "", content, as_user)
+}
+
func (mx *Client) RoomTopicAs(room string, topic string, as_user string) error {
content := map[string]interface{}{
"topic": topic,
@@ -295,7 +309,7 @@ func (mx *Client) UploadMedia(m connector.MediaObject) (*MediaObject, error) {
mx.Server+"/_matrix/media/r0/upload?filename="+url.QueryEscape(m.Filename()),
reader)
req.Header.Add("Content-Type", m.Mimetype())
- req.ContentLength = m.Size()
+ req.ContentLength = m.Size() // TODO: this wasn't specified as mandatory in the matrix client/server spec, do a PR to fix this
var resp UploadResponse
err = mx.DoAndParse(req, &resp)
@@ -320,3 +334,23 @@ func (mx *Client) UploadMedia(m connector.MediaObject) (*MediaObject, error) {
return media, nil
}
+func (mx *Client) ParseMediaInfo(content map[string]interface{}) *MediaObject {
+ // Content is an event content of type m.file or m.image
+ info := content["info"].(map[string]interface{})
+ mxc := strings.Split(strings.Replace(content["url"].(string), "mxc://", "", 1), "/")
+ media := &MediaObject{
+ mxClient: mx,
+ filename: content["body"].(string),
+ size: int64(info["size"].(float64)),
+ mimetype: info["mimetype"].(string),
+ MxcServer: mxc[0],
+ MxcMediaId: mxc[1],
+ }
+ if content["msgtype"].(string) == "m.image" {
+ media.imageSize = &connector.ImageSize{
+ Width: int(info["w"].(float64)),
+ Height: int(info["h"].(float64)),
+ }
+ }
+ return media
+}