1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
27 import Pleroma.Web.Gettext
28 import Pleroma.Web.Router.Helpers
35 root: "lib/pleroma/web/templates",
36 namespace: Pleroma.Web
38 # Import convenience functions from controllers
39 import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]
41 import Pleroma.Web.ErrorHelpers
42 import Pleroma.Web.Gettext
43 import Pleroma.Web.Router.Helpers
47 @doc "Same as `render/3` but wrapped in a rescue block"
48 def safe_render(view, template, assigns \\ %{}) do
49 Phoenix.View.render(view, template, assigns)
53 "#{__MODULE__} failed to render #{inspect({view, template})}: #{inspect(error)}"
56 Logger.error(inspect(__STACKTRACE__))
61 Same as `render_many/4` but wrapped in rescue block.
63 def safe_render_many(collection, view, template, assigns \\ %{}) do
64 Enum.map(collection, fn resource ->
65 as = Map.get(assigns, :as) || view.__resource__
66 assigns = Map.put(assigns, as, resource)
67 safe_render(view, template, assigns)
77 # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
79 import Phoenix.Controller
85 # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
87 import Pleroma.Web.Gettext
92 When used, dispatch to the appropriate controller/view/etc.
94 defmacro __using__(which) when is_atom(which) do
95 apply(__MODULE__, which, [])
99 Pleroma.Web.Endpoint.url()