summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHubert Chathi <hubert@uhoreg.ca>2021-02-20 14:55:48 +0000
committerHubert Chathi <hubert@uhoreg.ca>2021-02-20 14:55:48 +0000
commitb7192d97b0c428ec52cf3c8dd8ed2ea7bfa7341e (patch)
tree92895ec3228fef5a6d80626fc9ccab7a16ec5b42
parentMerge branch 'profile_function' into 'master' (diff)
parentHandle ephemeral events (e.g. for typing indicator) (diff)
Merge branch 'feature/ephemeral-events' into 'master'
Handle ephemeral events See merge request uhoreg/polyjuice_client!8
-rw-r--r--lib/polyjuice/client/sync.ex40
-rw-r--r--test/polyjuice/client/sync_test.exs32
-rw-r--r--test/polyjuice/client_test.exs10
-rw-r--r--test/support/client_test.ex9
4 files changed, 56 insertions, 35 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",
%{
diff --git a/test/polyjuice/client_test.exs b/test/polyjuice/client_test.exs
index 089c97f..aad44a3 100644
--- a/test/polyjuice/client_test.exs
+++ b/test/polyjuice/client_test.exs
@@ -72,16 +72,6 @@ defmodule Polyjuice.ClientTest do
end
end
- defmodule Httpd.LoggedOut do
- # just tells the user that they were logged out, no matter what
- def _matrix(session_id, _env, _input) do
- :mod_esi.deliver(
- session_id,
- 'Status: 401 Unauthorized\r\nContent-Type: application/json\r\n\r\n{"errcode":"M_UNKNOWN_TOKEN","error":"Unknown token"}'
- )
- end
- end
-
test "transaction_id is unique" do
client = %Polyjuice.Client{
base_url: "http://localhost:8008",
diff --git a/test/support/client_test.ex b/test/support/client_test.ex
new file mode 100644
index 0000000..c9fd2b8
--- /dev/null
+++ b/test/support/client_test.ex
@@ -0,0 +1,9 @@
+defmodule Polyjuice.ClientTest.Httpd.LoggedOut do
+ # just tells the user that they were logged out, no matter what
+ def _matrix(session_id, _env, _input) do
+ :mod_esi.deliver(
+ session_id,
+ 'Status: 401 Unauthorized\r\nContent-Type: application/json\r\n\r\n{"errcode":"M_UNKNOWN_TOKEN","error":"Unknown token"}'
+ )
+ end
+end