From d3f257a3a3399361a0bb48cbfa1b1cfcba447e61 Mon Sep 17 00:00:00 2001 From: multi prise Date: Sat, 26 Dec 2020 22:46:17 +0000 Subject: wrote functions corrected the tests --- .../client/endpoint/post_rooms_read_markers.ex | 68 ++++++++++++++++++++++ lib/polyjuice/client/room.ex | 28 +++++++++ .../endpoint/post_rooms_read_markers_test.exs | 55 +++++++++++++++++ test/polyjuice/client/room_test.exs | 21 +++++++ 4 files changed, 172 insertions(+) create mode 100644 lib/polyjuice/client/endpoint/post_rooms_read_markers.ex create mode 100644 test/polyjuice/client/endpoint/post_rooms_read_markers_test.exs diff --git a/lib/polyjuice/client/endpoint/post_rooms_read_markers.ex b/lib/polyjuice/client/endpoint/post_rooms_read_markers.ex new file mode 100644 index 0000000..420e293 --- /dev/null +++ b/lib/polyjuice/client/endpoint/post_rooms_read_markers.ex @@ -0,0 +1,68 @@ +# Copyright 2020 Multi Prise +# +# 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.PostRoomsReadMarkers do + @moduledoc """ + Sets the position of the read marker for a given room, and optionally the read receipt's location. + https://matrix.org/docs/spec/client_server/r0.5.0#post-matrix-client-r0-rooms-roomid-read-markers + """ + + @type t :: %__MODULE__{ + room: String.t(), + fully_read: String.t(), + read: String.t() | nil + } + + @enforce_keys [:room, :fully_read] + + defstruct [ + :room, + :fully_read, + :read + ] + + defimpl Polyjuice.Client.Endpoint.Proto do + def http_spec(%Polyjuice.Client.Endpoint.PostRoomsReadMarkers{ + room: room, + fully_read: fully_read, + read: read + }) do + e = &URI.encode_www_form/1 + + query = + case read do + nil -> %{"m.fully_read": fully_read} + _ -> %{"m.fully_read": fully_read, "m.read": read} + end + + {:ok, body} = query |> Jason.encode() + + Polyjuice.Client.Endpoint.HttpSpec.post( + :r0, + "rooms/#{e.(room)}/read_markers", + body: 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, _body) do + :ok + end + end +end diff --git a/lib/polyjuice/client/room.ex b/lib/polyjuice/client/room.ex index 62009f9..7435244 100644 --- a/lib/polyjuice/client/room.ex +++ b/lib/polyjuice/client/room.ex @@ -334,4 +334,32 @@ defmodule Polyjuice.Client.Room do } ) end + + @doc """ + Set up the read receipt marker positions for a given room + The history for a given room may be split into three sections: + messages the user has read (or indicated they aren't interested in them), + messages the user might have read some but not others, and messages the user hasn't seen yet. + The "fully read marker" (also known as a "read marker") marks the last event of the first section, + whereas the user's read receipt marks the last event of the second section. + it takes: + `fully_read`: the event id the read marker should be located at + `read`: the event id the to which the read receipt should be be up to + """ + @spec update_read_markers( + client_api :: Polyjuice.Client.API.t(), + room :: String.t(), + fully_read :: String.t(), + read :: String.t() | nil + ) :: {:ok} | any + def update_read_markers(client_api, room, fully_read, read \\ nil) do + Polyjuice.Client.API.call( + client_api, + %Polyjuice.Client.Endpoint.PostRoomsReadMarkers{ + room: room, + fully_read: fully_read, + read: read + } + ) + end end diff --git a/test/polyjuice/client/endpoint/post_rooms_read_markers_test.exs b/test/polyjuice/client/endpoint/post_rooms_read_markers_test.exs new file mode 100644 index 0000000..5014342 --- /dev/null +++ b/test/polyjuice/client/endpoint/post_rooms_read_markers_test.exs @@ -0,0 +1,55 @@ +# Copyright 2020 Multi Prise +# +# 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.PostRoomsReadMarkersTest do + use ExUnit.Case + + test "POST rooms/{room_id}/read_markers" do + endpoint = %Polyjuice.Client.Endpoint.PostRoomsReadMarkers{ + room: "!room_id", + fully_read: "$somewhere:example.org", + read: "$elsewhere:example.org" + } + + http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint) + + assert http_spec == %Polyjuice.Client.Endpoint.HttpSpec{ + auth_required: true, + body: + "{\"m.fully_read\":\"$somewhere:example.org\",\"m.read\":\"$elsewhere:example.org\"}", + headers: [ + {"Accept", "application/json"}, + {"Accept-Encoding", "gzip, deflate"}, + {"Content-Type", "application/json"} + ], + method: :post, + path: "_matrix/client/r0/rooms/%21room_id/read_markers" + } + + 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 a1e0dfa..349ac54 100644 --- a/test/polyjuice/client/room_test.exs +++ b/test/polyjuice/client/room_test.exs @@ -216,6 +216,27 @@ defmodule Polyjuice.Client.RoomTest do end end + test "update read markers" do + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.PostRoomsReadMarkers{ + room: "!room", + fully_read: "dede:local.fr", + read: "toto:local.fr" + }, + :ok + } + } do + :ok = + Polyjuice.Client.Room.update_read_markers( + client, + "!room", + "dede:local.fr", + "toto:local.fr" + ) + end + end + test "join room" do with client = %DummyClient{ response: { -- cgit v1.2.3