diff options
author | Pierre de Lacroix <pierre@pdelacroix.com> | 2020-05-12 02:48:14 +0200 |
---|---|---|
committer | Pierre de Lacroix <pierre@pdelacroix.com> | 2020-05-12 02:48:14 +0200 |
commit | bcf78d7f64567b269dd6d9da621bbfdacfbc0113 (patch) | |
tree | eea55d702aa3e377db781e47d33f8b894f2844e6 | |
parent | Remove irrelevant README (diff) |
laying out some structure
15 files changed, 171 insertions, 68 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_web/channels/user_socket.ex b/lib/matrix_app_service/phoenix/channels/user_socket.ex index ec2e9fb..ec2e9fb 100644 --- a/lib/matrix_app_service_web/channels/user_socket.ex +++ b/lib/matrix_app_service/phoenix/channels/user_socket.ex 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_web.ex b/lib/matrix_app_service/phoenix/phoenix.ex index e388fee..989c131 100644 --- a/lib/matrix_app_service_web.ex +++ b/lib/matrix_app_service/phoenix/phoenix.ex @@ -1,4 +1,4 @@ -defmodule MatrixAppServiceWeb do +defmodule MatrixAppService.Phoenix do @moduledoc """ The entrypoint for defining your web interface, such as controllers, views, channels and so on. @@ -22,15 +22,15 @@ defmodule MatrixAppServiceWeb do use Phoenix.Controller, namespace: MatrixAppServiceWeb import Plug.Conn - alias MatrixAppServiceWeb.Router.Helpers, as: Routes + alias MatrixAppService.Phoenix.Router.Helpers, as: Routes end end def view do quote do use Phoenix.View, - root: "lib/matrix_app_service_web/templates", - namespace: MatrixAppServiceWeb + 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] @@ -60,8 +60,8 @@ defmodule MatrixAppServiceWeb do # Import basic rendering functionality (render, render_layout, etc) import Phoenix.View - import MatrixAppServiceWeb.ErrorHelpers - alias MatrixAppServiceWeb.Router.Helpers, as: Routes + import MatrixAppService.Phoenix.ErrorHelpers + alias MatrixAppService.Phoenix.Router.Helpers, as: Routes 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_web/telemetry.ex b/lib/matrix_app_service/phoenix/telemetry.ex index de3ec2d..de3ec2d 100644 --- a/lib/matrix_app_service_web/telemetry.ex +++ b/lib/matrix_app_service/phoenix/telemetry.ex diff --git a/lib/matrix_app_service_web/views/error_helpers.ex b/lib/matrix_app_service/phoenix/views/error_helpers.ex index 4eff422..21e3555 100644 --- a/lib/matrix_app_service_web/views/error_helpers.ex +++ b/lib/matrix_app_service/phoenix/views/error_helpers.ex @@ -1,4 +1,4 @@ -defmodule MatrixAppServiceWeb.ErrorHelpers do +defmodule MatrixAppService.Phoenix.ErrorHelpers do @moduledoc """ Conveniences for translating and building error messages. """ diff --git a/lib/matrix_app_service_web/views/error_view.ex b/lib/matrix_app_service/phoenix/views/error_view.ex index 2358355..d9b91cc 100644 --- a/lib/matrix_app_service_web/views/error_view.ex +++ b/lib/matrix_app_service/phoenix/views/error_view.ex @@ -1,5 +1,5 @@ -defmodule MatrixAppServiceWeb.ErrorView do - use MatrixAppServiceWeb, :view +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. 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 deleted file mode 100644 index 3a24510..0000000 --- a/lib/matrix_app_service_web/router.ex +++ /dev/null @@ -1,11 +0,0 @@ -defmodule MatrixAppServiceWeb.Router do - use MatrixAppServiceWeb, :router - - pipeline :api do - plug :accepts, ["json"] - end - - scope "/api", MatrixAppServiceWeb do - pipe_through :api - end -end |