From 30c70355223452057c8528028e63afb90d6610af Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 5 Jan 2017 14:33:38 +0000 Subject: [PATCH 1/2] 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 24cbc75ecc0bce0804422e9a184f1b4492a74d5b Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 5 Jan 2017 17:47:24 +0000 Subject: [PATCH 2/2] 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)