diff options
Diffstat (limited to 'lib/matrix_app_service')
13 files changed, 349 insertions, 3 deletions
diff --git a/lib/matrix_app_service/application.ex b/lib/matrix_app_service/application.ex index 648cc70..f3d5803 100644 --- a/lib/matrix_app_service/application.ex +++ b/lib/matrix_app_service/application.ex @@ -8,11 +8,11 @@ defmodule MatrixAppService.Application do def start(_type, _args) do children = [ # Start the Telemetry supervisor - MatrixAppServiceWeb.Telemetry, + # MatrixAppServiceWeb.Telemetry, # Start the PubSub system - {Phoenix.PubSub, name: MatrixAppService.PubSub}, + # {Phoenix.PubSub, name: MatrixAppService.PubSub}, # Start the Endpoint (http/https) - MatrixAppServiceWeb.Endpoint + # MatrixAppServiceWeb.Endpoint # Start a worker by calling: MatrixAppService.Worker.start_link(arg) # {MatrixAppService.Worker, arg} ] diff --git a/lib/matrix_app_service/event.ex b/lib/matrix_app_service/event.ex new file mode 100644 index 0000000..9bdf34d --- /dev/null +++ b/lib/matrix_app_service/event.ex @@ -0,0 +1,12 @@ +defmodule MatrixAppService.Event do + defstruct age: nil, + content: %{}, + event_id: nil, + origin_server_ts: nil, + room_id: nil, + sender: nil, + type: nil, + unsigned: %{}, + user_id: nil + +end diff --git a/lib/matrix_app_service/phoenix/channels/user_socket.ex b/lib/matrix_app_service/phoenix/channels/user_socket.ex new file mode 100644 index 0000000..ec2e9fb --- /dev/null +++ b/lib/matrix_app_service/phoenix/channels/user_socket.ex @@ -0,0 +1,35 @@ +defmodule MatrixAppServiceWeb.UserSocket do + use Phoenix.Socket + + ## Channels + # channel "room:*", MatrixAppServiceWeb.RoomChannel + + # Socket params are passed from the client and can + # be used to verify and authenticate a user. After + # verification, you can put default assigns into + # the socket that will be set for all channels, ie + # + # {:ok, assign(socket, :user_id, verified_user_id)} + # + # To deny connection, return `:error`. + # + # See `Phoenix.Token` documentation for examples in + # performing token verification on connect. + @impl true + def connect(_params, socket, _connect_info) do + {:ok, socket} + end + + # Socket id's are topics that allow you to identify all sockets for a given user: + # + # def id(socket), do: "user_socket:#{socket.assigns.user_id}" + # + # Would allow you to broadcast a "disconnect" event and terminate + # all active sockets and channels for a given user: + # + # MatrixAppServiceWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{}) + # + # Returning `nil` makes this socket anonymous. + @impl true + def id(_socket), do: nil +end diff --git a/lib/matrix_app_service/phoenix/controllers/v1/room_controller.ex b/lib/matrix_app_service/phoenix/controllers/v1/room_controller.ex new file mode 100644 index 0000000..409ffa7 --- /dev/null +++ b/lib/matrix_app_service/phoenix/controllers/v1/room_controller.ex @@ -0,0 +1,7 @@ +defmodule MatrixAppService.Phoenix.V1.RoomController do + use MatrixAppService.Phoenix, :controller + + def show(conn, _params) do + send_resp(conn, 404, "") + end +end diff --git a/lib/matrix_app_service/phoenix/controllers/v1/third_party_controller.ex b/lib/matrix_app_service/phoenix/controllers/v1/third_party_controller.ex new file mode 100644 index 0000000..32b7e7b --- /dev/null +++ b/lib/matrix_app_service/phoenix/controllers/v1/third_party_controller.ex @@ -0,0 +1,7 @@ +defmodule MatrixAppService.Phoenix.V1.ThirdPartyController do + use MatrixAppService.Phoenix, :controller + + def show(conn, _params) do + send_resp(conn, 200, "") + end +end diff --git a/lib/matrix_app_service/phoenix/controllers/v1/transaction_controller.ex b/lib/matrix_app_service/phoenix/controllers/v1/transaction_controller.ex new file mode 100644 index 0000000..ebee348 --- /dev/null +++ b/lib/matrix_app_service/phoenix/controllers/v1/transaction_controller.ex @@ -0,0 +1,52 @@ +defmodule MatrixAppService.Phoenix.V1.TransactionController do + use MatrixAppService.Phoenix, :controller + + @module Application.compile_env!(:matrix_app_service, :module) + + # %{"access_token" => access_token, + # "events" => [ + # %{"age" => 199, + # "content" => %{ + # "body" => "test", + # "msgtype" => "m.room.message"}, + # "event_id" => "$pMDN7TcsCapjLRYoITwryk0anpQGs60y2Eng3lxropc", + # "origin_server_ts" => 1588950428966, + # "room_id" => "!SrVtqmyqzuaMnfVImP:matrix.imago.local", + # "sender" => "@alice:matrix.imago.local", + # "type" => "m.room.message", + # "unsigned" => %{"age" => 199}, + # "user_id" => "@alice:matrix.imago.local"}], + # "txn_id" => "269"} + + defp create_event( + %{"age" => age, + "content" => content, + "event_id" => event_id, + "origin_server_ts" => origin_server_ts, + "room_id" => room_id, + "sender" => sender, + "type" => type, + "unsigned" => unsigned, + "user_id" => user_id + }) do + event = + %MatrixAppService.Event{ + age: age, + content: content, + event_id: event_id, + origin_server_ts: origin_server_ts, + room_id: room_id, + sender: sender, + type: type, + unsigned: unsigned, + user_id: user_id + } + + @module.new_event(event) + end + + def create(conn, %{"events" => events}) do + Enum.each(events, &create_event(&1)) + send_resp(conn, 200, "{}") + end +end diff --git a/lib/matrix_app_service/phoenix/controllers/v1/user_controller.ex b/lib/matrix_app_service/phoenix/controllers/v1/user_controller.ex new file mode 100644 index 0000000..df11a4b --- /dev/null +++ b/lib/matrix_app_service/phoenix/controllers/v1/user_controller.ex @@ -0,0 +1,7 @@ +defmodule MatrixAppService.Phoenix.V1.UserController do + use MatrixAppService.Phoenix, :controller + + def show(conn, _params) do + send_resp(conn, 404, "") + end +end diff --git a/lib/matrix_app_service/phoenix/endpoint.ex b/lib/matrix_app_service/phoenix/endpoint.ex new file mode 100644 index 0000000..8af15eb --- /dev/null +++ b/lib/matrix_app_service/phoenix/endpoint.ex @@ -0,0 +1,45 @@ +#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/phoenix/phoenix.ex b/lib/matrix_app_service/phoenix/phoenix.ex new file mode 100644 index 0000000..989c131 --- /dev/null +++ b/lib/matrix_app_service/phoenix/phoenix.ex @@ -0,0 +1,74 @@ +defmodule MatrixAppService.Phoenix do + @moduledoc """ + The entrypoint for defining your web interface, such + as controllers, views, channels and so on. + + This can be used in your application as: + + use MatrixAppServiceWeb, :controller + use MatrixAppServiceWeb, :view + + The definitions below will be executed for every view, + controller, etc, so keep them short and clean, focused + on imports, uses and aliases. + + Do NOT define functions inside the quoted expressions + below. Instead, define any helper function in modules + and import those modules here. + """ + + def controller do + quote do + use Phoenix.Controller, namespace: MatrixAppServiceWeb + + import Plug.Conn + alias MatrixAppService.Phoenix.Router.Helpers, as: Routes + end + end + + def view do + quote do + use Phoenix.View, + root: "lib/matrix_app_service/phoenix/templates", + namespace: MatrixAppService.Phoenix + + # Import convenience functions from controllers + import Phoenix.Controller, only: [get_flash: 1, get_flash: 2, view_module: 1] + + # Include shared imports and aliases for views + unquote(view_helpers()) + end + end + + def router do + quote do + use Phoenix.Router + + import Plug.Conn + import Phoenix.Controller + end + end + + def channel do + quote do + use Phoenix.Channel + end + end + + defp view_helpers do + quote do + # Import basic rendering functionality (render, render_layout, etc) + import Phoenix.View + + import MatrixAppService.Phoenix.ErrorHelpers + alias MatrixAppService.Phoenix.Router.Helpers, as: Routes + end + end + + @doc """ + When used, dispatch to the appropriate controller/view/etc. + """ + defmacro __using__(which) when is_atom(which) do + apply(__MODULE__, which, []) + end +end diff --git a/lib/matrix_app_service/phoenix/router.ex b/lib/matrix_app_service/phoenix/router.ex new file mode 100644 index 0000000..715464b --- /dev/null +++ b/lib/matrix_app_service/phoenix/router.ex @@ -0,0 +1,29 @@ +defmodule MatrixAppService.Phoenix.Router do + + defmacro routes() do + quote do + pipeline :matrix_api do + plug :accepts, ["json"] + end + + scope "/matrix", MatrixAppService.Phoenix.V1 do + pipe_through :matrix_api + + # scope "/v1", V1 do + put "/transactions/:txn_id", TransactionController, :create + + get "/users/:user_id", UserController, :show + get "/rooms/:room_alias", RoomController, :show + + # scope "/thirdparty", ThirdParty do + 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 + end +end diff --git a/lib/matrix_app_service/phoenix/telemetry.ex b/lib/matrix_app_service/phoenix/telemetry.ex new file mode 100644 index 0000000..de3ec2d --- /dev/null +++ b/lib/matrix_app_service/phoenix/telemetry.ex @@ -0,0 +1,46 @@ +defmodule MatrixAppServiceWeb.Telemetry do + use Supervisor + import Telemetry.Metrics + + def start_link(arg) do + Supervisor.start_link(__MODULE__, arg, name: __MODULE__) + end + + @impl true + def init(_arg) do + children = [ + {:telemetry_poller, measurements: periodic_measurements(), period: 10_000} + # Add reporters as children of your supervision tree. + # {Telemetry.Metrics.ConsoleReporter, metrics: metrics()} + ] + + Supervisor.init(children, strategy: :one_for_one) + end + + def metrics do + [ + # Phoenix Metrics + summary("phoenix.endpoint.stop.duration", + unit: {:native, :millisecond} + ), + summary("phoenix.router_dispatch.stop.duration", + tags: [:route], + unit: {:native, :millisecond} + ), + + # VM Metrics + summary("vm.memory.total", unit: {:byte, :kilobyte}), + summary("vm.total_run_queue_lengths.total"), + summary("vm.total_run_queue_lengths.cpu"), + summary("vm.total_run_queue_lengths.io") + ] + end + + defp periodic_measurements do + [ + # A module, function and arguments to be invoked periodically. + # This function must call :telemetry.execute/3 and a metric must be added above. + # {MatrixAppServiceWeb, :count_users, []} + ] + end +end diff --git a/lib/matrix_app_service/phoenix/views/error_helpers.ex b/lib/matrix_app_service/phoenix/views/error_helpers.ex new file mode 100644 index 0000000..21e3555 --- /dev/null +++ b/lib/matrix_app_service/phoenix/views/error_helpers.ex @@ -0,0 +1,16 @@ +defmodule MatrixAppService.Phoenix.ErrorHelpers do + @moduledoc """ + Conveniences for translating and building error messages. + """ + + @doc """ + Translates an error message. + """ + def translate_error({msg, opts}) do + # Because the error messages we show in our forms and APIs + # are defined inside Ecto, we need to translate them dynamically. + Enum.reduce(opts, msg, fn {key, value}, acc -> + String.replace(acc, "%{#{key}}", to_string(value)) + end) + end +end diff --git a/lib/matrix_app_service/phoenix/views/error_view.ex b/lib/matrix_app_service/phoenix/views/error_view.ex new file mode 100644 index 0000000..d9b91cc --- /dev/null +++ b/lib/matrix_app_service/phoenix/views/error_view.ex @@ -0,0 +1,16 @@ +defmodule MatrixAppService.Phoenix.ErrorView do + use MatrixAppService.Phoenix, :view + + # If you want to customize a particular status code + # for a certain format, you may uncomment below. + # def render("500.json", _assigns) do + # %{errors: %{detail: "Internal Server Error"}} + # end + + # By default, Phoenix returns the status message from + # the template name. For example, "404.json" becomes + # "Not Found". + def template_not_found(template, _assigns) do + %{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}} + end +end |