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] 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"`