From fd768a10be36ec31f674fa291fcbe77b78a2855c Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 21 Feb 2020 18:08:40 +0100 Subject: Mattermost media objects in both ways + user/team profile pictures from MM to Matrix --- mxlib/client.go | 40 +++++++++++++++++++++++++++++++++++++--- mxlib/mediaobject.go | 4 ++++ 2 files changed, 41 insertions(+), 3 deletions(-) (limited to 'mxlib') 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 +} diff --git a/mxlib/mediaobject.go b/mxlib/mediaobject.go index 1c35187..f29127b 100644 --- a/mxlib/mediaobject.go +++ b/mxlib/mediaobject.go @@ -59,3 +59,7 @@ func (m *MediaObject) URL() string { return fmt.Sprintf("%s/_matrix/media/r0/download/%s/%s/%s", m.mxClient.Server, m.MxcServer, m.MxcMediaId, url.QueryEscape(m.filename)) } + +func (m *MediaObject) MxcUri() string { + return fmt.Sprintf("mxc://%s/%s", m.MxcServer, m.MxcMediaId) +} -- cgit v1.2.3