diff --git a/client.go b/client.go index c8b8968..623a94b 100644 --- a/client.go +++ b/client.go @@ -403,18 +403,37 @@ func (cli *Client) GetAvatarURL() (url string, err error) { s := struct { AvatarURL string `json:"avatar_url"` }{} - res, err := cli.MakeRequest("GET", urlPath, &s, nil) + // res, err := cli.MakeRequest("GET", urlPath, nil, &s) + _, err = cli.MakeRequest("GET", urlPath, nil, &s) if err != nil { return "", err } - if err = json.Unmarshal(res, &s); err != nil { - return "", err - } + // if err = json.Unmarshal(res, &s); 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} + res, err := cli.MakeRequest("PUT", urlPath, &s, nil) + if err != nil { + return err + } + + if err = json.Unmarshal(res, &s); 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 32bcd19..f238e48 100644 --- a/client_test.go +++ b/client_test.go @@ -25,13 +25,6 @@ func TestClient_LeaveRoom(t *testing.T) { } func TestClient_GetAvatarUrl(t *testing.T) { - // cli, _ := NewClient("https://matrix.org", "@_neb_google:matrix.org", "MDAxOGxvY2F0aW9uIG1hdHJpeC5vcmcKMDAxM2lkZW50aWZpZXIga2V5CjAwMTBjaWQgZ2VuID0gMQowMDJhY2lkIHVzZXJfaWQgPSBAX25lYl9nb29nbGU6bWF0cml4Lm9yZwowMDE2Y2lkIHR5cGUgPSBhY2Nlc3MKMDAyMWNpZCBub25jZSA9IDpUWHlDZ01KLnU3NFNWeFMKMDAyZnNpZ25hdHVyZSDSP25lx3Mm4JQ-eC5a5pTHxXpHYGFg55bYQNyEpofc2wo") - // url, err := cli.GetAvatarURL() - // if err != nil { - // t.Fatalf("Failed to get avatar URL: %s", err.Error()) - // } - // fmt.Printf("URL: %s\n", url) - 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{ @@ -50,6 +43,22 @@ func TestClient_GetAvatarUrl(t *testing.T) { } +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" {