From 712061a445f461eeb90d9a1e289132f196c0cb27 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 5 Jan 2017 12:58:55 +0000 Subject: [PATCH 1/5] Add /versions endpoint --- client.go | 7 +++++++ responses.go | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/client.go b/client.go index c3a4d4e..f27bb1c 100644 --- a/client.go +++ b/client.go @@ -350,6 +350,13 @@ func (cli *Client) Login(req *ReqLogin, setOnClient bool) (resp *RespLogin, err 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 diff --git a/responses.go b/responses.go index 1cfd4e0..0997786 100644 --- a/responses.go +++ b/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"` From 52bfdcb6fa3b4946f1dd8b5f8aea32bcc4265c7e Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 5 Jan 2017 14:25:05 +0000 Subject: [PATCH 2/5] Add PUT /room/roomId/state/type/key endpoint --- client.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/client.go b/client.go index c3a4d4e..acf8804 100644 --- a/client.go +++ b/client.go @@ -386,6 +386,14 @@ func (cli *Client) SendMessageEvent(roomID string, eventType string, contentJSON 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) { From 30c70355223452057c8528028e63afb90d6610af Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 5 Jan 2017 14:33:38 +0000 Subject: [PATCH 3/5] Add /redact endpoint --- client.go | 8 ++++++++ requests.go | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/client.go b/client.go index c3a4d4e..71d50c2 100644 --- a/client.go +++ b/client.go @@ -393,6 +393,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 := "go" + strconv.FormatInt(time.Now().UnixNano(), 10) + 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", diff --git a/requests.go b/requests.go index d26cf9d..a2a960e 100644 --- a/requests.go +++ b/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 { From 490a10cbc3889ce7db130a537791640ef6a3c1ab Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 5 Jan 2017 15:23:37 +0000 Subject: [PATCH 4/5] Add room membership APIs --- client.go | 42 ++++++++++++++++++++++++++++++++++++++++++ requests.go | 22 ++++++++++++++++++++++ responses.go | 15 +++++++++++++++ 3 files changed, 79 insertions(+) diff --git a/client.go b/client.go index c3a4d4e..99d5168 100644 --- a/client.go +++ b/client.go @@ -411,6 +411,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 diff --git a/requests.go b/requests.go index d26cf9d..7a9d4ac 100644 --- a/requests.go +++ b/requests.go @@ -43,3 +43,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"` +} diff --git a/responses.go b/responses.go index 1cfd4e0..7455703 100644 --- a/responses.go +++ b/responses.go @@ -25,6 +25,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"` From 24cbc75ecc0bce0804422e9a184f1b4492a74d5b Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 5 Jan 2017 17:47:24 +0000 Subject: [PATCH 5/5] Factor out txn ID generation --- client.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 71d50c2..c81812b 100644 --- a/client.go +++ b/client.go @@ -380,7 +380,7 @@ 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 @@ -395,7 +395,7 @@ func (cli *Client) SendText(roomID, text string) (*RespSendEvent, error) { // 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 := "go" + strconv.FormatInt(time.Now().UnixNano(), 10) + txnID := txnID() urlPath := cli.BuildURL("rooms", roomID, "redact", eventID, txnID) _, err = cli.MakeRequest("PUT", urlPath, req, &resp) return @@ -469,6 +469,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)