From 9c5753065f6eadacd11fefc577d91c06718ab337 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sat, 21 Dec 2019 22:26:19 +0000 Subject: [PATCH 1/7] Add methods to query /publicRooms API --- client.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ responses.go | 20 ++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/client.go b/client.go index fc290ed..acd33df 100644 --- a/client.go +++ b/client.go @@ -372,6 +372,52 @@ func (cli *Client) Versions() (resp *RespVersions, err error) { return } +// PublicRooms returns the list of public rooms on target server. Does not require Auth. See http://matrix.org/speculator/spec/HEAD/client_server/unstable.html#get-matrix-client-unstable-publicrooms +func (cli *Client) PublicRooms(limit int, since string, server string) (resp *RespPublicRooms, err error) { + args := map[string]string{} + + if limit != 0 { + args["limit"] = strconv.Itoa(limit) + } + if since != "" { + args["since"] = since + } + if server != "" { + args["server"] = server + } + + urlPath := cli.BuildURLWithQuery([]string{"publicRooms"}, args) + err = cli.MakeRequest("GET", urlPath, nil, &resp) + return +} + +// PublicRoomsFiltered returns a subset of PublicRooms filtered server side. See http://matrix.org/speculator/spec/HEAD/client_server/unstable.html#post-matrix-client-unstable-publicrooms +func (cli *Client) PublicRoomsFiltered(limit int, since string, server string, filter string) (resp *RespPublicRooms, err error) { + content := map[string]string{} + + if limit != 0 { + content["limit"] = strconv.Itoa(limit) + } + if since != "" { + content["since"] = since + } + if filter != "" { + content["filter"] = filter + } + + var urlPath string + if server == "" { + urlPath = cli.BuildURL("publicRooms") + } else { + urlPath = cli.BuildURLWithQuery([]string{"publicRooms"}, map[string]string{ + "server": server, + }) + } + + err = cli.MakeRequest("POST", urlPath, content, &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 7a3a4ce..1c1fd92 100644 --- a/responses.go +++ b/responses.go @@ -22,6 +22,26 @@ type RespVersions struct { Versions []string `json:"versions"` } +// RespPublicRooms is the JSON response for http://matrix.org/speculator/spec/HEAD/client_server/unstable.html#get-matrix-client-unstable-publicrooms +type RespPublicRooms struct { + TotalRoomCountEstimate int `json:"total_room_count_estimate"` + PrevBatch string `json:"prev_batch"` + NextBatch string `json:"next_batch"` + Chunk []PublicRoomsChunk `json:"chunk"` +} + +type PublicRoomsChunk struct { + CanonicalAlias string `json:"canonical_alias"` + Name string `json:"name"` + WorldReadable bool `json:"world_readable"` + Topic string `json:"topic"` + NumJoinedMembers int `json:"num_joined_members"` + AvatarUrl string `json:"avatar_url"` + RoomID string `json:"room_id"` + GuestCanJoin bool `json:"guest_can_join"` + Aliases []string `json:"aliases"` +} + // 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 48858059f1a63a246ca35a17ae8d51dd473a37bd Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sat, 21 Dec 2019 22:33:03 +0000 Subject: [PATCH 2/7] delint --- responses.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/responses.go b/responses.go index 1c1fd92..ec88ed9 100644 --- a/responses.go +++ b/responses.go @@ -24,19 +24,20 @@ type RespVersions struct { // RespPublicRooms is the JSON response for http://matrix.org/speculator/spec/HEAD/client_server/unstable.html#get-matrix-client-unstable-publicrooms type RespPublicRooms struct { - TotalRoomCountEstimate int `json:"total_room_count_estimate"` - PrevBatch string `json:"prev_batch"` - NextBatch string `json:"next_batch"` - Chunk []PublicRoomsChunk `json:"chunk"` + TotalRoomCountEstimate int `json:"total_room_count_estimate"` + PrevBatch string `json:"prev_batch"` + NextBatch string `json:"next_batch"` + Chunk []PublicRoom `json:"chunk"` } -type PublicRoomsChunk struct { +// PublicRooms is a part of RespPublicRooms representing the disclosed information regarding a public room +type PublicRoom struct { CanonicalAlias string `json:"canonical_alias"` Name string `json:"name"` WorldReadable bool `json:"world_readable"` Topic string `json:"topic"` NumJoinedMembers int `json:"num_joined_members"` - AvatarUrl string `json:"avatar_url"` + AvatarURL string `json:"avatar_url"` RoomID string `json:"room_id"` GuestCanJoin bool `json:"guest_can_join"` Aliases []string `json:"aliases"` From 5f2b2fc8434f60dc0d68caefb3004250b83d3ff6 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sat, 21 Dec 2019 22:35:11 +0000 Subject: [PATCH 3/7] rejig --- responses.go | 13 ------------- room.go | 13 +++++++++++++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/responses.go b/responses.go index ec88ed9..effb609 100644 --- a/responses.go +++ b/responses.go @@ -30,19 +30,6 @@ type RespPublicRooms struct { Chunk []PublicRoom `json:"chunk"` } -// PublicRooms is a part of RespPublicRooms representing the disclosed information regarding a public room -type PublicRoom struct { - CanonicalAlias string `json:"canonical_alias"` - Name string `json:"name"` - WorldReadable bool `json:"world_readable"` - Topic string `json:"topic"` - NumJoinedMembers int `json:"num_joined_members"` - AvatarURL string `json:"avatar_url"` - RoomID string `json:"room_id"` - GuestCanJoin bool `json:"guest_can_join"` - Aliases []string `json:"aliases"` -} - // 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"` diff --git a/room.go b/room.go index c9b2351..d1d2286 100644 --- a/room.go +++ b/room.go @@ -6,6 +6,19 @@ type Room struct { State map[string]map[string]*Event } +// PublicRoom represents the information about a public room obtainable from the room directory +type PublicRoom struct { + CanonicalAlias string `json:"canonical_alias"` + Name string `json:"name"` + WorldReadable bool `json:"world_readable"` + Topic string `json:"topic"` + NumJoinedMembers int `json:"num_joined_members"` + AvatarURL string `json:"avatar_url"` + RoomID string `json:"room_id"` + GuestCanJoin bool `json:"guest_can_join"` + Aliases []string `json:"aliases"` +} + // UpdateState updates the room's current state with the given Event. This will clobber events based // on the type/state_key combination. func (room Room) UpdateState(event *Event) { From 20d5d55bc2bafb24522a596aaf85c62b1173be39 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sat, 21 Dec 2019 22:48:51 +0000 Subject: [PATCH 4/7] Add test --- client_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/client_test.go b/client_test.go index 9b7d2da..b975873 100644 --- a/client_test.go +++ b/client_test.go @@ -84,6 +84,55 @@ func TestClient_StateEvent(t *testing.T) { } } +func TestClient_PublicRooms(t *testing.T) { + cli := mockClient(func(req *http.Request) (*http.Response, error) { + if req.Method == "GET" && req.URL.Path == "/_matrix/client/r0/publicRooms" { + return &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewBufferString(`{ + "chunk": [ + { + "aliases": [ + "#murrays:cheese.bar" + ], + "avatar_url": "mxc://bleeker.street/CHEDDARandBRIE", + "guest_can_join": false, + "name": "CHEESE", + "num_joined_members": 37, + "room_id": "!ol19s:bleecker.street", + "topic": "Tasty tasty cheese", + "world_readable": true + } + ], + "next_batch": "p190q", + "prev_batch": "p1902", + "total_room_count_estimate": 115 +}`)), + }, nil + } + + return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path) + }) + + publicRooms, err := cli.PublicRooms(0, "", "") + + if err != nil { + t.Fatalf("PublicRooms: error, got %s", err.Error()) + } + if publicRooms.TotalRoomCountEstimate != 115 { + t.Fatalf("PublicRooms: got %d, want %d", publicRooms.TotalRoomCountEstimate, 115) + } + if len(publicRooms.Chunk) != 1 { + t.Fatalf("PublicRooms: got %d, want %d", len(publicRooms.Chunk), 1) + } + if publicRooms.Chunk[0].Name != "CHEESE" { + t.Fatalf("PublicRooms: got %s, want %s", publicRooms.Chunk[0].Name, "CHEESE") + } + if publicRooms.Chunk[0].NumJoinedMembers != 37 { + t.Fatalf("PublicRooms: got %d, want %d", publicRooms.Chunk[0].NumJoinedMembers, 37) + } +} + func mockClient(fn func(*http.Request) (*http.Response, error)) *Client { mrt := MockRoundTripper{ RT: fn, From dc3a2baaffd0fd3e36b6bc07eaf7d8ac5f64f9fb Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sat, 21 Dec 2019 22:52:54 +0000 Subject: [PATCH 5/7] Add go.mod --- go.mod | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 go.mod diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..18e69bf --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/matrix-org/gomatrix + +go 1.12 From fa4abd0af90ca7e72eb1394e47128aedaca36307 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sun, 22 Dec 2019 18:20:50 +0000 Subject: [PATCH 6/7] unrelated: Add PrevContent --- events.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/events.go b/events.go index edf231a..e82a6a3 100644 --- a/events.go +++ b/events.go @@ -7,15 +7,16 @@ import ( // Event represents a single Matrix event. type Event struct { - StateKey *string `json:"state_key,omitempty"` // The state key for the event. Only present on State Events. - Sender string `json:"sender"` // The user ID of the sender of the event - Type string `json:"type"` // The event type - Timestamp int64 `json:"origin_server_ts"` // The unix timestamp when this message was sent by the origin server - ID string `json:"event_id"` // The unique ID of this event - RoomID string `json:"room_id"` // The room the event was sent to. May be nil (e.g. for presence) - Content map[string]interface{} `json:"content"` // The JSON content of the event. - Redacts string `json:"redacts,omitempty"` // The event ID that was redacted if a m.room.redaction event - Unsigned map[string]interface{} `json:"unsigned"` // The unsigned portions of the event, such as age and prev_content + StateKey *string `json:"state_key,omitempty"` // The state key for the event. Only present on State Events. + Sender string `json:"sender"` // The user ID of the sender of the event + Type string `json:"type"` // The event type + Timestamp int64 `json:"origin_server_ts"` // The unix timestamp when this message was sent by the origin server + ID string `json:"event_id"` // The unique ID of this event + RoomID string `json:"room_id"` // The room the event was sent to. May be nil (e.g. for presence) + Redacts string `json:"redacts,omitempty"` // The event ID that was redacted if a m.room.redaction event + Unsigned map[string]interface{} `json:"unsigned"` // The unsigned portions of the event, such as age and prev_content + Content map[string]interface{} `json:"content"` // The JSON content of the event. + PrevContent map[string]interface{} `json:"prev_content,omitempty"` // The JSON prev_content of the event. } // Body returns the value of the "body" key in the event content if it is From 49bed8e0e4317175fad3f10a32579089013cbbf2 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sat, 25 Jan 2020 11:55:05 +0000 Subject: [PATCH 7/7] update comments and remove go.mod --- client.go | 5 +++-- go.mod | 3 --- 2 files changed, 3 insertions(+), 5 deletions(-) delete mode 100644 go.mod diff --git a/client.go b/client.go index acd33df..7625a1c 100644 --- a/client.go +++ b/client.go @@ -372,7 +372,7 @@ func (cli *Client) Versions() (resp *RespVersions, err error) { return } -// PublicRooms returns the list of public rooms on target server. Does not require Auth. See http://matrix.org/speculator/spec/HEAD/client_server/unstable.html#get-matrix-client-unstable-publicrooms +// PublicRooms returns the list of public rooms on target server. See https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-unstable-publicrooms func (cli *Client) PublicRooms(limit int, since string, server string) (resp *RespPublicRooms, err error) { args := map[string]string{} @@ -391,7 +391,8 @@ func (cli *Client) PublicRooms(limit int, since string, server string) (resp *Re return } -// PublicRoomsFiltered returns a subset of PublicRooms filtered server side. See http://matrix.org/speculator/spec/HEAD/client_server/unstable.html#post-matrix-client-unstable-publicrooms +// PublicRoomsFiltered returns a subset of PublicRooms filtered server side. +// See https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-unstable-publicrooms func (cli *Client) PublicRoomsFiltered(limit int, since string, server string, filter string) (resp *RespPublicRooms, err error) { content := map[string]string{} diff --git a/go.mod b/go.mod deleted file mode 100644 index 18e69bf..0000000 --- a/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module github.com/matrix-org/gomatrix - -go 1.12