static fe: use a generic activity representer to render activities
authorWilliam Pitcock <nenolod@dereferenced.org>
Sat, 2 Mar 2019 20:10:30 +0000 (20:10 +0000)
committerPhil Hagelberg <phil@hagelb.org>
Sun, 10 Nov 2019 02:06:51 +0000 (18:06 -0800)
lib/pleroma/web/static_fe/activity_representer.ex [new file with mode: 0644]
lib/pleroma/web/static_fe/static_fe_controller.ex

diff --git a/lib/pleroma/web/static_fe/activity_representer.ex b/lib/pleroma/web/static_fe/activity_representer.ex
new file mode 100644 (file)
index 0000000..93f8a88
--- /dev/null
@@ -0,0 +1,52 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.StaticFE.ActivityRepresenter do
+  alias Pleroma.Activity
+  alias Pleroma.Object
+  alias Pleroma.User
+  alias Pleroma.Web.ActivityPub.Visibility
+
+  def prepare_activity(%User{} = user, %Object{} = object) do
+    %{}
+    |> set_user(user)
+    |> set_object(object)
+    |> set_title(object)
+    |> set_content(object)
+    |> set_attachments(object)
+  end
+
+  defp set_user(data, %User{} = user), do: Map.put(data, :user, user)
+
+  defp set_object(data, %Object{} = object), do: Map.put(data, :object, object)
+
+  defp set_title(data, %Object{data: %{"name" => name}}) when is_binary(name),
+    do: Map.put(data, :title, name)
+
+  defp set_title(data, %Object{data: %{"summary" => summary}}) when is_binary(summary),
+    do: Map.put(data, :title, summary)
+
+  defp set_title(data, _), do: Map.put(data, :title, nil)
+
+  defp set_content(data, %Object{data: %{"content" => content}}) when is_binary(content),
+    do: Map.put(data, :content, content)
+
+  defp set_content(data, _), do: Map.put(data, :content, nil)
+
+  # TODO: attachments
+  defp set_attachments(data, _), do: Map.put(data, :attachments, [])
+
+  def represent(activity_id) do
+    with %Activity{data: %{"type" => "Create"}} = activity <- Activity.get_by_id(activity_id),
+         true <- Visibility.is_public?(activity),
+         %Object{} = object <- Object.normalize(activity.data["object"]),
+         %User{} = user <- User.get_or_fetch(activity.data["actor"]),
+         data <- prepare_activity(user, object) do
+      {:ok, data}
+    else
+      e ->
+        {:error, e}
+    end
+  end
+end
index 2ac8577590a4d9bf086174842d947d5d084d2070..7d7cb6ddd55d77aac23004f1b2c6d8628d7b0dfd 100644 (file)
@@ -5,24 +5,17 @@
 defmodule Pleroma.Web.StaticFE.StaticFEController do
   use Pleroma.Web, :controller
 
-  alias Pleroma.Repo
-  alias Pleroma.Activity
-  alias Pleroma.Object
-  alias Pleroma.User
-  alias Pleroma.Web.ActivityPub.Visibility
+  alias Pleroma.Web.StaticFE.ActivityRepresenter
 
   require Logger
 
   def show_notice(conn, %{"notice_id" => notice_id}) do
-    with %Activity{} = activity <- Repo.get(Activity, notice_id),
-         true <- Visibility.is_public?(activity),
-         %User{} = user <- User.get_or_fetch(activity.data["actor"]),
-         %Object{} = object <- Object.normalize(activity.data["object"]) do
+    with {:ok, data} <- ActivityRepresenter.represent(notice_id) do
       conn
       |> put_layout(:static_fe)
       |> put_status(200)
       |> put_view(Pleroma.Web.StaticFE.StaticFEView)
-      |> render("notice.html", %{notice: activity, object: object, user: user})
+      |> render("notice.html", data)
     else
       _ ->
         conn