mirror of
				https://github.com/matrix-org/gomatrix
				synced 2025-11-04 06:18:05 +00:00 
			
		
		
		
	Add function to set a users' avatar
This commit is contained in:
		
							parent
							
								
									aa984fcabc
								
							
						
					
					
						commit
						4d75d81067
					
				
					 2 changed files with 39 additions and 11 deletions
				
			
		
							
								
								
									
										27
									
								
								client.go
									
										
									
									
									
								
							
							
						
						
									
										27
									
								
								client.go
									
										
									
									
									
								
							| 
						 | 
					@ -403,18 +403,37 @@ func (cli *Client) GetAvatarURL() (url string, err error) {
 | 
				
			||||||
	s := struct {
 | 
						s := struct {
 | 
				
			||||||
		AvatarURL string `json:"avatar_url"`
 | 
							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 {
 | 
						if err != nil {
 | 
				
			||||||
		return "", err
 | 
							return "", err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err = json.Unmarshal(res, &s); err != nil {
 | 
						// if err = json.Unmarshal(res, &s); err != nil {
 | 
				
			||||||
		return "", err
 | 
						// 	return "", err
 | 
				
			||||||
	}
 | 
						// }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return s.AvatarURL, nil
 | 
						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
 | 
					// 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) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -25,13 +25,6 @@ func TestClient_LeaveRoom(t *testing.T) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestClient_GetAvatarUrl(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) {
 | 
						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" {
 | 
							if req.Method == "GET" && req.URL.Path == "/_matrix/client/r0/profile/@user:test.gomatrix.org/avatar_url" {
 | 
				
			||||||
			return &http.Response{
 | 
								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) {
 | 
					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" {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue