summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre de Lacroix <pierre@pdelacroix.com>2020-09-18 16:31:24 +0200
committerPierre de Lacroix <pierre@pdelacroix.com>2020-09-18 16:31:24 +0200
commit3d8d3f1d387a7081b0f9ed5e1a62b14a28e87981 (patch)
tree5ae669cc1ec5d1666b047abe4a9ae4805b12afb9
parentMerge branch 'test/matrix_app_auth_plug' into 'master' (diff)
refacto WIP
-rw-r--r--lib/matrix_app_service/adapter/room.ex14
-rw-r--r--lib/matrix_app_service/adapter/transaction.ex10
-rw-r--r--lib/matrix_app_service/adapter/user.ex14
-rw-r--r--lib/matrix_app_service/client.ex68
-rw-r--r--lib/matrix_app_service/phoenix/router.ex27
-rw-r--r--lib/matrix_app_service/transaction_module.ex3
-rw-r--r--lib/matrix_app_service_web/endpoint.ex45
-rw-r--r--lib/matrix_app_service_web/router.ex26
8 files changed, 107 insertions, 100 deletions
diff --git a/lib/matrix_app_service/adapter/room.ex b/lib/matrix_app_service/adapter/room.ex
new file mode 100644
index 0000000..4c1cafc
--- /dev/null
+++ b/lib/matrix_app_service/adapter/room.ex
@@ -0,0 +1,14 @@
+defmodule MatrixAppService.Adapter.Room do
+ @moduledoc """
+ Behaviour for a module that handles rooms that the application service has
+ reserved.
+ """
+
+ @doc """
+ Responds to a room query by alias. If the room exists, implementations should
+ create the room (for instance by using
+ `MatrixAppService.Client.create_room/1`) then return `:ok`. If the room
+ doesn't exist, they sould return anything else.
+ """
+ @callback query_alias(String.t()) :: :ok | Any
+end
diff --git a/lib/matrix_app_service/adapter/transaction.ex b/lib/matrix_app_service/adapter/transaction.ex
new file mode 100644
index 0000000..1845536
--- /dev/null
+++ b/lib/matrix_app_service/adapter/transaction.ex
@@ -0,0 +1,10 @@
+defmodule MatrixAppService.Adapter.Transaction do
+ @moduledoc """
+
+ """
+
+ @doc """
+
+ """
+ @callback new_event(MatrixAppService.Event.t()) :: any()
+end
diff --git a/lib/matrix_app_service/adapter/user.ex b/lib/matrix_app_service/adapter/user.ex
new file mode 100644
index 0000000..92b9d96
--- /dev/null
+++ b/lib/matrix_app_service/adapter/user.ex
@@ -0,0 +1,14 @@
+defmodule MatrixAppService.Adapter.User do
+ @moduledoc """
+ Behaviour for a module that handles users that the application service has
+ reserved.
+ """
+
+ @doc """
+ Responds to a user query by Matrix ID. If the user exists, implementations
+ should create the user (for instance by using
+ `MatrixAppService.Client.register/1`) then return `:ok`. If the user
+ doesn't exist, they sould return anything else.
+ """
+ @callback query_user(String.t()) :: :ok | Any
+end
diff --git a/lib/matrix_app_service/client.ex b/lib/matrix_app_service/client.ex
index 5cb720d..d806666 100644
--- a/lib/matrix_app_service/client.ex
+++ b/lib/matrix_app_service/client.ex
@@ -1,37 +1,61 @@
defmodule MatrixAppService.Client do
- @type client_options :: [user_id: String.t(), storage: Polyjuice.Client.Storage.t()]
+ @moduledoc """
- @spec client(String.t() | nil, Polyjuice.Client.Storage.t() | nil) ::
+ """
+
+ @type client_options :: {:base_url, String.t()} | MatrixAppService.Client.LowLevel.create_opts()
+
+ @doc """
+
+ """
+ @spec client([client_options()]) ::
Polyjuice.Client.LowLevel.t()
- def client(user_id \\ nil, storage \\ nil) do
- base_url = Application.get_env(:matrix_app_service, :base_url)
- access_token = Application.get_env(:matrix_app_service, :access_token)
-
- Polyjuice.Client.LowLevel.create(base_url,
- access_token: access_token,
- # "@alice:matrix.imago.local",
- user_id: user_id,
- device_id: "Application service",
- storage: storage
- )
+ def client(opts \\ []) do
+ base_url =
+ Keyword.get(opts, :base_url, Application.fetch_env!(:matrix_app_service, :base_url))
+
+ default_opts = [
+ access_token: Application.fetch_env!(:matrix_app_service, :access_token),
+ device_id: "APP_SERVICE"
+ ]
+
+ opts = Keyword.merge(default_opts, opts)
+
+ Polyjuice.Client.LowLevel.create(base_url, opts)
end
+ @doc """
+
+ """
@spec create_room(Keyword.t()) :: {:ok, String.t()} | Any
- def create_room(options) do
- client_with_options(options)
+ def create_room(options \\ [], client_options \\ []) do
+ client(client_options)
|> Polyjuice.Client.Room.create_room(options)
end
+ @doc """
+
+ """
@spec create_alias(String.t(), String.t(), client_options()) :: {:ok, String.t()} | Any
- def create_alias(room_id, room_alias, options \\ []) do
- client_with_options(options)
+ def create_alias(room_id, room_alias, client_options \\ []) do
+ client(client_options)
|> Polyjuice.Client.Room.create_alias(room_id, room_alias)
end
- @spec client(client_options(), Polyjuice.Client.Storage.t()) :: Polyjuice.Client.LowLevel.t()
- defp client_with_options(options) do
- user_id = Keyword.get(options, :user_id, nil)
- storage = Keyword.get(options, :storage, nil)
- client(user_id, storage)
+ @doc """
+
+ """
+ @spec register(Polyjuice.Client.LowLevel.register_opts(), client_options()) :: {:ok, String.t()} | Any
+ def register(opts \\ [], client_options \\ []) do
+ default_opts = [
+ inhibit_login: true,
+ device_id: "APP_SERVICE",
+ initial_device_display_name: "Application Service"
+ ]
+
+ opts = Keyword.merge(default_opts, opts)
+
+ client(client_options)
+ |> Polyjuice.Client.LowLevel.register(opts)
end
end
diff --git a/lib/matrix_app_service/phoenix/router.ex b/lib/matrix_app_service/phoenix/router.ex
deleted file mode 100644
index b3e6c8f..0000000
--- a/lib/matrix_app_service/phoenix/router.ex
+++ /dev/null
@@ -1,27 +0,0 @@
-defmodule MatrixAppService.Phoenix.Router do
- defmacro routes() do
- quote do
- pipeline :matrix_api do
- plug :accepts, ["json"]
- plug MatrixAppServiceWeb.AuthPlug
- end
-
- path = Application.compile_env(:matrix_app_service, :path, "/")
-
- scope path, MatrixAppServiceWeb.V1 do
- pipe_through :matrix_api
-
- put "/transactions/:txn_id", TransactionController, :create
-
- get "/users/:user_id", UserController, :show
- get "/rooms/:room_alias", RoomController, :show
-
- get "/thirdparty/protocol/:protocol", ThirdPartyController, :show
- get "/thirdparty/user/:protocol", ThirdPartyController, :show
- get "/thirdparty/location/:protocol", ThirdPartyController, :show
- get "/thirdparty/location", ThirdPartyController, :show
- get "/thirdparty/user", ThirdPartyController, :show
- end
- end
- end
-end
diff --git a/lib/matrix_app_service/transaction_module.ex b/lib/matrix_app_service/transaction_module.ex
deleted file mode 100644
index 99058f6..0000000
--- a/lib/matrix_app_service/transaction_module.ex
+++ /dev/null
@@ -1,3 +0,0 @@
-defmodule MatrixAppService.TransactionModule do
- @callback new_event(MatrixAppService.Event.t()) :: any()
-end
diff --git a/lib/matrix_app_service_web/endpoint.ex b/lib/matrix_app_service_web/endpoint.ex
deleted file mode 100644
index 0ee00fc..0000000
--- a/lib/matrix_app_service_web/endpoint.ex
+++ /dev/null
@@ -1,45 +0,0 @@
-defmodule MatrixAppServiceWeb.Endpoint do
- use Phoenix.Endpoint, otp_app: :matrix_app_service
-
- # The session will be stored in the cookie and signed,
- # this means its contents can be read but not tampered with.
- # Set :encryption_salt if you would also like to encrypt it.
- @session_options [
- store: :cookie,
- key: "_matrix_app_service_key",
- signing_salt: "zE7AHynD"
- ]
-
- socket "/socket", MatrixAppServiceWeb.UserSocket,
- websocket: true,
- longpoll: false
-
- # Serve at "/" the static files from "priv/static" directory.
- #
- # You should set gzip to true if you are running phx.digest
- # when deploying your static files in production.
- plug Plug.Static,
- at: "/",
- from: :matrix_app_service,
- gzip: false,
- only: ~w(css fonts images js favicon.ico robots.txt)
-
- # Code reloading can be explicitly enabled under the
- # :code_reloader configuration of your endpoint.
- if code_reloading? do
- plug Phoenix.CodeReloader
- end
-
- plug Plug.RequestId
- plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
-
- plug Plug.Parsers,
- parsers: [:urlencoded, :multipart, :json],
- pass: ["*/*"],
- json_decoder: Phoenix.json_library()
-
- plug Plug.MethodOverride
- plug Plug.Head
- plug Plug.Session, @session_options
- plug MatrixAppServiceWeb.Router
-end
diff --git a/lib/matrix_app_service_web/router.ex b/lib/matrix_app_service_web/router.ex
index 99f4dfd..21ccc39 100644
--- a/lib/matrix_app_service_web/router.ex
+++ b/lib/matrix_app_service_web/router.ex
@@ -1,7 +1,27 @@
defmodule MatrixAppServiceWeb.Router do
- use MatrixAppServiceWeb, :router
+ defmacro routes() do
+ quote do
+ pipeline :matrix_api do
+ plug :accepts, ["json"]
+ plug MatrixAppServiceWeb.AuthPlug
+ end
- require MatrixAppService.Phoenix.Router
+ path = Application.compile_env(:matrix_app_service, :path, "/")
- MatrixAppService.Phoenix.Router.routes()
+ scope path, MatrixAppServiceWeb.V1 do
+ pipe_through :matrix_api
+
+ put "/transactions/:txn_id", TransactionController, :create
+
+ get "/users/:user_id", UserController, :show
+ get "/rooms/:room_alias", RoomController, :show
+
+ get "/thirdparty/protocol/:protocol", ThirdPartyController, :show
+ get "/thirdparty/user/:protocol", ThirdPartyController, :show
+ get "/thirdparty/location/:protocol", ThirdPartyController, :show
+ get "/thirdparty/location", ThirdPartyController, :show
+ get "/thirdparty/user", ThirdPartyController, :show
+ end
+ end
+ end
end