Migrate Pleroma.Web to phoenix 1.6 formats
This commit is contained in:
parent
6a333ade7f
commit
57e51fe62c
7 changed files with 81 additions and 67 deletions
|
@ -132,66 +132,6 @@ defmodule Pleroma.Web do
|
|||
end
|
||||
end
|
||||
|
||||
def view do
|
||||
quote do
|
||||
use Phoenix.View,
|
||||
root: "lib/pleroma/web/templates",
|
||||
namespace: Pleroma.Web
|
||||
|
||||
# Import convenience functions from controllers
|
||||
import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]
|
||||
|
||||
import Pleroma.Web.ErrorHelpers
|
||||
import Pleroma.Web.Gettext
|
||||
|
||||
alias Pleroma.Web.Router.Helpers, as: Routes
|
||||
|
||||
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})}\n" <>
|
||||
Exception.format(:error, error, __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
|
||||
end
|
||||
end
|
||||
|
||||
def router do
|
||||
quote do
|
||||
use Phoenix.Router
|
||||
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
|
||||
import Plug.Conn
|
||||
import Phoenix.Controller
|
||||
end
|
||||
end
|
||||
|
||||
def channel do
|
||||
quote do
|
||||
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
|
||||
import Phoenix.Channel
|
||||
import Pleroma.Web.Gettext
|
||||
end
|
||||
end
|
||||
|
||||
def plug do
|
||||
quote do
|
||||
@behaviour Pleroma.Web.Plug
|
||||
|
@ -236,6 +176,80 @@ defmodule Pleroma.Web do
|
|||
end
|
||||
end
|
||||
|
||||
def view do
|
||||
quote do
|
||||
use Phoenix.View,
|
||||
root: "lib/pleroma/web/templates",
|
||||
namespace: Pleroma.Web
|
||||
|
||||
# Import convenience functions from controllers
|
||||
import Phoenix.Controller,
|
||||
only: [get_flash: 1, get_flash: 2, view_module: 1, view_template: 1]
|
||||
|
||||
# Include shared imports and aliases for views
|
||||
unquote(view_helpers())
|
||||
end
|
||||
end
|
||||
|
||||
def live_view do
|
||||
quote do
|
||||
use Phoenix.LiveView,
|
||||
layout: {Pleroma.Web.LayoutView, "live.html"}
|
||||
|
||||
unquote(view_helpers())
|
||||
end
|
||||
end
|
||||
|
||||
def live_component do
|
||||
quote do
|
||||
use Phoenix.LiveComponent
|
||||
|
||||
unquote(view_helpers())
|
||||
end
|
||||
end
|
||||
|
||||
def component do
|
||||
quote do
|
||||
use Phoenix.Component
|
||||
|
||||
unquote(view_helpers())
|
||||
end
|
||||
end
|
||||
|
||||
def router do
|
||||
quote do
|
||||
use Phoenix.Router
|
||||
|
||||
import Plug.Conn
|
||||
import Phoenix.Controller
|
||||
import Phoenix.LiveView.Router
|
||||
end
|
||||
end
|
||||
|
||||
def channel do
|
||||
quote do
|
||||
use Phoenix.Channel
|
||||
import Pleroma.Web.Gettext
|
||||
end
|
||||
end
|
||||
|
||||
defp view_helpers do
|
||||
quote do
|
||||
# Use all HTML functionality (forms, tags, etc)
|
||||
use Phoenix.HTML
|
||||
|
||||
# Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc)
|
||||
import Phoenix.LiveView.Helpers
|
||||
|
||||
# Import basic rendering functionality (render, render_layout, etc)
|
||||
import Phoenix.View
|
||||
|
||||
import Pleroma.Web.ErrorHelpers
|
||||
import Pleroma.Web.Gettext
|
||||
alias Pleroma.Web.Router.Helpers, as: Routes
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
When used, dispatch to the appropriate controller/view/etc.
|
||||
"""
|
||||
|
|
|
@ -14,11 +14,11 @@ defmodule Pleroma.Web.AdminAPI.StatusView do
|
|||
defdelegate merge_account_views(user), to: AdminAPI.AccountView
|
||||
|
||||
def render("index.json", %{total: total} = opts) do
|
||||
%{total: total, activities: safe_render_many(opts.activities, __MODULE__, "show.json", opts)}
|
||||
%{total: total, activities: render_many(opts.activities, __MODULE__, "show.json", opts)}
|
||||
end
|
||||
|
||||
def render("index.json", opts) do
|
||||
safe_render_many(opts.activities, __MODULE__, "show.json", opts)
|
||||
render_many(opts.activities, __MODULE__, "show.json", opts)
|
||||
end
|
||||
|
||||
def render("show.json", %{activity: %{data: %{"object" => _object}} = activity} = opts) do
|
||||
|
|
|
@ -12,7 +12,7 @@ defmodule Pleroma.Web.MastodonAPI.ConversationView do
|
|||
alias Pleroma.Web.MastodonAPI.StatusView
|
||||
|
||||
def render("participations.json", %{participations: participations, for: user}) do
|
||||
safe_render_many(participations, __MODULE__, "participation.json", %{
|
||||
render_many(participations, __MODULE__, "participation.json", %{
|
||||
as: :participation,
|
||||
for: user
|
||||
})
|
||||
|
|
|
@ -66,7 +66,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do
|
|||
|> Map.put(:parent_activities, parent_activities)
|
||||
|> Map.put(:relationships, relationships_opt)
|
||||
|
||||
safe_render_many(notifications, NotificationView, "show.json", opts)
|
||||
render_many(notifications, NotificationView, "show.json", opts)
|
||||
end
|
||||
|
||||
def render(
|
||||
|
|
|
@ -131,7 +131,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
|
|||
|> Map.put(:parent_activities, parent_activities)
|
||||
|> Map.put(:relationships, relationships_opt)
|
||||
|
||||
safe_render_many(activities, StatusView, "show.json", opts)
|
||||
render_many(activities, StatusView, "show.json", opts)
|
||||
end
|
||||
|
||||
def render(
|
||||
|
|
|
@ -4,7 +4,7 @@ defmodule Pleroma.Web.MastodonAPI.TagView do
|
|||
alias Pleroma.Web.Router.Helpers
|
||||
|
||||
def render("index.json", %{tags: tags, for_user: user}) do
|
||||
safe_render_many(tags, __MODULE__, "show.json", %{for_user: user})
|
||||
render_many(tags, __MODULE__, "show.json", %{for_user: user})
|
||||
end
|
||||
|
||||
def render("show.json", %{tag: tag, for_user: user}) do
|
||||
|
|
|
@ -921,7 +921,7 @@ defmodule Pleroma.Web.Router do
|
|||
|
||||
# TODO: Change to Phoenix.Router.routes/1 for Phoenix 1.6.0+
|
||||
def get_api_routes do
|
||||
__MODULE__.__routes__()
|
||||
Phoenix.Router.routes(__MODULE__)
|
||||
|> Enum.reject(fn r -> r.plug == Pleroma.Web.Fallback.RedirectController end)
|
||||
|> Enum.map(fn r ->
|
||||
r.path
|
||||
|
|
Loading…
Reference in a new issue