AP C2S: allow limit & order on outbox & read_inbox
authorHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Fri, 8 May 2020 01:05:56 +0000 (03:05 +0200)
committerHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Fri, 29 May 2020 14:14:45 +0000 (16:14 +0200)
lib/pleroma/web/activity_pub/activity_pub_controller.ex
lib/pleroma/web/controller_helper.ex
test/web/activity_pub/activity_pub_controller_test.exs

index b624d4255c0f631833e951bd8ddb1c15ad00080b..5b84413848ce5c39746bf153a7481d9473efcc4e 100644 (file)
@@ -231,28 +231,22 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
       when page? in [true, "true"] do
     with %User{} = user <- User.get_cached_by_nickname(nickname),
          {:ok, user} <- User.ensure_keys_present(user) do
-      activities =
-        if params["max_id"] do
-          ActivityPub.fetch_user_activities(user, for_user, %{
-            "max_id" => params["max_id"],
-            # This is a hack because postgres generates inefficient queries when filtering by
-            # 'Answer', poll votes will be hidden by the visibility filter in this case anyway
-            "include_poll_votes" => true,
-            "limit" => 10
-          })
-        else
-          ActivityPub.fetch_user_activities(user, for_user, %{
-            "limit" => 10,
-            "include_poll_votes" => true
-          })
-        end
+      # "include_poll_votes" is a hack because postgres generates inefficient
+      # queries when filtering by 'Answer', poll votes will be hidden by the
+      # visibility filter in this case anyway
+      params =
+        params
+        |> Map.drop(["nickname", "page"])
+        |> Map.put("include_poll_votes", true)
+
+      activities = ActivityPub.fetch_user_activities(user, for_user, params)
 
       conn
       |> put_resp_content_type("application/activity+json")
       |> put_view(UserView)
       |> render("activity_collection_page.json", %{
         activities: activities,
-        pagination: ControllerHelper.get_pagination_fields(conn, activities, %{"limit" => "10"}),
+        pagination: ControllerHelper.get_pagination_fields(conn, activities),
         iri: "#{user.ap_id}/outbox"
       })
     end
@@ -355,22 +349,23 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
         %{"nickname" => nickname, "page" => page?} = params
       )
       when page? in [true, "true"] do
+    params =
+      params
+      |> Map.drop(["nickname", "page"])
+      |> Map.put("blocking_user", user)
+      |> Map.put("user", user)
+
     activities =
-      if params["max_id"] do
-        ActivityPub.fetch_activities([user.ap_id | User.following(user)], %{
-          "max_id" => params["max_id"],
-          "limit" => 10
-        })
-      else
-        ActivityPub.fetch_activities([user.ap_id | User.following(user)], %{"limit" => 10})
-      end
+      [user.ap_id | User.following(user)]
+      |> ActivityPub.fetch_activities(params)
+      |> Enum.reverse()
 
     conn
     |> put_resp_content_type("application/activity+json")
     |> put_view(UserView)
     |> render("activity_collection_page.json", %{
       activities: activities,
-      pagination: ControllerHelper.get_pagination_fields(conn, activities, %{"limit" => "10"}),
+      pagination: ControllerHelper.get_pagination_fields(conn, activities),
       iri: "#{user.ap_id}/inbox"
     })
   end
index 2d35bb56cddd3e5b568b16a587e9f3eab2b91ab6..9e5444817bc9c8c82f21b8b4467b923e5c17d50c 100644 (file)
@@ -63,8 +63,8 @@ defmodule Pleroma.Web.ControllerHelper do
         params =
           conn.params
           |> Map.drop(Map.keys(conn.path_params))
-          |> Map.drop(["since_id", "max_id", "min_id"])
           |> Map.merge(extra_params)
+          |> Map.drop(Pagination.page_keys() -- ["limit", "order"])
 
         min_id =
           activities
index 3f48553c98a1fbbbe69598a09bb329047c7092f4..e490a5744fc75dace9a97e5c9a622e46cad8fb68 100644 (file)
@@ -810,7 +810,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
       outbox_endpoint = user.ap_id <> "/outbox"
 
       _posts =
-        for i <- 0..15 do
+        for i <- 0..25 do
           {:ok, activity} = CommonAPI.post(user, %{status: "post #{i}"})
           activity
         end
@@ -822,8 +822,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
         |> json_response(200)
 
       result_ids = Enum.map(result["orderedItems"], fn x -> x["id"] end)
-      assert length(result["orderedItems"]) == 10
-      assert length(result_ids) == 10
+      assert length(result["orderedItems"]) == 20
+      assert length(result_ids) == 20
       assert result["next"]
       assert String.starts_with?(result["next"], outbox_endpoint)