From a946555b69a584f74455e5124d4d2597bc5114c7 Mon Sep 17 00:00:00 2001 From: Arjan Scherpenisse Date: Fri, 19 Feb 2021 13:03:24 +0100 Subject: Handle ephemeral events (e.g. for typing indicator) --- lib/polyjuice/client/sync.ex | 40 ++++++++++++++++--------------------- 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 @@ -130,6 +130,27 @@ defmodule Polyjuice.Client.SyncTest do "next_batch" => "3", "presence" => %{}, "account_data" => %{}, + "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" => %{ @@ -154,9 +175,9 @@ defmodule Polyjuice.Client.SyncTest do } } - "3" -> + "4" -> %{ - "next_batch" => "4", + "next_batch" => "5", "presence" => %{}, "account_data" => %{}, "rooms" => %{ @@ -293,6 +314,13 @@ defmodule Polyjuice.Client.SyncTest do 1000 ) + assert_receive( + {:polyjuice_client, :ephemeral, + {"!room_id", + %{"content" => %{"user_ids" => ["@alice:example.org"]}, "type" => "m.typing"}}}, + 1000 + ) + assert_receive( {:polyjuice_client, :message, {"!room_id", -- cgit v1.2.3