Apply all suggested changes from reviewers.
authorPhil Hagelberg <phil@hagelb.org>
Wed, 30 Oct 2019 00:45:26 +0000 (17:45 -0700)
committerPhil Hagelberg <phil@hagelb.org>
Sun, 10 Nov 2019 02:07:50 +0000 (18:07 -0800)
lib/pleroma/web/ostatus/ostatus_controller.ex
lib/pleroma/web/static_fe/activity_representer.ex
lib/pleroma/web/static_fe/static_fe_controller.ex
lib/pleroma/web/static_fe/static_fe_view.ex
lib/pleroma/web/static_fe/user_representer.ex
lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex

index ab5fdbc785d73250567de53b0c5fc4b072113182..be275977e933432f20980e5928910b7751809b9d 100644 (file)
@@ -77,7 +77,7 @@ defmodule Pleroma.Web.OStatus.OStatusController do
 
   def notice(%{assigns: %{format: format}} = conn, %{"id" => id}) do
     if Pleroma.Config.get([:instance, :static_fe], false) do
-      Pleroma.Web.StaticFE.StaticFEController.show(conn, %{"notice_id" => id})
+      Pleroma.Web.StaticFE.StaticFEController.call(conn, :show_notice)
     else
       with {_, %Activity{} = activity} <- {:activity, Activity.get_by_id_with_object(id)},
            {_, true} <- {:public?, Visibility.is_public?(activity)},
index 7b7e1730cc87adebe969735468e56ba712b879aa..8a499195c1e9e6035001b1b1ff26873168a24de0 100644 (file)
@@ -62,8 +62,8 @@ defmodule Pleroma.Web.StaticFE.ActivityRepresenter do
          {:ok, %User{} = user} <- User.get_or_fetch(activity.data["actor"]) do
       {:ok, prepare_activity(user, activity)}
     else
-      e ->
-        {:error, e}
+      {:error, reason} -> {:error, reason}
+      _error -> {:error, "Not found"}
     end
   end
 end
index d2b55767d5aa3776fe3311155f09e697c5b1ad6e..6e8d0d622e462134774028c8b7800c210ef01c32 100644 (file)
@@ -9,9 +9,12 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do
   alias Pleroma.Web.StaticFE.ActivityRepresenter
   alias Pleroma.Web.StaticFE.UserRepresenter
 
-  require Logger
+  plug(:put_layout, :static_fe)
+  plug(:put_view, Pleroma.Web.StaticFE.StaticFEView)
+  plug(:assign_id)
+  action_fallback(:not_found)
 
-  def show_notice(conn, %{"notice_id" => notice_id}) do
+  def show_notice(%{assigns: %{notice_id: notice_id}} = conn, _params) do
     with {:ok, data} <- ActivityRepresenter.represent(notice_id) do
       context = data.object.data["context"]
       activities = ActivityPub.fetch_activities_for_context(context, %{})
@@ -22,45 +25,29 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do
           |> Map.put(:selected, a.object.id == data.object.id)
         end
 
-      conn
-      |> put_layout(:static_fe)
-      |> put_status(200)
-      |> put_view(Pleroma.Web.StaticFE.StaticFEView)
-      |> render("conversation.html", %{data: data})
-    else
-      {:error, nil} ->
-        conn
-        |> put_status(404)
-        |> text("Not found")
+      render(conn, "conversation.html", data: data)
     end
   end
 
-  def show_user(conn, %{"username_or_id" => username_or_id}) do
+  def show_user(%{assigns: %{username_or_id: username_or_id}} = conn, _params) do
     with {:ok, data} <- UserRepresenter.represent(username_or_id) do
-      conn
-      |> put_layout(:static_fe)
-      |> put_status(200)
-      |> put_view(Pleroma.Web.StaticFE.StaticFEView)
-      |> render("profile.html", %{data: data})
-    else
-      {:error, nil} ->
-        conn
-        |> put_status(404)
-        |> text("Not found")
+      render(conn, "profile.html", data: data)
     end
   end
 
-  def show(%{path_info: ["notice", notice_id]} = conn, _params),
-    do: show_notice(conn, %{"notice_id" => notice_id})
+  def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts),
+    do: assign(conn, :notice_id, notice_id)
 
-  def show(%{path_info: ["users", user_id]} = conn, _params),
-    do: show_user(conn, %{"username_or_id" => user_id})
+  def assign_id(%{path_info: ["users", user_id]} = conn, _opts),
+    do: assign(conn, :username_or_id, user_id)
 
-  def show(%{path_info: [user_id]} = conn, _params),
-    do: show_user(conn, %{"username_or_id" => user_id})
+  def assign_id(%{path_info: [user_id]} = conn, _opts),
+    do: assign(conn, :username_or_id, user_id)
+
+  def assign_id(conn, _opts), do: conn
 
   # Fallback for unhandled types
-  def show(conn, _params) do
+  def not_found(conn, _opts) do
     conn
     |> put_status(404)
     |> text("Not found")
index 2b3b968d3ff6a0e33c8f14a75ab8846d49a3e332..d633751dd1102632990a16bf6b96775f0b4e367b 100644 (file)
@@ -11,19 +11,20 @@ defmodule Pleroma.Web.StaticFE.StaticFEView do
   alias Pleroma.Web.Metadata.Utils
   alias Pleroma.Web.Router.Helpers
 
-  import Phoenix.HTML
+  use Phoenix.HTML
 
   @media_types ["image", "audio", "video"]
 
   def emoji_for_user(%User{} = user) do
-    (user.source_data["tag"] || [])
+    user.source_data
+    |> Map.get("tag", [])
     |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
     |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
       {String.trim(name, ":"), url}
     end)
   end
 
-  def fetch_media_type(url) do
-    Utils.fetch_media_type(@media_types, url["mediaType"])
+  def fetch_media_type(%{"mediaType" => mediaType}) do
+    Utils.fetch_media_type(@media_types, mediaType)
   end
 end
index 9d2f1eb85e8850803871bc26a6632c9be31ece3c..26320ea69366e9604021bb4ec3ace3ad637f92af 100644 (file)
@@ -24,12 +24,9 @@ defmodule Pleroma.Web.StaticFE.UserRepresenter do
   end
 
   def represent(username_or_id) do
-    with %User{} = user <- User.get_cached_by_nickname_or_id(username_or_id),
-         data <- prepare_user(user) do
-      {:ok, data}
-    else
-      e ->
-        {:error, e}
+    case User.get_cached_by_nickname_or_id(username_or_id) do
+      %User{} = user -> {:ok, prepare_user(user)}
+      nil -> {:error, "User not found"}
     end
   end
 end
index 90b5ef67c7007b8ec7ec964406ee77dcd2653d4b..ed43ae838ae3fc5273f34dc8926bb6e869680675 100644 (file)
@@ -1,6 +1,7 @@
 <div class="activity" <%= if @data[:selected] do %> id="selected" <% end %>>
   <p class="pull-right">
-    <a href="<%= @data.link %>" class="activity-link"><%= @data.published %></a></p>
+    <%= link @data.published, to: @data.link, class: "activity-link" %>
+  </p>
   <%= render("user_card.html", %{user: @data.user}) %>
   <div class="activity-content">
     <%= if @data.title != "" do %>