mirror of
https://git.pleroma.social/pleroma/pleroma.git
synced 2024-06-10 09:19:34 +00:00
Static FE
This commit is contained in:
parent
eeafcb9579
commit
f6c543a82a
|
@ -646,8 +646,6 @@ config :pleroma, Pleroma.ActivityExpiration, enabled: true
|
|||
|
||||
config :pleroma, Pleroma.Plugs.RemoteIp, enabled: true
|
||||
|
||||
config :pleroma, :static_fe, enabled: false
|
||||
|
||||
# Example of frontend configuration
|
||||
# This example will make us serve the primary frontend from the
|
||||
# frontends directory within your `:pleroma, :instance, static_dir`.
|
||||
|
|
|
@ -3151,21 +3151,6 @@ config :pleroma, :config_description, [
|
|||
}
|
||||
]
|
||||
},
|
||||
%{
|
||||
group: :pleroma,
|
||||
key: :static_fe,
|
||||
label: "Static FE",
|
||||
type: :group,
|
||||
description:
|
||||
"Render profiles and posts using server-generated HTML that is viewable without using JavaScript",
|
||||
children: [
|
||||
%{
|
||||
key: :enabled,
|
||||
type: :boolean,
|
||||
description: "Enables the rendering of static HTML. Default: disabled."
|
||||
}
|
||||
]
|
||||
},
|
||||
%{
|
||||
group: :pleroma,
|
||||
key: :feed,
|
||||
|
|
|
@ -9,6 +9,7 @@ defmodule Pleroma.Frontend do
|
|||
|
||||
def get_config(:primary) do
|
||||
primary_fe_config = Pleroma.Config.get([:frontends, :primary], %{"name" => "pleroma"})
|
||||
static_enabled? = Pleroma.Config.get([:frontends, :static], false)
|
||||
|
||||
{config, controller} =
|
||||
if primary_fe_config["name"] == "none" do
|
||||
|
@ -21,7 +22,7 @@ defmodule Pleroma.Frontend do
|
|||
])}
|
||||
end
|
||||
|
||||
%{"config" => config, "controller" => controller}
|
||||
%{"config" => config, "controller" => controller, "static" => static_enabled?}
|
||||
end
|
||||
|
||||
@spec file_path(String.t(), frontend_kind()) :: {:ok, String.t()} | {:error, String.t()}
|
||||
|
|
|
@ -4,23 +4,25 @@
|
|||
|
||||
defmodule Pleroma.Plugs.StaticFEPlug do
|
||||
import Plug.Conn
|
||||
alias Pleroma.Web.StaticFE.StaticFEController
|
||||
|
||||
def init(options), do: options
|
||||
|
||||
def call(conn, _) do
|
||||
if enabled?() and requires_html?(conn) do
|
||||
def call(%{private: %{frontend: %{"static" => true}}} = conn, _) do
|
||||
action = Phoenix.Controller.action_name(conn)
|
||||
|
||||
if requires_html?(conn) and has_action?(action) do
|
||||
conn
|
||||
|> StaticFEController.call(:show)
|
||||
|> Pleroma.Web.FrontendController.call(action)
|
||||
|> halt()
|
||||
else
|
||||
conn
|
||||
end
|
||||
end
|
||||
|
||||
defp enabled?, do: Pleroma.Config.get([:static_fe, :enabled], false)
|
||||
def call(conn, _), do: conn
|
||||
|
||||
defp requires_html?(conn) do
|
||||
Phoenix.Controller.get_format(conn) == "html"
|
||||
end
|
||||
defp requires_html?(conn), do: Phoenix.Controller.get_format(conn) == "html"
|
||||
|
||||
defp has_action?(action),
|
||||
do: function_exported?(Pleroma.Web.Frontend.StaticController, action, 2)
|
||||
end
|
||||
|
|
|
@ -1,8 +1,4 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.StaticFE.StaticFEController do
|
||||
defmodule Pleroma.Web.Frontend.StaticController do
|
||||
use Pleroma.Web, :controller
|
||||
|
||||
alias Pleroma.Activity
|
||||
|
@ -11,11 +7,8 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do
|
|||
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||
alias Pleroma.Web.ActivityPub.Visibility
|
||||
alias Pleroma.Web.Metadata
|
||||
alias Pleroma.Web.Router.Helpers
|
||||
|
||||
plug(:put_layout, :static_fe)
|
||||
plug(:put_view, Pleroma.Web.StaticFE.StaticFEView)
|
||||
plug(:assign_id)
|
||||
|
||||
plug(Pleroma.Plugs.EnsureAuthenticatedPlug,
|
||||
unless_func: &Pleroma.Web.FederatingPlug.federating?/1
|
||||
|
@ -23,65 +16,20 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do
|
|||
|
||||
@page_keys ["max_id", "min_id", "limit", "since_id", "order"]
|
||||
|
||||
defp get_title(%Object{data: %{"name" => name}}) when is_binary(name),
|
||||
do: name
|
||||
def object(conn, %{"uuid" => _uuid}) do
|
||||
url = url(conn) <> conn.request_path
|
||||
|
||||
defp get_title(%Object{data: %{"summary" => summary}}) when is_binary(summary),
|
||||
do: summary
|
||||
case Activity.get_create_by_object_ap_id_with_object(url) do
|
||||
%Activity{} = activity ->
|
||||
to = o_status_path(Pleroma.Web.Endpoint, :notice, activity)
|
||||
redirect(conn, to: to)
|
||||
|
||||
defp get_title(_), do: nil
|
||||
|
||||
defp not_found(conn, message) do
|
||||
conn
|
||||
|> put_status(404)
|
||||
|> render("error.html", %{message: message, meta: ""})
|
||||
_ ->
|
||||
not_found(conn, "Post not found.")
|
||||
end
|
||||
end
|
||||
|
||||
defp get_counts(%Activity{} = activity) do
|
||||
%Object{data: data} = Object.normalize(activity)
|
||||
|
||||
%{
|
||||
likes: data["like_count"] || 0,
|
||||
replies: data["repliesCount"] || 0,
|
||||
announces: data["announcement_count"] || 0
|
||||
}
|
||||
end
|
||||
|
||||
defp represent(%Activity{} = activity), do: represent(activity, false)
|
||||
|
||||
defp represent(%Activity{object: %Object{data: data}} = activity, selected) do
|
||||
{:ok, user} = User.get_or_fetch(activity.object.data["actor"])
|
||||
|
||||
link =
|
||||
case user.local do
|
||||
true -> Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity)
|
||||
_ -> data["url"] || data["external_url"] || data["id"]
|
||||
end
|
||||
|
||||
content =
|
||||
if data["content"] do
|
||||
data["content"]
|
||||
|> Pleroma.HTML.filter_tags()
|
||||
|> Pleroma.Emoji.Formatter.emojify(Map.get(data, "emoji", %{}))
|
||||
else
|
||||
nil
|
||||
end
|
||||
|
||||
%{
|
||||
user: User.sanitize_html(user),
|
||||
title: get_title(activity.object),
|
||||
content: content,
|
||||
attachment: data["attachment"],
|
||||
link: link,
|
||||
published: data["published"],
|
||||
sensitive: data["sensitive"],
|
||||
selected: selected,
|
||||
counts: get_counts(activity),
|
||||
id: activity.id
|
||||
}
|
||||
end
|
||||
|
||||
def show(%{assigns: %{notice_id: notice_id}} = conn, _params) do
|
||||
def notice(conn, %{"id" => notice_id}) do
|
||||
with %Activity{local: true} = activity <-
|
||||
Activity.get_by_id_with_object(notice_id),
|
||||
true <- Visibility.is_public?(activity.object),
|
||||
|
@ -106,7 +54,7 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do
|
|||
end
|
||||
end
|
||||
|
||||
def show(%{assigns: %{username_or_id: username_or_id}} = conn, params) do
|
||||
def feed_redirect(conn, %{"nickname" => username_or_id} = params) do
|
||||
case User.get_cached_by_nickname_or_id(username_or_id) do
|
||||
%User{} = user ->
|
||||
meta = Metadata.build_tags(%{user: user})
|
||||
|
@ -140,25 +88,12 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do
|
|||
end
|
||||
end
|
||||
|
||||
def show(%{assigns: %{object_id: _}} = conn, _params) do
|
||||
url = Helpers.url(conn) <> conn.request_path
|
||||
|
||||
case Activity.get_create_by_object_ap_id_with_object(url) do
|
||||
%Activity{} = activity ->
|
||||
to = Helpers.o_status_path(Pleroma.Web.Endpoint, :notice, activity)
|
||||
redirect(conn, to: to)
|
||||
|
||||
_ ->
|
||||
not_found(conn, "Post not found.")
|
||||
end
|
||||
end
|
||||
|
||||
def show(%{assigns: %{activity_id: _}} = conn, _params) do
|
||||
url = Helpers.url(conn) <> conn.request_path
|
||||
def activity(conn, %{"uuid" => _uuid}) do
|
||||
url = url(conn) <> conn.request_path
|
||||
|
||||
case Activity.get_by_ap_id(url) do
|
||||
%Activity{} = activity ->
|
||||
to = Helpers.o_status_path(Pleroma.Web.Endpoint, :notice, activity)
|
||||
to = o_status_path(Pleroma.Web.Endpoint, :notice, activity)
|
||||
redirect(conn, to: to)
|
||||
|
||||
_ ->
|
||||
|
@ -166,17 +101,60 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do
|
|||
end
|
||||
end
|
||||
|
||||
defp assign_id(%{path_info: ["notice", notice_id]} = conn, _opts),
|
||||
do: assign(conn, :notice_id, notice_id)
|
||||
defp get_title(%Object{data: %{"name" => name}}) when is_binary(name),
|
||||
do: name
|
||||
|
||||
defp assign_id(%{path_info: ["users", user_id]} = conn, _opts),
|
||||
do: assign(conn, :username_or_id, user_id)
|
||||
defp get_title(%Object{data: %{"summary" => summary}}) when is_binary(summary),
|
||||
do: summary
|
||||
|
||||
defp assign_id(%{path_info: ["objects", object_id]} = conn, _opts),
|
||||
do: assign(conn, :object_id, object_id)
|
||||
defp get_title(_), do: nil
|
||||
|
||||
defp assign_id(%{path_info: ["activities", activity_id]} = conn, _opts),
|
||||
do: assign(conn, :activity_id, activity_id)
|
||||
defp not_found(conn, message) do
|
||||
conn
|
||||
|> put_status(404)
|
||||
|> render("error.html", %{message: message, meta: ""})
|
||||
end
|
||||
|
||||
defp assign_id(conn, _opts), do: conn
|
||||
defp get_counts(%Activity{} = activity) do
|
||||
%Object{data: data} = Object.normalize(activity)
|
||||
|
||||
%{
|
||||
likes: data["like_count"] || 0,
|
||||
replies: data["repliesCount"] || 0,
|
||||
announces: data["announcement_count"] || 0
|
||||
}
|
||||
end
|
||||
|
||||
defp represent(%Activity{} = activity), do: represent(activity, false)
|
||||
|
||||
defp represent(%Activity{object: %Object{data: data}} = activity, selected) do
|
||||
{:ok, user} = User.get_or_fetch(activity.object.data["actor"])
|
||||
|
||||
link =
|
||||
if user.local do
|
||||
o_status_url(Pleroma.Web.Endpoint, :notice, activity)
|
||||
else
|
||||
data["url"] || data["external_url"] || data["id"]
|
||||
end
|
||||
|
||||
content =
|
||||
if data["content"] do
|
||||
data["content"]
|
||||
|> Pleroma.HTML.filter_tags()
|
||||
|> Pleroma.Emoji.Formatter.emojify(Map.get(data, "emoji", %{}))
|
||||
end
|
||||
|
||||
%{
|
||||
user: User.sanitize_html(user),
|
||||
title: get_title(activity.object),
|
||||
content: content,
|
||||
attachment: data["attachment"],
|
||||
link: link,
|
||||
published: data["published"],
|
||||
sensitive: data["sensitive"],
|
||||
selected: selected,
|
||||
counts: get_counts(activity),
|
||||
id: activity.id
|
||||
}
|
||||
end
|
||||
end
|
|
@ -120,6 +120,10 @@ defmodule Pleroma.Web.FrontendController do
|
|||
|
||||
{controller, action} =
|
||||
cond do
|
||||
fe_config["static"] &&
|
||||
function_exported?(Pleroma.Web.Frontend.StaticController, action_name, 2) ->
|
||||
{Pleroma.Web.Frontend.StaticController, action_name}
|
||||
|
||||
function_exported?(fe_config["controller"], action_name, 2) ->
|
||||
{fe_config["controller"], action_name}
|
||||
|
||||
|
|
|
@ -12,6 +12,8 @@ defmodule Pleroma.Web.Feed.UserController do
|
|||
|
||||
plug(Pleroma.Plugs.SetFormatPlug when action in [:feed_redirect])
|
||||
|
||||
plug(Pleroma.Plugs.StaticFEPlug)
|
||||
|
||||
action_fallback(:errors)
|
||||
|
||||
def feed_redirect(%{assigns: %{format: "html"}} = conn, %{"nickname" => nickname}) do
|
||||
|
|
|
@ -29,6 +29,8 @@ defmodule Pleroma.Web.OStatus.OStatusController do
|
|||
when action in [:object, :activity, :notice]
|
||||
)
|
||||
|
||||
plug(Pleroma.Plugs.StaticFEPlug)
|
||||
|
||||
action_fallback(:errors)
|
||||
|
||||
def object(%{assigns: %{format: format}} = conn, _params)
|
||||
|
|
|
@ -548,7 +548,6 @@ defmodule Pleroma.Web.Router do
|
|||
|
||||
pipeline :ostatus do
|
||||
plug(:accepts, ["html", "xml", "rss", "atom", "activity+json", "json"])
|
||||
plug(Pleroma.Plugs.StaticFEPlug)
|
||||
end
|
||||
|
||||
pipeline :oembed do
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.StaticFE.StaticFEView do
|
||||
defmodule Pleroma.Web.Frontend.StaticView do
|
||||
use Pleroma.Web, :view
|
||||
|
||||
alias Calendar.Strftime
|
|
@ -10,11 +10,12 @@ defmodule Pleroma.FrontendTest do
|
|||
test "Primary config" do
|
||||
config = %{"name" => "monsta", "ref" => "pika"}
|
||||
|
||||
clear_config([:frontends, :primary], config)
|
||||
clear_config(:frontends, %{primary: config, static: true})
|
||||
|
||||
fe_config = Pleroma.Frontend.get_config()
|
||||
assert fe_config["config"] == config
|
||||
assert fe_config["controller"] == Pleroma.Web.Frontend.MonstaController
|
||||
assert fe_config["static"] == true
|
||||
end
|
||||
|
||||
test "Headless" do
|
||||
|
@ -25,6 +26,7 @@ defmodule Pleroma.FrontendTest do
|
|||
fe_config = Pleroma.Frontend.get_config()
|
||||
assert fe_config["config"] == %{}
|
||||
assert fe_config["controller"] == Pleroma.Web.Frontend.HeadlessController
|
||||
assert fe_config["static"] == false
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
defmodule Pleroma.Web.StaticFE.StaticFEControllerTest do
|
||||
defmodule Pleroma.Web.Frontend.StaticControllerTest do
|
||||
use Pleroma.Web.ConnCase
|
||||
|
||||
alias Pleroma.Activity
|
||||
|
@ -8,7 +8,7 @@ defmodule Pleroma.Web.StaticFE.StaticFEControllerTest do
|
|||
|
||||
import Pleroma.Factory
|
||||
|
||||
setup_all do: clear_config([:static_fe, :enabled], true)
|
||||
setup_all do: clear_config([:frontends, :static], true)
|
||||
setup do: clear_config([:instance, :federating], true)
|
||||
|
||||
setup %{conn: conn} do
|
Loading…
Reference in a new issue