Move view logic from StatusController.context to StatusView and add a test
authorEgor Kislitsyn <egor@kislitsyn.com>
Thu, 26 Sep 2019 07:28:35 +0000 (14:28 +0700)
committerEgor Kislitsyn <egor@kislitsyn.com>
Fri, 27 Sep 2019 03:52:47 +0000 (10:52 +0700)
lib/pleroma/web/mastodon_api/controllers/status_controller.ex
lib/pleroma/web/mastodon_api/views/status_view.ex
test/web/mastodon_api/controllers/status_controller_test.exs

index 89869bda097785cb38f9122ce8f218f97fe3a222..f7da10289f5c41df835d3bc37cbc14ed8c787590 100644 (file)
@@ -21,7 +21,6 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
   alias Pleroma.Web.CommonAPI
   alias Pleroma.Web.MastodonAPI.AccountView
   alias Pleroma.Web.MastodonAPI.ScheduledActivityView
-  alias Pleroma.Web.MastodonAPI.StatusView
 
   @rate_limited_status_actions ~w(reblog unreblog favourite unfavourite create delete)a
 
@@ -264,31 +263,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
           "exclude_id" => activity.id
         })
 
-      # TODO: Move to view
-      grouped_activities = Enum.group_by(activities, fn %{id: id} -> id < activity.id end)
-
-      result = %{
-        ancestors:
-          StatusView.render(
-            "index.json",
-            for: user,
-            activities: grouped_activities[true] || [],
-            as: :activity
-          )
-          |> Enum.reverse(),
-        # credo:disable-for-previous-line Credo.Check.Refactor.PipeChainStart
-        descendants:
-          StatusView.render(
-            "index.json",
-            for: user,
-            activities: grouped_activities[false] || [],
-            as: :activity
-          )
-          |> Enum.reverse()
-        # credo:disable-for-previous-line Credo.Check.Refactor.PipeChainStart
-      }
-
-      json(conn, result)
+      render(conn, "context.json", activity: activity, activities: activities, user: user)
     end
   end
 end
index 59bef30f2aa7998fac1213e5bd933a862ecaeee6..715d40766604bf8a8d2dd103728fcb93681c2558 100644 (file)
@@ -439,6 +439,20 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     end
   end
 
+  def render("context.json", %{activity: activity, activities: activities, user: user}) do
+    %{ancestors: ancestors, descendants: descendants} =
+      activities
+      |> Enum.reverse()
+      |> Enum.group_by(fn %{id: id} -> if id < activity.id, do: :ancestors, else: :descendants end)
+      |> Map.put_new(:ancestors, [])
+      |> Map.put_new(:descendants, [])
+
+    %{
+      ancestors: render("index.json", for: user, activities: ancestors, as: :activity),
+      descendants: render("index.json", for: user, activities: descendants, as: :activity)
+    }
+  end
+
   def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
     object = Object.normalize(activity)
 
index f80ce77045433a5969364e2868002ca0bd6cc367..14c7bd6d98c38120731cd2d2d51a8786f07929fe 100644 (file)
@@ -1053,4 +1053,25 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
       assert [] == response
     end
   end
+
+  test "context" do
+    user = insert(:user)
+
+    {:ok, %{id: id1}} = CommonAPI.post(user, %{"status" => "1"})
+    {:ok, %{id: id2}} = CommonAPI.post(user, %{"status" => "2", "in_reply_to_status_id" => id1})
+    {:ok, %{id: id3}} = CommonAPI.post(user, %{"status" => "3", "in_reply_to_status_id" => id2})
+    {:ok, %{id: id4}} = CommonAPI.post(user, %{"status" => "4", "in_reply_to_status_id" => id3})
+    {:ok, %{id: id5}} = CommonAPI.post(user, %{"status" => "5", "in_reply_to_status_id" => id4})
+
+    response =
+      build_conn()
+      |> assign(:user, nil)
+      |> get("/api/v1/statuses/#{id3}/context")
+      |> json_response(:ok)
+
+    assert %{
+             "ancestors" => [%{"id" => ^id1}, %{"id" => ^id2}],
+             "descendants" => [%{"id" => ^id4}, %{"id" => ^id5}]
+           } = response
+  end
 end