From d11baa1ab6628781506ad17936dfcd5a9afd0821 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Sat, 26 Dec 2020 21:32:44 -0500 Subject: add function for getting user/device ID and use it when the user ID is unspecified when setting/getting account data --- lib/polyjuice/client.ex | 16 ++++++++++++++++ lib/polyjuice/client/account.ex | 12 ++++++------ lib/polyjuice/client/low_level.ex | 4 ++++ test/polyjuice/client/account_test.exs | 35 ++++++++++++++++++++++++++++++---- test/polyjuice/client_test.exs | 17 +++++++++++++++++ test/support/dummy_client.ex | 8 ++++++++ 6 files changed, 82 insertions(+), 10 deletions(-) diff --git a/lib/polyjuice/client.ex b/lib/polyjuice/client.ex index d7ad60a..361914d 100644 --- a/lib/polyjuice/client.ex +++ b/lib/polyjuice/client.ex @@ -308,6 +308,11 @@ defmodule Polyjuice.Client do {:reply, Map.take(state, [:access_token, :user_id, :device_id]), state} end + @impl GenServer + def handle_call(:get_user_and_device, _from, state) do + {:reply, {Map.get(state, :user_id), Map.get(state, :device_id)}, state} + end + @impl GenServer def handle_cast({:set, new_state}, state) do Map.take(new_state, [:access_token, :user_id, :device_id]) @@ -365,6 +370,13 @@ defmodule Polyjuice.Client do @spec transaction_id(client_api :: Polyjuice.Client.API.t()) :: String.t() def transaction_id(client_api) + @doc """ + Get the client's user and device IDs. + """ + @spec get_user_and_device(client_api :: Polyjuice.Client.API.t()) :: + {String.t() | nil, String.t() | nil} + def get_user_and_device(client_api) + @doc """ Stop the client. """ @@ -502,6 +514,10 @@ defmodule Polyjuice.Client do "#{Node.self()}_#{:erlang.system_time(:millisecond)}_#{:erlang.unique_integer()}" end + def get_user_and_device(%{pid: pid}) do + GenServer.call(pid, :get_user_and_device) + end + def stop(%{pid: pid}, reason \\ :normal, timeout \\ :infinity) do GenServer.stop(pid, reason, timeout) end diff --git a/lib/polyjuice/client/account.ex b/lib/polyjuice/client/account.ex index 0ad18c0..d7b8d0d 100644 --- a/lib/polyjuice/client/account.ex +++ b/lib/polyjuice/client/account.ex @@ -24,14 +24,14 @@ defmodule Polyjuice.Client.Account do """ @spec get_data( client_api :: Polyjuice.Client.API.t(), - user_id :: String.t(), + user_id :: String.t() | nil, event_type :: String.t() ) :: {:ok, map()} | any - def get_data(client_api, user_id, event_type) do + def get_data(client_api, user_id \\ nil, event_type) do Polyjuice.Client.API.call( client_api, %Polyjuice.Client.Endpoint.GetAccountData{ - user_id: user_id, + user_id: user_id || Polyjuice.Client.API.get_user_and_device(client_api) |> elem(0), type: event_type } ) @@ -45,15 +45,15 @@ defmodule Polyjuice.Client.Account do """ @spec put_data( client_api :: Polyjuice.Client.API.t(), - user_id :: String.t(), + user_id :: String.t() | nil, event_type :: String.t(), changes :: Map.t() ) :: {:ok} | any - def put_data(client_api, user_id, event_type, changes) do + def put_data(client_api, user_id \\ nil, event_type, changes) do Polyjuice.Client.API.call( client_api, %Polyjuice.Client.Endpoint.PutAccountData{ - user_id: user_id, + user_id: user_id || Polyjuice.Client.API.get_user_and_device(client_api) |> elem(0), type: event_type, account_data: changes } diff --git a/lib/polyjuice/client/low_level.ex b/lib/polyjuice/client/low_level.ex index 54acf83..91b39ae 100644 --- a/lib/polyjuice/client/low_level.ex +++ b/lib/polyjuice/client/low_level.ex @@ -118,6 +118,10 @@ defmodule Polyjuice.Client.LowLevel do "#{Node.self()}_#{:erlang.system_time(:millisecond)}_#{:erlang.unique_integer()}" end + def get_user_and_device(%{user_id: user_id, device_id: device_id}) do + {user_id, device_id} + end + def stop(_, _, _) do # don't need to do anything to stop it :ok diff --git a/test/polyjuice/client/account_test.exs b/test/polyjuice/client/account_test.exs index 6f11d25..4a21df4 100644 --- a/test/polyjuice/client/account_test.exs +++ b/test/polyjuice/client/account_test.exs @@ -20,14 +20,28 @@ defmodule Polyjuice.Client.AccountTest do with client = %DummyClient{ response: { %Polyjuice.Client.Endpoint.GetAccountData{ - user_id: "toto@kazarma.local", + user_id: "@toto:kazarma.local", type: "name" }, {:ok, %{name: "toto"}} } } do {:ok, %{name: name}} = - Polyjuice.Client.Account.get_data(client, "toto@kazarma.local", "name") + Polyjuice.Client.Account.get_data(client, "@toto:kazarma.local", "name") + + assert name == "toto" + end + + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.GetAccountData{ + user_id: "@alice:example.org", + type: "name" + }, + {:ok, %{name: "toto"}} + } + } do + {:ok, %{name: name}} = Polyjuice.Client.Account.get_data(client, "name") assert name == "toto" end @@ -37,7 +51,7 @@ defmodule Polyjuice.Client.AccountTest do with client = %DummyClient{ response: { %Polyjuice.Client.Endpoint.PutAccountData{ - user_id: "toto@kazarma.local", + user_id: "@toto:kazarma.local", type: "name", account_data: %{name: "marc"} }, @@ -45,7 +59,20 @@ defmodule Polyjuice.Client.AccountTest do } } do {:ok, %{}} = - Polyjuice.Client.Account.put_data(client, "toto@kazarma.local", "name", %{name: "marc"}) + Polyjuice.Client.Account.put_data(client, "@toto:kazarma.local", "name", %{name: "marc"}) + end + + with client = %DummyClient{ + response: { + %Polyjuice.Client.Endpoint.PutAccountData{ + user_id: "@alice:example.org", + type: "name", + account_data: %{name: "marc"} + }, + {:ok, %{}} + } + } do + {:ok, %{}} = Polyjuice.Client.Account.put_data(client, "name", %{name: "marc"}) end end end diff --git a/test/polyjuice/client_test.exs b/test/polyjuice/client_test.exs index c516096..089c97f 100644 --- a/test/polyjuice/client_test.exs +++ b/test/polyjuice/client_test.exs @@ -423,4 +423,21 @@ defmodule Polyjuice.ClientTest do File.rm_rf(tmpdir) end end + + test "gets the user and device IDs" do + {:ok, client_pid} = + Polyjuice.Client.start_link( + "", + access_token: nil, + sync: false, + handler: self(), + test: true, + user_id: "@alice:example.org", + device_id: "DEVICEID" + ) + + client = Polyjuice.Client.get_client(client_pid) + + assert Polyjuice.Client.API.get_user_and_device(client) == {"@alice:example.org", "DEVICEID"} + end end diff --git a/test/support/dummy_client.ex b/test/support/dummy_client.ex index bc0d202..4335282 100644 --- a/test/support/dummy_client.ex +++ b/test/support/dummy_client.ex @@ -33,6 +33,10 @@ defmodule DummyClient do def transaction_id(_), do: "txn_id" + def get_user_and_device(_) do + {"@alice:example.org", "DEVICEID"} + end + def stop(_, _, _) do # don't need to do anything to stop it :ok @@ -76,6 +80,10 @@ defmodule DummyClient do def transaction_id(_), do: "txn_id" + def get_user_and_device(_) do + {"@alice:example.org", "DEVICEID"} + end + def stop(%{pid: pid}, _, _) do # make sure we weren't expecting any more requests remaining = Agent.get(pid, & &1) -- cgit v1.2.3