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
34 defp set_put_layout(conn, _) do
35 put_layout(conn, Pleroma.Config.get(:app_layout, "app.html"))
43 root: "lib/pleroma/web/templates",
44 namespace: Pleroma.Web
46 # Import convenience functions from controllers
47 import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]
49 import Pleroma.Web.ErrorHelpers
50 import Pleroma.Web.Gettext
51 import Pleroma.Web.Router.Helpers
55 @doc "Same as `render/3` but wrapped in a rescue block"
56 def safe_render(view, template, assigns \\ %{}) do
57 Phoenix.View.render(view, template, assigns)
61 "#{__MODULE__} failed to render #{inspect({view, template})}\n" <>
62 Exception.format(:error, error, __STACKTRACE__)
69 Same as `render_many/4` but wrapped in rescue block.
71 def safe_render_many(collection, view, template, assigns \\ %{}) do
72 Enum.map(collection, fn resource ->
73 as = Map.get(assigns, :as) || view.__resource__
74 assigns = Map.put(assigns, as, resource)
75 safe_render(view, template, assigns)
85 # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
87 import Phoenix.Controller
93 # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
95 import Pleroma.Web.Gettext
100 When used, dispatch to the appropriate controller/view/etc.
102 defmacro __using__(which) when is_atom(which) do
103 apply(__MODULE__, which, [])
107 Pleroma.Web.Endpoint.url()