1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web do
7 A module that keeps using definitions for controllers,
10 This can be used in your application as:
12 use Pleroma.Web, :controller
13 use Pleroma.Web, :view
15 The definitions below will be executed for every view,
16 controller, etc, so keep them short and clean, focused
17 on imports, uses and aliases.
19 Do NOT define functions inside the quoted expressions
25 use Phoenix.Controller, namespace: Pleroma.Web
28 import Pleroma.Web.Gettext
29 import Pleroma.Web.Router.Helpers
30 import Pleroma.Web.TranslationHelpers
32 alias Pleroma.Plugs.PlugHelper
36 defp set_put_layout(conn, _) do
37 put_layout(conn, Pleroma.Config.get(:app_layout, "app.html"))
40 # Marks a plug intentionally skipped and blocks its execution if it's present in plugs chain
41 defp skip_plug(conn, plug_module) do
43 plug_module.ensure_skippable()
45 UndefinedFunctionError ->
46 raise "#{plug_module} is not skippable. Append `use Pleroma.Web, :plug` to its code."
49 PlugHelper.append_to_skipped_plugs(conn, plug_module)
52 # Here we can apply before-action hooks (e.g. verify whether auth checks were preformed)
53 defp action(conn, params) do
54 if Pleroma.Plugs.AuthExpectedPlug.auth_expected?(conn) &&
55 not PlugHelper.plug_called_or_skipped?(conn, Pleroma.Plugs.OAuthScopesPlug) do
59 "Security violation: OAuth scopes check was neither handled nor explicitly skipped."
72 root: "lib/pleroma/web/templates",
73 namespace: Pleroma.Web
75 # Import convenience functions from controllers
76 import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]
78 import Pleroma.Web.ErrorHelpers
79 import Pleroma.Web.Gettext
80 import Pleroma.Web.Router.Helpers
84 @doc "Same as `render/3` but wrapped in a rescue block"
85 def safe_render(view, template, assigns \\ %{}) do
86 Phoenix.View.render(view, template, assigns)
90 "#{__MODULE__} failed to render #{inspect({view, template})}\n" <>
91 Exception.format(:error, error, __STACKTRACE__)
98 Same as `render_many/4` but wrapped in rescue block.
100 def safe_render_many(collection, view, template, assigns \\ %{}) do
101 Enum.map(collection, fn resource ->
102 as = Map.get(assigns, :as) || view.__resource__
103 assigns = Map.put(assigns, as, resource)
104 safe_render(view, template, assigns)
114 # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
116 import Phoenix.Controller
122 # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
124 import Pleroma.Web.Gettext
130 alias Pleroma.Plugs.PlugHelper
132 def ensure_skippable, do: :noop
135 @doc "If marked as skipped, returns `conn`, and calls `perform/2` otherwise."
136 def call(%Plug.Conn{} = conn, options) do
137 if PlugHelper.plug_skipped?(conn, __MODULE__) do
141 |> PlugHelper.append_to_called_plugs(__MODULE__)
149 When used, dispatch to the appropriate controller/view/etc.
151 defmacro __using__(which) when is_atom(which) do
152 apply(__MODULE__, which, [])
156 Pleroma.Web.Endpoint.url()