summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/polyjuice/client.ex13
-rw-r--r--lib/polyjuice/client/endpoint/post_join.ex28
-rw-r--r--lib/polyjuice/client/sync.ex64
-rw-r--r--test/polyjuice/client/endpoint/post_join_test.exs57
4 files changed, 87 insertions, 75 deletions
diff --git a/lib/polyjuice/client.ex b/lib/polyjuice/client.ex
index b737d93..0c60241 100644
--- a/lib/polyjuice/client.ex
+++ b/lib/polyjuice/client.ex
@@ -223,13 +223,14 @@ defmodule Polyjuice.Client do
Join a room.
"""
@spec join_room(
- client_api :: Polyjuice.Client.API.t(),
- room :: String.t(),
- servers :: list(String.t()),
- third_party_join :: map | nil
- ) :: Any
+ client_api :: Polyjuice.Client.API.t(),
+ room :: String.t(),
+ servers :: list(String.t()),
+ third_party_join :: map | nil
+ ) :: Any
def join_room(client_api, room, servers \\ [], third_party_signed \\ nil)
- when is_binary(room) and is_list(servers) and (is_map(third_party_signed) or third_party_signed == nil) do
+ when is_binary(room) and is_list(servers) and
+ (is_map(third_party_signed) or third_party_signed == nil) do
Polyjuice.Client.API.call(
client_api,
%Polyjuice.Client.Endpoint.PostJoin{
diff --git a/lib/polyjuice/client/endpoint/post_join.ex b/lib/polyjuice/client/endpoint/post_join.ex
index 8fb742c..66007cf 100644
--- a/lib/polyjuice/client/endpoint/post_join.ex
+++ b/lib/polyjuice/client/endpoint/post_join.ex
@@ -42,23 +42,27 @@ defmodule Polyjuice.Client.Endpoint.PostJoin do
base_url
) do
e = &URI.encode_www_form/1
- body = Poison.encode!(
- if third_party_signed do
- %{"third_party_signed" => third_party_signed}
- else
- %{}
- end
- )
+
+ body =
+ Poison.encode!(
+ if third_party_signed do
+ %{"third_party_signed" => third_party_signed}
+ else
+ %{}
+ end
+ )
url = %{
URI.merge(
base_url,
"#{Polyjuice.Client.prefix_r0()}/join/#{e.(room)}"
- ) | query: if servers == [] do
- nil
- else
- Enum.join(Enum.map(servers, &("server_name=#{e.(&1)}")), "&")
- end
+ )
+ | query:
+ if servers == [] do
+ nil
+ else
+ Enum.join(Enum.map(servers, &"server_name=#{e.(&1)}"), "&")
+ end
}
%Polyjuice.Client.Endpoint.HttpSpec{
diff --git a/lib/polyjuice/client/sync.ex b/lib/polyjuice/client/sync.ex
index 1f4c2f8..fe4dd1a 100644
--- a/lib/polyjuice/client/sync.ex
+++ b/lib/polyjuice/client/sync.ex
@@ -40,7 +40,7 @@ defmodule Polyjuice.Client.Sync do
query_params: "",
backoff: nil,
set_filter: nil,
- initial_done: false,
+ initial_done: false
]
@sync_path "_matrix/client/r0/sync"
@@ -165,11 +165,15 @@ defmodule Polyjuice.Client.Sync do
case status_code do
200 ->
{:ok, body} = :hackney.body(client_ref)
+
with {:ok, %{} = json_body} <- Poison.decode(body),
filter_id = Map.get(json_body, "filter_id") do
Logger.debug("got filter id #{filter_id}")
+
Polyjuice.Client.Storage.set_filter_id(
- state.storage, state.set_filter, filter_id
+ state.storage,
+ state.set_filter,
+ filter_id
)
do_sync(%{
@@ -228,14 +232,17 @@ defmodule Polyjuice.Client.Sync do
case status_code do
200 ->
{:ok, body} = :hackney.body(client_ref)
+
with {:ok, json_body} <- Poison.decode(body),
%{"next_batch" => next_batch} <- json_body do
if state.backoff, do: Logger.info("Sync resumed")
process_body(json_body, state)
Polyjuice.Client.Storage.set_sync_token(state.storage, next_batch)
+
if not state.initial_done do
send(state.listener, {:initial_sync_completed})
end
+
do_sync(%{state | since: next_batch, backoff: nil, initial_done: true})
else
_ ->
@@ -284,12 +291,10 @@ defmodule Polyjuice.Client.Sync do
rooms
|> Map.get("leave", [])
- |> Enum.each(
- fn {k, v} ->
- process_room(k, v, state)
- send(state.listener, {:left, k})
- end
- )
+ |> Enum.each(fn {k, v} ->
+ process_room(k, v, state)
+ send(state.listener, {:left, k})
+ end)
end
defp process_room(roomname, room, state) do
@@ -346,29 +351,32 @@ defmodule Polyjuice.Client.Sync do
defp process_invite(roomname, room, state) do
Logger.debug("invite received #{inspect(room)}")
# The invite state is a map from state type to state key to event.
- invite_state = Enum.reduce(
- Map.get(room, "invite_state", %{}) |> Map.get("events", []),
- %{},
- fn
- %{
- "type" => type,
- "state_key" => state_key
- } = val, acc ->
- Map.get(acc, type, %{})
- |> Map.put(state_key, val)
- |> (&Map.put(acc, type, &1)).()
-
- _, acc ->
- acc
- end
- )
+ invite_state =
+ Enum.reduce(
+ Map.get(room, "invite_state", %{}) |> Map.get("events", []),
+ %{},
+ fn
+ %{
+ "type" => type,
+ "state_key" => state_key
+ } = val,
+ acc ->
+ Map.get(acc, type, %{})
+ |> Map.put(state_key, val)
+ |> (&Map.put(acc, type, &1)).()
+
+ _, acc ->
+ acc
+ end
+ )
Logger.debug("state #{inspect(invite_state)}")
- inviter = invite_state
- |> Map.get("m.room.member", %{})
- |> Map.get(state.user_id, %{})
- |> Map.get("sender")
+ inviter =
+ invite_state
+ |> Map.get("m.room.member", %{})
+ |> Map.get(state.user_id, %{})
+ |> Map.get("sender")
if inviter do
send(
diff --git a/test/polyjuice/client/endpoint/post_join_test.exs b/test/polyjuice/client/endpoint/post_join_test.exs
index 71010bf..ae69d8c 100644
--- a/test/polyjuice/client/endpoint/post_join_test.exs
+++ b/test/polyjuice/client/endpoint/post_join_test.exs
@@ -17,19 +17,18 @@ defmodule Polyjuice.Client.Endpoint.PostJoinTest do
test "POST join/{roomIdOrAlias}" do
with endpoint = %Polyjuice.Client.Endpoint.PostJoin{
- room: "!room",
- third_party_signed: %{
- "sender" => "@alice:example.org",
- "mxid" => "@bob:example.org",
- "token" => "random8nonce",
- "signatures" => %{
- "example.org" => %{
- "ed25519:0" => "some9signature"
- }
- }
- }
- } do
-
+ room: "!room",
+ third_party_signed: %{
+ "sender" => "@alice:example.org",
+ "mxid" => "@bob:example.org",
+ "token" => "random8nonce",
+ "signatures" => %{
+ "example.org" => %{
+ "ed25519:0" => "some9signature"
+ }
+ }
+ }
+ } do
http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint, "https://example.com")
assert %{http_spec | transform: nil, body: nil} == %Polyjuice.Client.Endpoint.HttpSpec{
@@ -45,17 +44,17 @@ defmodule Polyjuice.Client.Endpoint.PostJoinTest do
}
assert Poison.decode!(http_spec.body) == %{
- "third_party_signed" => %{
- "sender" => "@alice:example.org",
- "mxid" => "@bob:example.org",
- "token" => "random8nonce",
- "signatures" => %{
- "example.org" => %{
- "ed25519:0" => "some9signature"
- }
- }
- }
- }
+ "third_party_signed" => %{
+ "sender" => "@alice:example.org",
+ "mxid" => "@bob:example.org",
+ "token" => "random8nonce",
+ "signatures" => %{
+ "example.org" => %{
+ "ed25519:0" => "some9signature"
+ }
+ }
+ }
+ }
assert http_spec.transform.(200, [], ~s({"room_id":"!room"})) == {:ok, "!room"}
@@ -63,10 +62,9 @@ defmodule Polyjuice.Client.Endpoint.PostJoinTest do
end
with endpoint = %Polyjuice.Client.Endpoint.PostJoin{
- room: "!room",
- servers: ["example.com", "example.org"]
- } do
-
+ room: "!room",
+ servers: ["example.com", "example.org"]
+ } do
http_spec = Polyjuice.Client.Endpoint.Proto.http_spec(endpoint, "https://example.com")
assert %{http_spec | transform: nil} == %Polyjuice.Client.Endpoint.HttpSpec{
@@ -78,7 +76,8 @@ defmodule Polyjuice.Client.Endpoint.PostJoinTest do
],
method: :post,
transform: nil,
- url: "https://example.com/_matrix/client/r0/join/%21room?server_name=example.com&server_name=example.org"
+ url:
+ "https://example.com/_matrix/client/r0/join/%21room?server_name=example.com&server_name=example.org"
}
assert http_spec.transform.(200, [], ~s({"room_id":"!room"})) == {:ok, "!room"}