diff options
author | Hubert Chathi <hubert@uhoreg.ca> | 2020-09-21 19:07:54 -0400 |
---|---|---|
committer | Hubert Chathi <hubert@uhoreg.ca> | 2020-09-21 19:07:54 -0400 |
commit | 0cd322d20b34eb4141327daedb19e15fe2f07e83 (patch) | |
tree | c8f3a8ce585fad2c2e284b3b0c073b00e6add660 | |
parent | return :ok instead of {:ok} (diff) |
add functions for leaving/forgetting rooms
-rw-r--r-- | lib/polyjuice/client/endpoint/post_rooms_forget.ex | 52 | ||||
-rw-r--r-- | lib/polyjuice/client/endpoint/post_rooms_leave.ex | 52 | ||||
-rw-r--r-- | lib/polyjuice/client/room.ex | 38 | ||||
-rw-r--r-- | test/polyjuice/client/endpoint/post_rooms_forget_test.exs | 48 | ||||
-rw-r--r-- | test/polyjuice/client/endpoint/post_rooms_leave_test.exs | 48 | ||||
-rw-r--r-- | test/polyjuice/client/room_test.exs | 22 |
6 files changed, 259 insertions, 1 deletions
diff --git a/lib/polyjuice/client/endpoint/post_rooms_forget.ex b/lib/polyjuice/client/endpoint/post_rooms_forget.ex new file mode 100644 index 0000000..a5e00ee --- /dev/null +++ b/lib/polyjuice/client/endpoint/post_rooms_forget.ex @@ -0,0 +1,52 @@ +# 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.PostRoomsForget do + @moduledoc """ + Leave a room. + + https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-rooms-roomid-forget + """ + + @type t :: %__MODULE__{ + room: String.t() + } + + @enforce_keys [:room] + defstruct [ + :room + ] + + defimpl Polyjuice.Client.Endpoint.Proto do + def http_spec(%{room: room}) do + e = &URI.encode_www_form/1 + + Polyjuice.Client.Endpoint.HttpSpec.post( + :r0, + "rooms/#{e.(room)}/forget", + body: "{}" + ) + 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 + + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, _parsed) do + :ok + end + end +end diff --git a/lib/polyjuice/client/endpoint/post_rooms_leave.ex b/lib/polyjuice/client/endpoint/post_rooms_leave.ex new file mode 100644 index 0000000..510f291 --- /dev/null +++ b/lib/polyjuice/client/endpoint/post_rooms_leave.ex @@ -0,0 +1,52 @@ +# 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.PostRoomsLeave do + @moduledoc """ + Leave a room. + + https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-rooms-roomid-leave + """ + + @type t :: %__MODULE__{ + room: String.t() + } + + @enforce_keys [:room] + defstruct [ + :room + ] + + defimpl Polyjuice.Client.Endpoint.Proto do + def http_spec(%{room: room}) do + e = &URI.encode_www_form/1 + + Polyjuice.Client.Endpoint.HttpSpec.post( + :r0, + "rooms/#{e.(room)}/leave", + body: "{}" + ) + 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 + + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, _parsed) do + :ok + end + end +end diff --git a/lib/polyjuice/client/room.ex b/lib/polyjuice/client/room.ex index af43bb6..62009f9 100644 --- a/lib/polyjuice/client/room.ex +++ b/lib/polyjuice/client/room.ex @@ -1,4 +1,4 @@ -# Copyright 2019 Hubert Chathi <hubert@uhoreg.ca> +# 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. @@ -187,6 +187,42 @@ defmodule Polyjuice.Client.Room do end @doc """ + Leave a room. + """ + @spec leave( + client_api :: Polyjuice.Client.API.t(), + room :: String.t() + ) :: {:ok, String.t()} | any + def leave(client_api, room) when is_binary(room) do + Polyjuice.Client.API.room_queue(client_api, room, fn -> + Polyjuice.Client.API.call( + client_api, + %Polyjuice.Client.Endpoint.PostRoomsLeave{ + room: room + } + ) + end) + end + + @doc """ + Forget a room. + """ + @spec forget( + client_api :: Polyjuice.Client.API.t(), + room :: String.t() + ) :: {:ok, String.t()} | any + def forget(client_api, room) when is_binary(room) do + Polyjuice.Client.API.room_queue(client_api, room, fn -> + Polyjuice.Client.API.call( + client_api, + %Polyjuice.Client.Endpoint.PostRoomsForget{ + room: room + } + ) + end) + end + + @doc """ Get messages from a room starting from a certain point. """ @spec get_messages( diff --git a/test/polyjuice/client/endpoint/post_rooms_forget_test.exs b/test/polyjuice/client/endpoint/post_rooms_forget_test.exs new file mode 100644 index 0000000..a2ce841 --- /dev/null +++ b/test/polyjuice/client/endpoint/post_rooms_forget_test.exs @@ -0,0 +1,48 @@ +# 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.PostRoomsForgetTest do + use ExUnit.Case + + test "POST rooms/{roomId}/forget" do + endpoint = %Polyjuice.Client.Endpoint.PostRoomsForget{room: "!room"} + http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint) + + assert http_spec == %Polyjuice.Client.Endpoint.HttpSpec{ + auth_required: true, + body: "{}", + headers: [ + {"Accept", "application/json"}, + {"Content-Type", "application/json"} + ], + method: :post, + path: "_matrix/client/r0/rooms/%21room/forget" + } + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 200, + [{"Content-Type", "application/json"}], + "{}" + ) == :ok + + 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/endpoint/post_rooms_leave_test.exs b/test/polyjuice/client/endpoint/post_rooms_leave_test.exs new file mode 100644 index 0000000..d3448ef --- /dev/null +++ b/test/polyjuice/client/endpoint/post_rooms_leave_test.exs @@ -0,0 +1,48 @@ +# 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.PostRoomsLeaveTest do + use ExUnit.Case + + test "POST rooms/{roomId}/leave" do + endpoint = %Polyjuice.Client.Endpoint.PostRoomsLeave{room: "!room"} + http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint) + + assert http_spec == %Polyjuice.Client.Endpoint.HttpSpec{ + auth_required: true, + body: "{}", + headers: [ + {"Accept", "application/json"}, + {"Content-Type", "application/json"} + ], + method: :post, + path: "_matrix/client/r0/rooms/%21room/leave" + } + + assert Polyjuice.Client.Endpoint.Proto.transform_http_result( + endpoint, + 200, + [{"Content-Type", "application/json"}], + "{}" + ) == :ok + + 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 b45f607..a1e0dfa 100644 --- a/test/polyjuice/client/room_test.exs +++ b/test/polyjuice/client/room_test.exs @@ -244,6 +244,28 @@ defmodule Polyjuice.Client.RoomTest do end end + test "leave room" do + client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.PostRoomsLeave{room: "!room"}, + :ok + } + } + + :ok = Polyjuice.Client.Room.leave(client, "!room") + end + + test "forget room" do + client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.PostRoomsForget{room: "!room"}, + :ok + } + } + + :ok = Polyjuice.Client.Room.forget(client, "!room") + end + test "get messages" do with client = %DummyClient{ response: { |