diff options
author | Roman Chvanikov <chvanikoff@pm.me> | 2020-07-31 12:28:38 +0300 |
---|---|---|
committer | Roman Chvanikov <chvanikoff@pm.me> | 2020-07-31 12:28:38 +0300 |
commit | f6c543a82a71610df87191b24934ffd6eacbe0ab (patch) | |
tree | 9ae983b0f31debe2a27e9ed2a69c7e18bfa2f81a /lib/pleroma | |
parent | eeafcb9579fe559e85441a5b7864f7671d4880e8 (diff) | |
download | pleroma-frontends/static.tar.gz |
Static FEfrontends/static
Diffstat (limited to 'lib/pleroma')
-rw-r--r-- | lib/pleroma/frontend.ex | 3 | ||||
-rw-r--r-- | lib/pleroma/plugs/static_fe_plug.ex | 18 | ||||
-rw-r--r-- | lib/pleroma/web/controller/frontend/static_controller.ex (renamed from lib/pleroma/web/static_fe/static_fe_controller.ex) | 150 | ||||
-rw-r--r-- | lib/pleroma/web/controller/frontend_controller.ex | 4 | ||||
-rw-r--r-- | lib/pleroma/web/feed/user_controller.ex | 2 | ||||
-rw-r--r-- | lib/pleroma/web/ostatus/ostatus_controller.ex | 2 | ||||
-rw-r--r-- | lib/pleroma/web/router.ex | 1 | ||||
-rw-r--r-- | lib/pleroma/web/templates/frontend/static/_attachment.html.eex (renamed from lib/pleroma/web/templates/static_fe/static_fe/_attachment.html.eex) | 0 | ||||
-rw-r--r-- | lib/pleroma/web/templates/frontend/static/_notice.html.eex (renamed from lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex) | 0 | ||||
-rw-r--r-- | lib/pleroma/web/templates/frontend/static/_user_card.html.eex (renamed from lib/pleroma/web/templates/static_fe/static_fe/_user_card.html.eex) | 0 | ||||
-rw-r--r-- | lib/pleroma/web/templates/frontend/static/conversation.html.eex (renamed from lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex) | 0 | ||||
-rw-r--r-- | lib/pleroma/web/templates/frontend/static/error.html.eex (renamed from lib/pleroma/web/templates/static_fe/static_fe/error.html.eex) | 0 | ||||
-rw-r--r-- | lib/pleroma/web/templates/frontend/static/profile.html.eex (renamed from lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex) | 0 | ||||
-rw-r--r-- | lib/pleroma/web/views/frontend/static_view.ex (renamed from lib/pleroma/web/static_fe/static_fe_view.ex) | 2 |
14 files changed, 85 insertions, 97 deletions
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 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 <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 - - defp get_title(%Object{data: %{"summary" => summary}}) when is_binary(summary), - do: summary - - defp get_title(_), do: nil - - defp not_found(conn, message) do - conn - |> put_status(404) - |> render("error.html", %{message: message, meta: ""}) - 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) + def object(conn, %{"uuid" => _uuid}) do + url = url(conn) <> conn.request_path - 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 + 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) - %{ - 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 - } + _ -> + not_found(conn, "Post not found.") + end 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,12 +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 + def activity(conn, %{"uuid" => _uuid}) do + url = url(conn) <> conn.request_path - case Activity.get_create_by_object_ap_id_with_object(url) do + 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) _ -> @@ -153,30 +101,60 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do end end - def show(%{assigns: %{activity_id: _}} = conn, _params) do - url = Helpers.url(conn) <> conn.request_path + defp get_title(%Object{data: %{"name" => name}}) when is_binary(name), + do: name - case Activity.get_by_ap_id(url) do - %Activity{} = activity -> - to = Helpers.o_status_path(Pleroma.Web.Endpoint, :notice, activity) - redirect(conn, to: to) + defp get_title(%Object{data: %{"summary" => summary}}) when is_binary(summary), + do: summary - _ -> - not_found(conn, "Post not found.") - end + defp get_title(_), do: nil + + defp not_found(conn, message) do + conn + |> put_status(404) + |> render("error.html", %{message: message, meta: ""}) end - defp assign_id(%{path_info: ["notice", notice_id]} = conn, _opts), - do: assign(conn, :notice_id, notice_id) + defp get_counts(%Activity{} = activity) do + %Object{data: data} = Object.normalize(activity) - defp assign_id(%{path_info: ["users", user_id]} = conn, _opts), - do: assign(conn, :username_or_id, user_id) + %{ + likes: data["like_count"] || 0, + replies: data["repliesCount"] || 0, + announces: data["announcement_count"] || 0 + } + end - defp assign_id(%{path_info: ["objects", object_id]} = conn, _opts), - do: assign(conn, :object_id, object_id) + 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 - defp assign_id(%{path_info: ["activities", activity_id]} = conn, _opts), - do: assign(conn, :activity_id, activity_id) + content = + if data["content"] do + data["content"] + |> Pleroma.HTML.filter_tags() + |> Pleroma.Emoji.Formatter.emojify(Map.get(data, "emoji", %{})) + end - defp assign_id(conn, _opts), do: conn + %{ + 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 index 4853e7f4b..4853e7f4b 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/_attachment.html.eex +++ b/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 index df0244795..df0244795 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex +++ b/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 index 977b894d3..977b894d3 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/_user_card.html.eex +++ b/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 index 2acd84828..2acd84828 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex +++ b/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 index d98a1eba7..d98a1eba7 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/error.html.eex +++ b/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 index 3191bf450..3191bf450 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex +++ b/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 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 <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 |