aboutsummaryrefslogtreecommitdiff
path: root/mxlib/client.go
diff options
context:
space:
mode:
Diffstat (limited to 'mxlib/client.go')
-rw-r--r--mxlib/client.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/mxlib/client.go b/mxlib/client.go
index 2385d6e..6d87663 100644
--- a/mxlib/client.go
+++ b/mxlib/client.go
@@ -1,6 +1,7 @@
package mxlib
import (
+ "strings"
"bytes"
"encoding/json"
"fmt"
@@ -9,6 +10,8 @@ import (
"time"
log "github.com/sirupsen/logrus"
+
+ "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
)
type Client struct {
@@ -123,6 +126,29 @@ func (mx *Client) ProfileDisplayname(userid string, displayname string) error {
return err
}
+func (mx *Client) ProfileAvatar(userid string, m connector.MediaObject) error {
+ var mxc *MediaObject
+ if mxm, ok := m.(*MediaObject); ok {
+ mxc = mxm
+ } else {
+ mxm, err := mx.UploadMedia(m)
+ if err != nil {
+ return err
+ }
+ mxc = mxm
+ }
+ mxc_uri := fmt.Sprintf("mxc://%s/%s", mxc.MxcServer, mxc.MxcMediaId)
+
+ req := ProfileAvatarUrl{
+ AvatarUrl: mxc_uri,
+ }
+ var rep struct{}
+ err := mx.PutApiCall(fmt.Sprintf("/_matrix/client/r0/profile/%s/avatar_url?user_id=%s",
+ url.QueryEscape(userid), url.QueryEscape(userid)),
+ &req, &rep)
+ return err
+}
+
func (mx *Client) DirectoryRoom(alias string) (string, error) {
var rep DirectoryRoomResponse
err := mx.GetApiCall("/_matrix/client/r0/directory/room/"+url.QueryEscape(alias), &rep)
@@ -252,3 +278,39 @@ func (mx *Client) RoomTopicAs(room string, topic string, as_user string) error {
}
return mx.PutStateAs(room, "m.room.topic", "", content, as_user)
}
+
+func (mx *Client) UploadMedia(m connector.MediaObject) (*MediaObject, error) {
+ reader, err := m.Read()
+ if err != nil {
+ return nil, err
+ }
+ defer reader.Close()
+
+ req, err := http.NewRequest("POST",
+ mx.Server+"/_matrix/media/r0/upload?filename="+url.QueryEscape(m.Filename()),
+ reader)
+ req.Header.Add("Content-Type", m.Mimetype())
+ req.ContentLength = m.Size()
+
+ var resp UploadResponse
+ err = mx.DoAndParse(req, &resp)
+ if err != nil {
+ return nil, err
+ }
+
+ mxc := strings.Split(strings.Replace(resp.ContentUri, "mxc://", "", 1), "/")
+ if len(mxc) != 2 {
+ return nil, fmt.Errorf("Invalid mxc:// returned: %s", resp.ContentUri)
+ }
+
+ media := &MediaObject{
+ mxClient: mx,
+ filename: m.Filename(),
+ size: m.Size(),
+ mimetype: m.Mimetype(),
+ MxcServer: mxc[0],
+ MxcMediaId: mxc[1],
+ }
+ return media, nil
+}
+