diff options
author | Hubert Chathi <hubert@uhoreg.ca> | 2020-06-02 22:34:56 +0000 |
---|---|---|
committer | Hubert Chathi <hubert@uhoreg.ca> | 2020-06-02 22:34:56 +0000 |
commit | c97623214f81dacba8ddf022c3deb48954cf78fe (patch) | |
tree | 7d63815f6046ec46045e81c05619934b14374312 | |
parent | add some more filtering methods and reduce duplicate code (diff) | |
parent | add endpoint for getting room state (diff) |
Merge branch 'get_room_state' into 'master'
Add endpoint for getting room state
See merge request uhoreg/polyjuice_client!1
-rw-r--r-- | lib/polyjuice/client/endpoint/get_rooms_state.ex | 72 | ||||
-rw-r--r-- | lib/polyjuice/client/room.ex | 25 | ||||
-rw-r--r-- | test/polyjuice/client/endpoint/get_rooms_state_test.exs | 87 | ||||
-rw-r--r-- | test/polyjuice/client/room_test.exs | 46 |
4 files changed, 230 insertions, 0 deletions
diff --git a/lib/polyjuice/client/endpoint/get_rooms_state.ex b/lib/polyjuice/client/endpoint/get_rooms_state.ex new file mode 100644 index 0000000..4d05dd5 --- /dev/null +++ b/lib/polyjuice/client/endpoint/get_rooms_state.ex @@ -0,0 +1,72 @@ +# Copyright 2020 Hubert Chathi <hubert@uhoreg.ca> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.Endpoint.GetRoomsState do + @moduledoc """ + Get a room state. + + https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-rooms-roomid-state-eventtype-statekey + https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-rooms-roomid-state + + """ + + @type t :: %__MODULE__{ + room: String.t(), + event_type: String.t(), + state_key: String.t() + } + + @enforce_keys [:room, :event_type, :state_key] + defstruct [ + :room, + :event_type, + :state_key + ] + + defimpl Polyjuice.Client.Endpoint.Proto do + def http_spec( + %Polyjuice.Client.Endpoint.GetRoomsState{ + room: room, + event_type: event_type, + state_key: state_key + }, + base_url + ) do + e = &URI.encode_www_form/1 + + path = + if event_type == nil do + "#{Polyjuice.Client.prefix_r0()}/rooms/#{e.(room)}/state" + else + "#{Polyjuice.Client.prefix_r0()}/rooms/#{e.(room)}/state/#{e.(event_type)}/#{ + e.(state_key) + }" + end + + url = URI.merge(base_url, path) + + %Polyjuice.Client.Endpoint.HttpSpec{ + method: :get, + headers: [ + {"Accept", "application/json"} + ], + url: to_string(url) + } + end + + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end +end diff --git a/lib/polyjuice/client/room.ex b/lib/polyjuice/client/room.ex index f640861..886a38e 100644 --- a/lib/polyjuice/client/room.ex +++ b/lib/polyjuice/client/room.ex @@ -265,4 +265,29 @@ defmodule Polyjuice.Client.Room do fn _ -> nil end ) end + + @doc """ + Get a room state. + If `event_type` is not provided, returns a list of events. + If `event_type` and `state_key` are provided, returns an event content. + `state_key` can be omitted but this will return events that have a blank state key, not events that have "any state key". + """ + @spec get_state( + client_api :: Polyjuice.Client.API.t(), + room :: String.t(), + event_type :: String.t(), + state_key :: String.t() + ) :: {:ok, String.t()} | Any + def get_state(client_api, room, event_type \\ nil, state_key \\ "") + when (is_nil(event_type) or is_binary(event_type)) and is_binary(state_key) and + is_binary(room) do + Polyjuice.Client.API.call( + client_api, + %Polyjuice.Client.Endpoint.GetRoomsState{ + room: room, + event_type: event_type, + state_key: state_key + } + ) + end end diff --git a/test/polyjuice/client/endpoint/get_rooms_state_test.exs b/test/polyjuice/client/endpoint/get_rooms_state_test.exs new file mode 100644 index 0000000..f6e0596 --- /dev/null +++ b/test/polyjuice/client/endpoint/get_rooms_state_test.exs @@ -0,0 +1,87 @@ +# Copyright 2020 Hubert Chathi <hubert@uhoreg.ca> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.Endpoint.GetRoomsStateTest do + use ExUnit.Case + + test "GET rooms/{room_id}/state" do + endpoint = %Polyjuice.Client.Endpoint.GetRoomsState{ + room: "!room_id", + event_type: nil, + state_key: "" + } + + http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint, "https://example.com") + + assert http_spec == %Polyjuice.Client.Endpoint.HttpSpec{ + auth_required: true, + body: "", + headers: [ + {"Accept", "application/json"} + ], + method: :get, + url: "https://example.com/_matrix/client/r0/rooms/%21room_id/state" + } + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 200, + [], + "[{\"type\": \"m.room.name\", \"content\": {\"name\": \"foo1\"}}]" + ) == {:ok, [%{"type" => "m.room.name", "content" => %{"name" => "foo1"}}]} + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 500, + [], + "Aaah!" + ) == + {:error, 500, %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} + end + + test "GET rooms/{room_id}/state/{event_type}/{state_key}" do + endpoint = %Polyjuice.Client.Endpoint.GetRoomsState{ + room: "!room_id", + event_type: "m.room.name", + state_key: "" + } + + http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint, "https://example.com") + + assert http_spec == %Polyjuice.Client.Endpoint.HttpSpec{ + auth_required: true, + body: "", + headers: [ + {"Accept", "application/json"} + ], + method: :get, + url: "https://example.com/_matrix/client/r0/rooms/%21room_id/state/m.room.name/" + } + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 200, + [], + "{\"name\": \"foo1\"}" + ) == {:ok, %{"name" => "foo1"}} + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 500, + [], + "Aaah!" + ) == + {:error, 500, %{"body" => "Aaah!", "errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE"}} + end +end diff --git a/test/polyjuice/client/room_test.exs b/test/polyjuice/client/room_test.exs index f5470bc..97180b0 100644 --- a/test/polyjuice/client/room_test.exs +++ b/test/polyjuice/client/room_test.exs @@ -154,6 +154,52 @@ defmodule Polyjuice.Client.RoomTest do end end + test "get all state events" do + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.GetRoomsState{ + room: "!bar", + event_type: nil, + state_key: "" + }, + {:ok, [%{"type" => "m.room.name", "content" => %{"name" => "foo1"}}]} + } + } do + {:ok, events_list} = + Polyjuice.Client.Room.get_state( + client, + "!bar", + nil, + "" + ) + + assert events_list == [%{"type" => "m.room.name", "content" => %{"name" => "foo1"}}] + end + end + + test "get one state event" do + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.GetRoomsState{ + room: "!bar", + event_type: "m.room.name", + state_key: "" + }, + {:ok, %{"name" => "foo1"}} + } + } do + {:ok, event_content} = + Polyjuice.Client.Room.get_state( + client, + "!bar", + "m.room.name", + "" + ) + + assert event_content == %{"name" => "foo1"} + end + end + test "update read receipt" do with client = %DummyClient{ response: { |