mirror of
				https://github.com/matrix-org/gomatrix
				synced 2025-11-03 22:08:04 +00:00 
			
		
		
		
	Merge branch 'master' into kegan/api-logout
This commit is contained in:
		
						commit
						b6093bbf56
					
				
					 3 changed files with 117 additions and 1 deletions
				
			
		
							
								
								
									
										71
									
								
								client.go
									
										
									
									
									
								
							
							
						
						
									
										71
									
								
								client.go
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -362,6 +362,13 @@ func (cli *Client) Logout(removeCredentials bool) (resp *RespLogout, err error)
 | 
			
		|||
    return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Versions returns the list of supported Matrix versions on this homeserver. See http://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-versions
 | 
			
		||||
func (cli *Client) Versions() (resp *RespVersions, err error) {
 | 
			
		||||
	urlPath := cli.BuildBaseURL("_matrix", "client", "versions")
 | 
			
		||||
	_, err = cli.MakeRequest("GET", urlPath, nil, &resp)
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// JoinRoom joins the client to a room ID or alias. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-join-roomidoralias
 | 
			
		||||
//
 | 
			
		||||
// If serverName is specified, this will be added as a query param to instruct the homeserver to join via that server. If content is specified, it will
 | 
			
		||||
| 
						 | 
				
			
			@ -392,12 +399,20 @@ func (cli *Client) SetDisplayName(displayName string) (err error) {
 | 
			
		|||
// 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) {
 | 
			
		||||
	txnID := "go" + strconv.FormatInt(time.Now().UnixNano(), 10)
 | 
			
		||||
	txnID := txnID()
 | 
			
		||||
	urlPath := cli.BuildURL("rooms", roomID, "send", eventType, txnID)
 | 
			
		||||
	_, err = cli.MakeRequest("PUT", urlPath, contentJSON, &resp)
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// SendStateEvent sends a state event into a room. See http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-state-eventtype-statekey
 | 
			
		||||
// contentJSON should be a pointer to something that can be encoded as JSON using json.Marshal.
 | 
			
		||||
func (cli *Client) SendStateEvent(roomID, eventType, stateKey string, contentJSON interface{}) (resp *RespSendEvent, err error) {
 | 
			
		||||
	urlPath := cli.BuildURL("rooms", roomID, "state", eventType, stateKey)
 | 
			
		||||
	_, err = cli.MakeRequest("PUT", urlPath, contentJSON, &resp)
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// SendText sends an m.room.message event into the given room with a msgtype of m.text
 | 
			
		||||
// See http://matrix.org/docs/spec/client_server/r0.2.0.html#m-text
 | 
			
		||||
func (cli *Client) SendText(roomID, text string) (*RespSendEvent, error) {
 | 
			
		||||
| 
						 | 
				
			
			@ -405,6 +420,14 @@ func (cli *Client) SendText(roomID, text string) (*RespSendEvent, error) {
 | 
			
		|||
		TextMessage{"m.text", text})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RedactEvent redacts the given event. See http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-redact-eventid-txnid
 | 
			
		||||
func (cli *Client) RedactEvent(roomID, eventID string, req *ReqRedact) (resp *RespSendEvent, err error) {
 | 
			
		||||
	txnID := txnID()
 | 
			
		||||
	urlPath := cli.BuildURL("rooms", roomID, "redact", eventID, txnID)
 | 
			
		||||
	_, err = cli.MakeRequest("PUT", urlPath, req, &resp)
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateRoom creates a new Matrix room. See https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom
 | 
			
		||||
//  resp, err := cli.CreateRoom(&gomatrix.ReqCreateRoom{
 | 
			
		||||
//  	Preset: "public_chat",
 | 
			
		||||
| 
						 | 
				
			
			@ -423,6 +446,48 @@ func (cli *Client) LeaveRoom(roomID string) (resp *RespLeaveRoom, err error) {
 | 
			
		|||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ForgetRoom forgets a room entirely. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-forget
 | 
			
		||||
func (cli *Client) ForgetRoom(roomID string) (resp *RespForgetRoom, err error) {
 | 
			
		||||
	u := cli.BuildURL("rooms", roomID, "forget")
 | 
			
		||||
	_, err = cli.MakeRequest("POST", u, struct{}{}, &resp)
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// InviteUser invites a user to a room. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-invite
 | 
			
		||||
func (cli *Client) InviteUser(roomID string, req *ReqInviteUser) (resp *RespInviteUser, err error) {
 | 
			
		||||
	u := cli.BuildURL("rooms", roomID, "invite")
 | 
			
		||||
	_, err = cli.MakeRequest("POST", u, struct{}{}, &resp)
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// InviteUserByThirdParty invites a third-party identifier to a room. See http://matrix.org/docs/spec/client_server/r0.2.0.html#invite-by-third-party-id-endpoint
 | 
			
		||||
func (cli *Client) InviteUserByThirdParty(roomID string, req *ReqInvite3PID) (resp *RespInviteUser, err error) {
 | 
			
		||||
	u := cli.BuildURL("rooms", roomID, "invite")
 | 
			
		||||
	_, err = cli.MakeRequest("POST", u, req, &resp)
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// KickUser kicks a user from a room. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-kick
 | 
			
		||||
func (cli *Client) KickUser(roomID string, req *ReqKickUser) (resp *RespKickUser, err error) {
 | 
			
		||||
	u := cli.BuildURL("rooms", roomID, "kick")
 | 
			
		||||
	_, err = cli.MakeRequest("POST", u, req, &resp)
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BanUser bans a user from a room. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-ban
 | 
			
		||||
func (cli *Client) BanUser(roomID string, req *ReqBanUser) (resp *RespBanUser, err error) {
 | 
			
		||||
	u := cli.BuildURL("rooms", roomID, "ban")
 | 
			
		||||
	_, err = cli.MakeRequest("POST", u, req, &resp)
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UnbanUser unbans a user from a room. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-unban
 | 
			
		||||
func (cli *Client) UnbanUser(roomID string, req *ReqUnbanUser) (resp *RespUnbanUser, err error) {
 | 
			
		||||
	u := cli.BuildURL("rooms", roomID, "unban")
 | 
			
		||||
	_, err = cli.MakeRequest("POST", u, req, &resp)
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// StateEvent gets a single state event in a room. It will attempt to JSON unmarshal into the given "outContent" struct with
 | 
			
		||||
// the HTTP response body, or return an error.
 | 
			
		||||
// See http://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-rooms-roomid-state-eventtype-statekey
 | 
			
		||||
| 
						 | 
				
			
			@ -473,6 +538,10 @@ func (cli *Client) UploadToContentRepo(content io.Reader, contentType string, co
 | 
			
		|||
	return &m, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func txnID() string {
 | 
			
		||||
	return "go" + strconv.FormatInt(time.Now().UnixNano(), 10)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewClient creates a new Matrix Client ready for syncing
 | 
			
		||||
func NewClient(homeserverURL, userID, accessToken string) (*Client, error) {
 | 
			
		||||
	hsURL, err := url.Parse(homeserverURL)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										27
									
								
								requests.go
									
										
									
									
									
								
							
							
						
						
									
										27
									
								
								requests.go
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -36,6 +36,11 @@ type ReqCreateRoom struct {
 | 
			
		|||
	IsDirect        bool                   `json:"is_direct,omitempty"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ReqRedact is the JSON request for http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-redact-eventid-txnid
 | 
			
		||||
type ReqRedact struct {
 | 
			
		||||
	Reason string `json:"reason,omitempty"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ReqInvite3PID is the JSON request for https://matrix.org/docs/spec/client_server/r0.2.0.html#id57
 | 
			
		||||
// It is also a JSON object used in https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom
 | 
			
		||||
type ReqInvite3PID struct {
 | 
			
		||||
| 
						 | 
				
			
			@ -43,3 +48,25 @@ type ReqInvite3PID struct {
 | 
			
		|||
	Medium   string `json:"medium"`
 | 
			
		||||
	Address  string `json:"address"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ReqInviteUser is the JSON request for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-invite
 | 
			
		||||
type ReqInviteUser struct {
 | 
			
		||||
	UserID string `json:"user_id"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ReqKickUser is the JSON request for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-kick
 | 
			
		||||
type ReqKickUser struct {
 | 
			
		||||
	Reason string `json:"reason,omitempty"`
 | 
			
		||||
	UserID string `json:"user_id"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ReqBanUser is the JSON request for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-ban
 | 
			
		||||
type ReqBanUser struct {
 | 
			
		||||
	Reason string `json:"reason,omitempty"`
 | 
			
		||||
	UserID string `json:"user_id"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ReqUnbanUser is the JSON request for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-unban
 | 
			
		||||
type ReqUnbanUser struct {
 | 
			
		||||
	UserID string `json:"user_id"`
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										20
									
								
								responses.go
									
										
									
									
									
								
							
							
						
						
									
										20
									
								
								responses.go
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -17,6 +17,11 @@ type RespCreateFilter struct {
 | 
			
		|||
	FilterID string `json:"filter_id"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RespVersions is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-versions
 | 
			
		||||
type RespVersions struct {
 | 
			
		||||
	Versions []string `json:"versions"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RespJoinRoom is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-join
 | 
			
		||||
type RespJoinRoom struct {
 | 
			
		||||
	RoomID string `json:"room_id"`
 | 
			
		||||
| 
						 | 
				
			
			@ -25,6 +30,21 @@ type RespJoinRoom struct {
 | 
			
		|||
// RespLeaveRoom is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-leave
 | 
			
		||||
type RespLeaveRoom struct{}
 | 
			
		||||
 | 
			
		||||
// RespForgetRoom is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-forget
 | 
			
		||||
type RespForgetRoom struct{}
 | 
			
		||||
 | 
			
		||||
// RespInviteUser is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-invite
 | 
			
		||||
type RespInviteUser struct{}
 | 
			
		||||
 | 
			
		||||
// RespKickUser is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-kick
 | 
			
		||||
type RespKickUser struct{}
 | 
			
		||||
 | 
			
		||||
// RespBanUser is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-ban
 | 
			
		||||
type RespBanUser struct{}
 | 
			
		||||
 | 
			
		||||
// RespUnbanUser is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-unban
 | 
			
		||||
type RespUnbanUser struct{}
 | 
			
		||||
 | 
			
		||||
// RespSendEvent is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid
 | 
			
		||||
type RespSendEvent struct {
 | 
			
		||||
	EventID string `json:"event_id"`
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue