2018-12-23 20:04:54 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2018-12-31 15:41:47 +00:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 20:04:54 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-03-17 16:09:58 +00:00
|
|
|
defmodule Pleroma.Web do
|
|
|
|
@moduledoc """
|
|
|
|
A module that keeps using definitions for controllers,
|
|
|
|
views and so on.
|
|
|
|
|
|
|
|
This can be used in your application as:
|
|
|
|
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
use Pleroma.Web, :view
|
|
|
|
|
|
|
|
The definitions below will be executed for every view,
|
|
|
|
controller, etc, so keep them short and clean, focused
|
|
|
|
on imports, uses and aliases.
|
|
|
|
|
|
|
|
Do NOT define functions inside the quoted expressions
|
|
|
|
below.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def controller do
|
|
|
|
quote do
|
|
|
|
use Phoenix.Controller, namespace: Pleroma.Web
|
|
|
|
import Plug.Conn
|
2019-02-09 15:20:18 +00:00
|
|
|
import Pleroma.Web.Gettext
|
|
|
|
import Pleroma.Web.Router.Helpers
|
2019-02-21 15:55:19 +00:00
|
|
|
|
2019-02-22 08:10:17 +00:00
|
|
|
plug(:put_layout, Application.get_env(:pleroma, :app_layout, "app.html"))
|
2017-03-17 16:09:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def view do
|
|
|
|
quote do
|
2018-03-30 14:01:53 +01:00
|
|
|
use Phoenix.View,
|
|
|
|
root: "lib/pleroma/web/templates",
|
|
|
|
namespace: Pleroma.Web
|
2017-03-17 16:09:58 +00:00
|
|
|
|
|
|
|
# Import convenience functions from controllers
|
|
|
|
import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]
|
|
|
|
|
2019-02-09 15:20:18 +00:00
|
|
|
import Pleroma.Web.ErrorHelpers
|
|
|
|
import Pleroma.Web.Gettext
|
|
|
|
import Pleroma.Web.Router.Helpers
|
2019-01-27 18:15:35 +00:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
@doc "Same as `render/3` but wrapped in a rescue block"
|
|
|
|
def safe_render(view, template, assigns \\ %{}) do
|
|
|
|
Phoenix.View.render(view, template, assigns)
|
|
|
|
rescue
|
|
|
|
error ->
|
|
|
|
Logger.error(
|
|
|
|
"#{__MODULE__} failed to render #{inspect({view, template})}: #{inspect(error)}"
|
|
|
|
)
|
|
|
|
|
|
|
|
Logger.error(inspect(__STACKTRACE__))
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Same as `render_many/4` but wrapped in rescue block.
|
|
|
|
"""
|
|
|
|
def safe_render_many(collection, view, template, assigns \\ %{}) do
|
|
|
|
Enum.map(collection, fn resource ->
|
|
|
|
as = Map.get(assigns, :as) || view.__resource__
|
|
|
|
assigns = Map.put(assigns, as, resource)
|
|
|
|
safe_render(view, template, assigns)
|
|
|
|
end)
|
|
|
|
|> Enum.filter(& &1)
|
|
|
|
end
|
2017-03-17 16:09:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def router do
|
|
|
|
quote do
|
|
|
|
use Phoenix.Router
|
2019-02-08 16:18:44 +00:00
|
|
|
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
|
2017-03-17 16:09:58 +00:00
|
|
|
import Plug.Conn
|
|
|
|
import Phoenix.Controller
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def channel do
|
|
|
|
quote do
|
2019-02-08 16:18:44 +00:00
|
|
|
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
|
2017-03-17 16:09:58 +00:00
|
|
|
use Phoenix.Channel
|
|
|
|
import Pleroma.Web.Gettext
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
When used, dispatch to the appropriate controller/view/etc.
|
|
|
|
"""
|
|
|
|
defmacro __using__(which) when is_atom(which) do
|
|
|
|
apply(__MODULE__, which, [])
|
|
|
|
end
|
2017-03-30 13:32:36 +01:00
|
|
|
|
|
|
|
def base_url do
|
2018-03-30 14:01:53 +01:00
|
|
|
Pleroma.Web.Endpoint.url()
|
2017-03-30 13:32:36 +01:00
|
|
|
end
|
2017-03-17 16:09:58 +00:00
|
|
|
end
|