diff options
author | Hadrien <26697460+ketsapiwiq@users.noreply.github.com> | 2020-11-11 16:43:57 +0100 |
---|---|---|
committer | Pierre de Lacroix <pierre@pdelacroix.com> | 2021-03-03 18:12:18 +0100 |
commit | f2ee1f306ad4b5a7bcc33d8e024b6f155c8bdab6 (patch) | |
tree | 8a5846b5e031223135514e974662cb03d30ed7f8 /lib | |
parent | add post register endpoint + tests (diff) |
register interface for client and lowlevel
Diffstat (limited to 'lib')
-rw-r--r-- | lib/polyjuice/client.ex | 100 | ||||
-rw-r--r-- | lib/polyjuice/client/low_level.ex | 40 |
2 files changed, 140 insertions, 0 deletions
diff --git a/lib/polyjuice/client.ex b/lib/polyjuice/client.ex index 361914d..54e2670 100644 --- a/lib/polyjuice/client.ex +++ b/lib/polyjuice/client.ex @@ -723,6 +723,106 @@ defmodule Polyjuice.Client do end end + @type register_opts() :: [ + kind: :guest | :user, + username: String.t() | nil, + password: String.t() | nil, + device_id: String.t() | nil, + initial_device_display_name: String.t() | nil, + inhibit_login: boolean() + ] + + @doc """ + Register a user. + + `opts` is a keyword list of options: + + - `username:` (string) the basis for the localpart of the desired Matrix ID + - `password:` (string) the desired password for the account + - `device_id:` (string) the device ID to use + - `initial_device_display_name:` (string) the display name to use for the device + - `inhibit_login:` (boolean) don't login after successful register + - `kind:` (atom) kind of account to register. Defaults to user. One of: ["guest", "user"] + """ + @spec register( + client :: Polyjuice.Client.t(), + opts :: register_opts() + ) :: {:ok, map()} | any + def register(%Polyjuice.Client{} = client, opts \\ []) do + case Polyjuice.Client.API.call( + client, + %Polyjuice.Client.Endpoint.PostRegister{ + auth: %{type: "m.login.dummy"}, + username: Keyword.get(opts, :username), + password: Keyword.get(opts, :password), + kind: Keyword.get(opts, :kind), + device_id: Keyword.get(opts, :device_id), + initial_device_display_name: Keyword.get(opts, :initial_device_display_name), + inhibit_login: Keyword.get(opts, :inhibit_login, false) + } + ) do + ret = + {:ok, + %{"access_token" => access_token, "user_id" => user_id, "device_id" => device_id} = + http_ret} -> + GenServer.cast( + client.pid, + {:set, %{access_token: access_token, user_id: user_id, device_id: device_id}} + ) + + # do not send logged_in event if the inhibit_login option is set to true + unless Keyword.get(opts, :inhibit_login) == true do + if client.opts.storage do + Polyjuice.Client.Storage.kv_put( + client.opts.storage, + "ca.uhoreg.polyjuice", + "access_token", + access_token + ) + + Polyjuice.Client.Storage.kv_put( + client.opts.storage, + "ca.uhoreg.polyjuice", + "user_id", + user_id + ) + + Polyjuice.Client.Storage.kv_put( + client.opts.storage, + "ca.uhoreg.polyjuice", + "device_id", + device_id + ) + end + + if client.opts.handler do + Polyjuice.Client.Handler.handle( + client.opts.handler, + :logged_in, + {user_id, device_id, Map.drop(http_ret, ["user_id", "device_id"])} + ) + end + + if client.opts.sync && client.opts.handler do + # make sure we don't already have a sync process running + kill_sync(client.id) + + supervisor_name = process_name(client.id, :supervisor) + + DynamicSupervisor.start_child( + supervisor_name, + sync_child_spec(client.base_url, client.id, client.pid, client.opts) + ) + end + end + + ret + + ret -> + ret + end + end + @doc false def kill_sync(id) do supervisor_name = process_name(id, :supervisor) diff --git a/lib/polyjuice/client/low_level.ex b/lib/polyjuice/client/low_level.ex index 6a6521d..51a9154 100644 --- a/lib/polyjuice/client/low_level.ex +++ b/lib/polyjuice/client/low_level.ex @@ -212,4 +212,44 @@ defmodule Polyjuice.Client.LowLevel do %Polyjuice.Client.Endpoint.PostLogout{} ) end + + @type register_opts() :: [ + kind: :guest | :user, + username: String.t() | nil, + password: String.t() | nil, + device_id: String.t() | nil, + initial_device_display_name: String.t() | nil, + inhibit_login: boolean() + ] + + @doc """ + Register a user. + + `opts` is a keyword list of options: + + - `username:` (string) the basis for the localpart of the desired Matrix ID + - `password:` (string) the desired password for the account + - `device_id:` (string) the device ID to use + - `initial_device_display_name:` (string) the display name to use for the device + - `inhibit_login:` (boolean) don't login after successful register + - `kind:` (atom) kind of account to register. Defaults to user. One of: ["guest", "user"] + """ + @spec register( + client :: Polyjuice.Client.LowLevel.t(), + opts :: register_opts() + ) :: {:ok, map()} | any + def register(client, opts \\ []) do + Polyjuice.Client.API.call( + client, + %Polyjuice.Client.Endpoint.PostRegister{ + kind: Keyword.get(opts, :kind, :user), + auth: %{type: "m.login.dummy"}, + username: Keyword.get(opts, :username), + password: Keyword.get(opts, :password), + device_id: Keyword.get(opts, :device_id), + initial_device_display_name: Keyword.get(opts, :initial_device_display_name), + inhibit_login: Keyword.get(opts, :inhibit_login, false) + } + ) + end end |