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.skip_plug(conn)
45 UndefinedFunctionError ->
46 raise "#{plug_module} is not skippable. Append `use Pleroma.Web, :plug` to its code."
50 # Executed just before actual controller action, invokes before-action hooks (callbacks)
51 defp action(conn, params) do
52 with %Plug.Conn{halted: false} <- maybe_halt_on_missing_oauth_scopes_check(conn) do
57 # Halts if authenticated API action neither performs nor explicitly skips OAuth scopes check
58 defp maybe_halt_on_missing_oauth_scopes_check(conn) do
59 if Pleroma.Plugs.AuthExpectedPlug.auth_expected?(conn) &&
60 not PlugHelper.plug_called_or_skipped?(conn, Pleroma.Plugs.OAuthScopesPlug) do
64 "Security violation: OAuth scopes check was neither handled nor explicitly skipped."
77 root: "lib/pleroma/web/templates",
78 namespace: Pleroma.Web
80 # Import convenience functions from controllers
81 import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]
83 import Pleroma.Web.ErrorHelpers
84 import Pleroma.Web.Gettext
85 import Pleroma.Web.Router.Helpers
89 @doc "Same as `render/3` but wrapped in a rescue block"
90 def safe_render(view, template, assigns \\ %{}) do
91 Phoenix.View.render(view, template, assigns)
95 "#{__MODULE__} failed to render #{inspect({view, template})}\n" <>
96 Exception.format(:error, error, __STACKTRACE__)
103 Same as `render_many/4` but wrapped in rescue block.
105 def safe_render_many(collection, view, template, assigns \\ %{}) do
106 Enum.map(collection, fn resource ->
107 as = Map.get(assigns, :as) || view.__resource__
108 assigns = Map.put(assigns, as, resource)
109 safe_render(view, template, assigns)
119 # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
121 import Phoenix.Controller
127 # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
129 import Pleroma.Web.Gettext
135 alias Pleroma.Plugs.PlugHelper
138 Marks a plug intentionally skipped and blocks its execution if it's present in plugs chain.
140 def skip_plug(conn) do
141 PlugHelper.append_to_private_list(
143 PlugHelper.skipped_plugs_list_id(),
149 @doc "If marked as skipped, returns `conn`, and calls `perform/2` otherwise."
150 def call(%Plug.Conn{} = conn, options) do
151 if PlugHelper.plug_skipped?(conn, __MODULE__) do
155 |> PlugHelper.append_to_private_list(PlugHelper.called_plugs_list_id(), __MODULE__)
163 When used, dispatch to the appropriate controller/view/etc.
165 defmacro __using__(which) when is_atom(which) do
166 apply(__MODULE__, which, [])
170 Pleroma.Web.Endpoint.url()