From f6c543a82a71610df87191b24934ffd6eacbe0ab Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Fri, 31 Jul 2020 12:28:38 +0300 Subject: [PATCH] Static FE --- config/config.exs | 2 - config/description.exs | 15 -- lib/pleroma/frontend.ex | 3 +- lib/pleroma/plugs/static_fe_plug.ex | 18 +- .../frontend/static_controller.ex} | 156 ++++++++---------- .../web/controller/frontend_controller.ex | 4 + lib/pleroma/web/feed/user_controller.ex | 2 + lib/pleroma/web/ostatus/ostatus_controller.ex | 2 + lib/pleroma/web/router.ex | 1 - .../static}/_attachment.html.eex | 0 .../static}/_notice.html.eex | 0 .../static}/_user_card.html.eex | 0 .../static}/conversation.html.eex | 0 .../static}/error.html.eex | 0 .../static}/profile.html.eex | 0 .../frontend/static_view.ex} | 2 +- test/frontend_test.exs | 4 +- .../frontend/static_controller_test.exs} | 4 +- 18 files changed, 93 insertions(+), 120 deletions(-) rename lib/pleroma/web/{static_fe/static_fe_controller.ex => controller/frontend/static_controller.ex} (72%) rename lib/pleroma/web/templates/{static_fe/static_fe => frontend/static}/_attachment.html.eex (100%) rename lib/pleroma/web/templates/{static_fe/static_fe => frontend/static}/_notice.html.eex (100%) rename lib/pleroma/web/templates/{static_fe/static_fe => frontend/static}/_user_card.html.eex (100%) rename lib/pleroma/web/templates/{static_fe/static_fe => frontend/static}/conversation.html.eex (100%) rename lib/pleroma/web/templates/{static_fe/static_fe => frontend/static}/error.html.eex (100%) rename lib/pleroma/web/templates/{static_fe/static_fe => frontend/static}/profile.html.eex (100%) rename lib/pleroma/web/{static_fe/static_fe_view.ex => views/frontend/static_view.ex} (95%) rename test/web/{static_fe/static_fe_controller_test.exs => controllers/frontend/static_controller_test.exs} (98%) diff --git a/config/config.exs b/config/config.exs index 857e0afbb..dbb1622aa 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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`. diff --git a/config/description.exs b/config/description.exs index 11fbe0d78..277348d42 100644 --- a/config/description.exs +++ b/config/description.exs @@ -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, diff --git a/lib/pleroma/frontend.ex b/lib/pleroma/frontend.ex index f6aa3629f..9aa27cbeb 100644 --- a/lib/pleroma/frontend.ex +++ b/lib/pleroma/frontend.ex @@ -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()} diff --git a/lib/pleroma/plugs/static_fe_plug.ex b/lib/pleroma/plugs/static_fe_plug.ex index 143665c71..ea8d5ab0c 100644 --- a/lib/pleroma/plugs/static_fe_plug.ex +++ b/lib/pleroma/plugs/static_fe_plug.ex @@ -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 diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/controller/frontend/static_controller.ex similarity index 72% rename from lib/pleroma/web/static_fe/static_fe_controller.ex rename to lib/pleroma/web/controller/frontend/static_controller.ex index a7a891b13..947134968 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/controller/frontend/static_controller.ex @@ -1,8 +1,4 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors -# 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 diff --git a/lib/pleroma/web/controller/frontend_controller.ex b/lib/pleroma/web/controller/frontend_controller.ex index ce04934fe..c77fcc6e4 100644 --- a/lib/pleroma/web/controller/frontend_controller.ex +++ b/lib/pleroma/web/controller/frontend_controller.ex @@ -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} diff --git a/lib/pleroma/web/feed/user_controller.ex b/lib/pleroma/web/feed/user_controller.ex index 7118c087c..4a1f1cfce 100644 --- a/lib/pleroma/web/feed/user_controller.ex +++ b/lib/pleroma/web/feed/user_controller.ex @@ -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 diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex index a1b8a3adc..2ded7eac3 100644 --- a/lib/pleroma/web/ostatus/ostatus_controller.ex +++ b/lib/pleroma/web/ostatus/ostatus_controller.ex @@ -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) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 1e3ecf298..1755d0082 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -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 diff --git a/lib/pleroma/web/templates/static_fe/static_fe/_attachment.html.eex b/lib/pleroma/web/templates/frontend/static/_attachment.html.eex similarity index 100% rename from lib/pleroma/web/templates/static_fe/static_fe/_attachment.html.eex rename to lib/pleroma/web/templates/frontend/static/_attachment.html.eex diff --git a/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex b/lib/pleroma/web/templates/frontend/static/_notice.html.eex similarity index 100% rename from lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex rename to lib/pleroma/web/templates/frontend/static/_notice.html.eex diff --git a/lib/pleroma/web/templates/static_fe/static_fe/_user_card.html.eex b/lib/pleroma/web/templates/frontend/static/_user_card.html.eex similarity index 100% rename from lib/pleroma/web/templates/static_fe/static_fe/_user_card.html.eex rename to lib/pleroma/web/templates/frontend/static/_user_card.html.eex diff --git a/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex b/lib/pleroma/web/templates/frontend/static/conversation.html.eex similarity index 100% rename from lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex rename to lib/pleroma/web/templates/frontend/static/conversation.html.eex diff --git a/lib/pleroma/web/templates/static_fe/static_fe/error.html.eex b/lib/pleroma/web/templates/frontend/static/error.html.eex similarity index 100% rename from lib/pleroma/web/templates/static_fe/static_fe/error.html.eex rename to lib/pleroma/web/templates/frontend/static/error.html.eex diff --git a/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex b/lib/pleroma/web/templates/frontend/static/profile.html.eex similarity index 100% rename from lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex rename to lib/pleroma/web/templates/frontend/static/profile.html.eex diff --git a/lib/pleroma/web/static_fe/static_fe_view.ex b/lib/pleroma/web/views/frontend/static_view.ex similarity index 95% rename from lib/pleroma/web/static_fe/static_fe_view.ex rename to lib/pleroma/web/views/frontend/static_view.ex index b3d1d1ec8..7263e45db 100644 --- a/lib/pleroma/web/static_fe/static_fe_view.ex +++ b/lib/pleroma/web/views/frontend/static_view.ex @@ -2,7 +2,7 @@ # Copyright © 2017-2020 Pleroma Authors # 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 diff --git a/test/frontend_test.exs b/test/frontend_test.exs index b5f04d10a..ddd4b5ee0 100644 --- a/test/frontend_test.exs +++ b/test/frontend_test.exs @@ -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 diff --git a/test/web/static_fe/static_fe_controller_test.exs b/test/web/controllers/frontend/static_controller_test.exs similarity index 98% rename from test/web/static_fe/static_fe_controller_test.exs rename to test/web/controllers/frontend/static_controller_test.exs index 1598bf675..b36ae2b28 100644 --- a/test/web/static_fe/static_fe_controller_test.exs +++ b/test/web/controllers/frontend/static_controller_test.exs @@ -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