diff --git a/client.go b/client.go index 87a1f73..29784eb 100644 --- a/client.go +++ b/client.go @@ -397,6 +397,35 @@ func (cli *Client) SetDisplayName(displayName string) (err error) { return } +// 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) GetAvatarURL() (url string, err error) { + urlPath := cli.BuildURL("profile", cli.UserID, "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) (err error) { + urlPath := cli.BuildURL("profile", cli.UserID, "avatar_url") + s := struct { + AvatarURL string `json:"avatar_url"` + }{url} + _, err = cli.MakeRequest("PUT", urlPath, &s, nil) + if err != nil { + return err + } + + return nil +} + // SendMessageEvent sends a message event into a room. See http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid // contentJSON should be a pointer to something that can be encoded as JSON using json.Marshal. func (cli *Client) SendMessageEvent(roomID string, eventType string, contentJSON interface{}) (resp *RespSendEvent, err error) { diff --git a/client_test.go b/client_test.go index bd99260..9b7d2da 100644 --- a/client_test.go +++ b/client_test.go @@ -24,6 +24,43 @@ func TestClient_LeaveRoom(t *testing.T) { } } +func TestClient_GetAvatarUrl(t *testing.T) { + cli := mockClient(func(req *http.Request) (*http.Response, error) { + if req.Method == "GET" && req.URL.Path == "/_matrix/client/r0/profile/@user:test.gomatrix.org/avatar_url" { + return &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewBufferString(`{"avatar_url":"mxc://matrix.org/iJaUjkshgdfsdkjfn"}`)), + }, nil + } + return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path) + }) + + if response, err := cli.GetAvatarURL(); err != nil { + t.Fatalf("GetAvatarURL: Got error: %s", err.Error()) + } else if response == "" { + t.Fatal("GetAvatarURL: Got empty response") + } else if response != "mxc://matrix.org/iJaUjkshgdfsdkjfn" { + t.Fatalf("Unexpected response URL: %s", response) + } + +} + +func TestClient_SetAvatarUrl(t *testing.T) { + cli := mockClient(func(req *http.Request) (*http.Response, error) { + if req.Method == "PUT" && req.URL.Path == "/_matrix/client/r0/profile/@user:test.gomatrix.org/avatar_url" { + return &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewBufferString(`{}`)), + }, nil + } + return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path) + }) + + if err := cli.SetAvatarURL("https://foo.com/bar.png"); err != nil { + t.Fatalf("GetAvatarURL: Got error: %s", err.Error()) + } +} + func TestClient_StateEvent(t *testing.T) { cli := mockClient(func(req *http.Request) (*http.Response, error) { if req.Method == "GET" && req.URL.Path == "/_matrix/client/r0/rooms/!foo:bar/state/m.room.name" {