diff options
author | Arjan Scherpenisse <arjan@scherpenisse.net> | 2021-02-19 13:03:24 +0100 |
---|---|---|
committer | Arjan Scherpenisse <arjan@scherpenisse.net> | 2021-02-20 09:08:56 +0100 |
commit | a946555b69a584f74455e5124d4d2597bc5114c7 (patch) | |
tree | 92895ec3228fef5a6d80626fc9ccab7a16ec5b42 | |
parent | Move defmodule out of client test so each test can be run individually (diff) |
Handle ephemeral events (e.g. for typing indicator)
-rw-r--r-- | lib/polyjuice/client/sync.ex | 40 | ||||
-rw-r--r-- | test/polyjuice/client/sync_test.exs | 32 |
2 files changed, 47 insertions, 25 deletions
diff --git a/lib/polyjuice/client/sync.ex b/lib/polyjuice/client/sync.ex index c315e90..7b995b4 100644 --- a/lib/polyjuice/client/sync.ex +++ b/lib/polyjuice/client/sync.ex @@ -414,37 +414,31 @@ defmodule Polyjuice.Client.Sync do room |> Map.get("state", %{}) |> Map.get("events", []) - |> Enum.each(&process_event(&1, roomname, state)) + |> Enum.each(&process_event(&1, roomname, nil, state)) - timeline + room + |> Map.get("ephemeral", %{}) |> Map.get("events", []) - |> Enum.each(&process_event(&1, roomname, state)) - end + |> Enum.each(&process_event(&1, roomname, :ephemeral, state)) - defp process_event( - %{ - "state_key" => _state_key - } = event, - roomname, - state - ) do - Polyjuice.Client.Handler.handle(state.handler, :state, {roomname, event}) - - state + timeline + |> Map.get("events", []) + |> Enum.each(&process_event(&1, roomname, nil, state)) end - defp process_event( - %{} = event, - roomname, - state - ) do - Polyjuice.Client.Handler.handle(state.handler, :message, {roomname, event}) + defp process_event(%{} = event, roomname, type, state) do + type = + type || + case event do + %{"state_key" => _} -> :state + _ -> :message + end - state + Polyjuice.Client.Handler.handle(state.handler, type, {roomname, event}) end - defp process_event(_, _, state) do - state + defp process_event(_, _, _, _state) do + :ok end defp process_invite(roomname, room, state) do diff --git a/test/polyjuice/client/sync_test.exs b/test/polyjuice/client/sync_test.exs index 94387aa..5cf2f35 100644 --- a/test/polyjuice/client/sync_test.exs +++ b/test/polyjuice/client/sync_test.exs @@ -133,6 +133,27 @@ defmodule Polyjuice.Client.SyncTest do "rooms" => %{ "join" => %{ "!room_id" => %{ + "ephemeral" => %{ + "events" => [ + %{ + "content" => %{"user_ids" => ["@alice:example.org"]}, + "type" => "m.typing" + } + ] + } + } + } + } + } + + "3" -> + %{ + "next_batch" => "4", + "presence" => %{}, + "account_data" => %{}, + "rooms" => %{ + "join" => %{ + "!room_id" => %{ "timeline" => %{ "limited" => false, "prev_batch" => "p2", @@ -154,9 +175,9 @@ defmodule Polyjuice.Client.SyncTest do } } - "3" -> + "4" -> %{ - "next_batch" => "4", + "next_batch" => "5", "presence" => %{}, "account_data" => %{}, "rooms" => %{ @@ -294,6 +315,13 @@ defmodule Polyjuice.Client.SyncTest do ) assert_receive( + {:polyjuice_client, :ephemeral, + {"!room_id", + %{"content" => %{"user_ids" => ["@alice:example.org"]}, "type" => "m.typing"}}}, + 1000 + ) + + assert_receive( {:polyjuice_client, :message, {"!room_id", %{ |