From 556e9529c056e08df59251d20796df7c9c62ae23 Mon Sep 17 00:00:00 2001 From: Dhole Date: Sat, 6 May 2017 21:21:07 -0700 Subject: [PATCH 1/2] Add /messages API --- client.go | 20 ++++++++++++++++++++ responses.go | 7 +++++++ 2 files changed, 27 insertions(+) diff --git a/client.go b/client.go index f916174..94dbd5c 100644 --- a/client.go +++ b/client.go @@ -603,6 +603,26 @@ func (cli *Client) JoinedRooms() (resp *RespJoinedRooms, err error) { return } +// Messages returns a list of message and state events for a room. It uses +// pagination query parameters to paginate history in the room. +// See https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-rooms-roomid-messages +func (cli *Client) Messages(roomID, from, to string, dir rune, limit int) (resp *RespMessages, err error) { + query := map[string]string{ + "from": from, + "dir": string(dir), + } + if to != "" { + query["to"] = to + } + if limit != 0 { + query["limit"] = string(limit) + } + + urlPath := cli.BuildURLWithQuery([]string{"rooms", roomID, "messages"}, query) + _, err = cli.MakeRequest("GET", urlPath, nil, &resp) + return +} + func txnID() string { return "go" + strconv.FormatInt(time.Now().UnixNano(), 10) } diff --git a/responses.go b/responses.go index 8a18c02..58191bc 100644 --- a/responses.go +++ b/responses.go @@ -61,6 +61,13 @@ type RespJoinedMembers struct { } `json:"joined"` } +// RespMessages is the JSON response for https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-rooms-roomid-messages +type RespMessages struct { + Start string `json:"start"` + Chunk []Event `json:"chunk"` + End string `json:"end"` +} + // 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 43492b703b03922efb040aca93d26830e278e059 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 8 May 2017 11:29:23 +0100 Subject: [PATCH 2/2] Add convenience function to send a room message of type send.notice --- client.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/client.go b/client.go index f916174..a671edc 100644 --- a/client.go +++ b/client.go @@ -450,6 +450,13 @@ func (cli *Client) SendText(roomID, text string) (*RespSendEvent, error) { TextMessage{"m.text", text}) } +// SendNotice sends an m.room.message event into the given room with a msgtype of m.notice +// See http://matrix.org/docs/spec/client_server/r0.2.0.html#m-notice +func (cli *Client) SendNotice(roomID, text string) (*RespSendEvent, error) { + return cli.SendMessageEvent(roomID, "m.room.message", + TextMessage{"m.notice", 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()