File size: 3,615 Bytes
c27e67a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
defmodule PlausibleWeb do
  @moduledoc false

  use Plausible

  def live_view(opts \\ []) do
    quote do
      use Plausible
      use Phoenix.LiveView, global_prefixes: ~w(x-)
      use PlausibleWeb.Live.Flash

      use PlausibleWeb.Live.AuthContext

      unless :no_sentry_context in unquote(opts) do
        use PlausibleWeb.Live.SentryContext
      end

      on_ee do
        use Plausible.Audit.LiveContext
      end

      alias PlausibleWeb.Router.Helpers, as: Routes
      alias Phoenix.LiveView.JS

      import PlausibleWeb.Components.Generic
      import PlausibleWeb.Live.Components.Form
    end
  end

  def live_component do
    quote do
      use Phoenix.LiveComponent, global_prefixes: ~w(x-)
      import PlausibleWeb.Components.Generic
      import PlausibleWeb.Live.Components.Form
      alias Phoenix.LiveView.JS
      alias PlausibleWeb.Router.Helpers, as: Routes
    end
  end

  def component do
    quote do
      use Phoenix.Component, global_prefixes: ~w(x-)
      import PlausibleWeb.Components.Generic
      import PlausibleWeb.Live.Components.Form
      alias Phoenix.LiveView.JS
      alias PlausibleWeb.Router.Helpers, as: Routes
    end
  end

  def controller do
    quote do
      use Phoenix.Controller, formats: [html: "View", json: "View"]

      import Plug.Conn
      import PlausibleWeb.ControllerHelpers
      alias PlausibleWeb.Router.Helpers, as: Routes
    end
  end

  def view do
    quote do
      use Phoenix.View,
        root: "lib/plausible_web/templates"

      # Import convenience functions from controllers
      import Phoenix.Controller, only: [view_module: 1]

      use Phoenix.Component, global_prefixes: ~w(x-)

      import PlausibleWeb.Components.Generic
      import PlausibleWeb.Live.Components.Form
      alias PlausibleWeb.Router.Helpers, as: Routes
    end
  end

  on_ee do
    def extra_view do
      quote do
        use Phoenix.View,
          root: "extra/lib/plausible_web/templates"

        # Import convenience functions from controllers
        import Phoenix.Controller, only: [view_module: 1]

        use Phoenix.Component

        import PlausibleWeb.Components.Generic
        import PlausibleWeb.Live.Components.Form
        alias PlausibleWeb.Router.Helpers, as: Routes
      end
    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

  def plugins_api_controller do
    quote do
      use Phoenix.Controller, formats: [:json]
      import Plug.Conn
      import PlausibleWeb.Router.Helpers

      alias PlausibleWeb.Plugins.API.Schemas
      alias PlausibleWeb.Plugins.API.Views
      alias PlausibleWeb.Plugins.API.Errors
      alias Plausible.Plugins.API

      plug(OpenApiSpex.Plug.CastAndValidate, json_render_error_v2: true, replace_params: false)

      use OpenApiSpex.ControllerSpecs
    end
  end

  def plugins_api_view do
    quote do
      use Phoenix.View, root: ""

      alias PlausibleWeb.Router.Helpers
      import PlausibleWeb.Plugins.API.Views.Pagination, only: [render_metadata_links: 4]
    end
  end

  def open_api_schema do
    quote do
      require OpenApiSpex
      alias OpenApiSpex.Schema
      alias PlausibleWeb.Plugins.API.Schemas
    end
  end

  @doc """
  When used, dispatch to the appropriate controller/view/etc.
  """
  defmacro __using__(which) when is_atom(which) do
    apply(__MODULE__, which, [])
  end

  defmacro __using__([{which, opts}]) when is_atom(which) do
    apply(__MODULE__, which, [List.wrap(opts)])
  end
end