summaryrefslogtreecommitdiff
path: root/lib/matrix_app_service_web/routes.ex
blob: 54f5c833deb8c4a5fac15bd963ebdbe6ab24c800 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
defmodule MatrixAppServiceWeb.Routes do
  @moduledoc """
  Provides the Matrix Application Service API routes.

  https://matrix.org/docs/spec/application_service/r0.1.2
  """

  @doc """

  """
  defmacro __using__(_env) do
    quote do
      require unquote(__MODULE__)

      @matrix_app_services 0
    end
  end

  @doc """
  This macro injects the API routes in a Phoenix router.
  """
  # defmacro routes(opts \\ [])
  defmacro routes(:no_config) do
    quote do
      pipeline :matrix_app_service do
        plug :accepts, ["json"]
      end

      scope "/", MatrixAppServiceWeb.V1, as: :matrix do
        pipe_through :matrix_app_service

        put "/transactions/:txn_id", TransactionController, :push

        get "/users/:user_id", UserController, :query
        get "/rooms/:room_alias", RoomController, :query

        get "/thirdparty/protocol/:protocol", ThirdPartyController, :query_protocol
        get "/thirdparty/user/:protocol", ThirdPartyController, :query_users
        get "/thirdparty/location/:protocol", ThirdPartyController, :query_locations
        get "/thirdparty/location", ThirdPartyController, :query_location_by_alias
        get "/thirdparty/user", ThirdPartyController, :query_user_by_id
      end
    end
  end

  defmacro routes(opts) do
    quote bind_quoted: [opts: opts] do
      path = Keyword.get(opts, :path, "/")
      namespace = Keyword.get(opts, :namespace, :matrix)
      homeserver_token = Keyword.fetch!(opts, :homeserver_token)
      pipeline_name = String.to_atom("matrix_api_#{@matrix_app_services}")

      pipeline pipeline_name do
        plug :accepts, ["json"]
        plug MatrixAppServiceWeb.SetConfigPlug, opts
        plug MatrixAppServiceWeb.AuthPlug, homeserver_token
      end

      scope path, MatrixAppServiceWeb.V1, as: namespace do
        pipe_through pipeline_name

        put "/transactions/:txn_id", TransactionController, :push

        get "/users/:user_id", UserController, :query
        get "/rooms/:room_alias", RoomController, :query

        get "/thirdparty/protocol/:protocol", ThirdPartyController, :query_protocol
        get "/thirdparty/user/:protocol", ThirdPartyController, :query_users
        get "/thirdparty/location/:protocol", ThirdPartyController, :query_locations
        get "/thirdparty/location", ThirdPartyController, :query_location_by_alias
        get "/thirdparty/user", ThirdPartyController, :query_user_by_id
      end

      @matrix_app_services @matrix_app_services + 1
    end
  end
end