Make many of the improvements suggested in review.
authorPhil Hagelberg <phil@hagelb.org>
Tue, 5 Nov 2019 06:02:10 +0000 (22:02 -0800)
committerPhil Hagelberg <phil@hagelb.org>
Sun, 10 Nov 2019 02:08:08 +0000 (18:08 -0800)
lib/pleroma/plugs/static_fe_plug.ex
lib/pleroma/web/static_fe/static_fe_controller.ex
lib/pleroma/web/static_fe/static_fe_view.ex
lib/pleroma/web/templates/layout/static_fe.html.eex
lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex
lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex

index dcbabc9df00ff8ee07354afe39d52cca0359043e..2af45e52a61071c8a39aa2638916e27db1b1aa10 100644 (file)
@@ -3,17 +3,24 @@
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.Plugs.StaticFEPlug do
-  def init(options), do: options
+  import Plug.Conn
+  alias Pleroma.Web.StaticFE.StaticFEController
 
-  def accepts_html?({"accept", a}), do: String.contains?(a, "text/html")
-  def accepts_html?({_, _}), do: false
+  def init(options), do: options
 
   def call(conn, _) do
-    with true <- Pleroma.Config.get([:instance, :static_fe], false),
-         {_, _} <- Enum.find(conn.req_headers, &accepts_html?/1) do
-      Pleroma.Web.StaticFE.StaticFEController.call(conn, :show)
+    if enabled?() and accepts_html?(conn) do
+      conn
+      |> StaticFEController.call(:show)
+      |> halt()
     else
-      _ -> conn
+      conn
     end
   end
+
+  defp enabled?, do: Pleroma.Config.get([:instance, :static_fe], false)
+
+  defp accepts_html?(conn) do
+    conn |> get_req_header("accept") |> List.first() |> String.contains?("text/html")
+  end
 end
index 96e30f31758fce9b6f7c9e3eecfa9494fc1a2447..a00c6db4fc383d7b823fea7bf7c87684ace3fb78 100644 (file)
@@ -58,28 +58,24 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do
   end
 
   def show(%{assigns: %{notice_id: notice_id}} = conn, _params) do
-    instance_name = Pleroma.Config.get([:instance, :name], "Pleroma")
     activity = Activity.get_by_id_with_object(notice_id)
-    context = activity.object.data["context"]
-    activities = ActivityPub.fetch_activities_for_context(context, %{})
-
     timeline =
-      for a <- Enum.reverse(activities) do
-        represent(a, a.object.id == activity.object.id)
-      end
+      activity.object.data["context"]
+      |> ActivityPub.fetch_activities_for_context(%{})
+      |> Enum.reverse()
+      |> Enum.map(&represent(&1, &1.object.id == activity.object.id))
 
-    render(conn, "conversation.html", %{activities: timeline, instance_name: instance_name})
+    render(conn, "conversation.html", %{activities: timeline})
   end
 
   def show(%{assigns: %{username_or_id: username_or_id}} = conn, _params) do
-    instance_name = Pleroma.Config.get([:instance, :name], "Pleroma")
     %User{} = user = User.get_cached_by_nickname_or_id(username_or_id)
 
     timeline =
       ActivityPub.fetch_user_activities(user, nil, %{})
       |> Enum.map(&represent/1)
 
-    render(conn, "profile.html", %{user: user, timeline: timeline, instance_name: instance_name})
+    render(conn, "profile.html", %{user: user, timeline: timeline})
   end
 
   def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts),
index 160261af9e0b4f55cba7f1bd954aa8c95e9a2438..5612a06bb0b01121e543e017ad00000554bb98a4 100644 (file)
@@ -36,4 +36,6 @@ defmodule Pleroma.Web.StaticFE.StaticFEView do
     {:ok, date, _} = DateTime.from_iso8601(date)
     Strftime.strftime!(date, "%Y/%m/%d %l:%M:%S %p UTC")
   end
+
+  def instance_name, do: Pleroma.Config.get([:instance, :name], "Pleroma")
 end
index 62b59f17c53595d8204675a4685e2d4912831d91..4b889bb192af9c93b6442371eecba405510e27a5 100644 (file)
@@ -4,7 +4,7 @@
     <meta charset="utf-8" />
     <meta name="viewport" content="width=device-width,initial-scale=1,minimal-ui" />
     <title>
-    <%= Application.get_env(:pleroma, :instance)[:name] %>
+    <%= Pleroma.Config.get([:instance, :name]) %>
     </title>
     <link rel="stylesheet" href="/static/static-fe.css">
   </head>
index 7ac4a9e5f72260a4c4c16815a576a291f3291868..2acd84828f2056df5424fd4becd5bcea29c38268 100644 (file)
@@ -1,5 +1,5 @@
 <header>
-  <h1><%= link @instance_name, to: "/" %></h1>
+  <h1><%= link instance_name(), to: "/" %></h1>
 </header>
 
 <main>
index 9b3d0509e7b698f0c4d281530df6fa4ed56a74a8..fa3df3b4ef9acf70f30557df43c989424a19d38c 100644 (file)
@@ -1,5 +1,5 @@
 <header>
-  <h1><%= link @instance_name, to: "/" %></h1>
+  <h1><%= link instance_name(), to: "/" %></h1>
 
   <h3>
     <form class="pull-right collapse" method="POST" action="<%= Helpers.util_path(@conn, :remote_subscribe) %>">