Merge pull request #19 from matrix-org/rxl881/get_avatar

Added function to get a user's avatar URL
This commit is contained in:
Richard Lewis 2017-02-17 11:53:43 +00:00 committed by GitHub
commit 812dcb5515
2 changed files with 66 additions and 0 deletions

View File

@ -397,6 +397,35 @@ func (cli *Client) SetDisplayName(displayName string) (err error) {
return 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 // 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. // 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) { func (cli *Client) SendMessageEvent(roomID string, eventType string, contentJSON interface{}) (resp *RespSendEvent, err error) {

View File

@ -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) { func TestClient_StateEvent(t *testing.T) {
cli := mockClient(func(req *http.Request) (*http.Response, error) { 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" { if req.Method == "GET" && req.URL.Path == "/_matrix/client/r0/rooms/!foo:bar/state/m.room.name" {