Add limiting to activity pub fetching.
authorRoger Braun <roger@rogerbraun.net>
Tue, 21 Mar 2017 19:22:05 +0000 (20:22 +0100)
committerRoger Braun <roger@rogerbraun.net>
Tue, 21 Mar 2017 19:22:05 +0000 (20:22 +0100)
lib/pleroma/web/activity_pub/activity_pub.ex
test/support/builders/activity_builder.ex
test/web/activity_pub/activity_pub_test.exs

index 0317b4cd5000e2d1db826901255c5def340a77c3..1f6ee47448ab9de0632b2cc052b7c63435d3cf03 100644 (file)
@@ -9,8 +9,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
 
   def fetch_public_activities do
     query = from activity in Activity,
-    where: fragment(~s(? @> '{"to": ["https://www.w3.org/ns/activitystreams#Public"]}'), activity.data)
+      where: fragment(~s(? @> '{"to": ["https://www.w3.org/ns/activitystreams#Public"]}'), activity.data),
+      limit: 20,
+      order_by: [desc: :inserted_at]
 
     Repo.all(query)
+    |> Enum.reverse
   end
 end
index 0b7f79c5f32f7d70b6a4e1df63dc5603451eb33a..b44afa896899c93e1496f40adcd74a7c39f79ed3 100644 (file)
@@ -1,11 +1,10 @@
 defmodule Pleroma.Builders.ActivityBuilder do
   alias Pleroma.Builders.UserBuilder
   alias Pleroma.Web.ActivityPub.ActivityPub
-  alias Pleroma.User
 
-  def public_and_non_public do
-    {:ok, user} = UserBuilder.insert
-    public = %{
+  def build(data \\ %{}, opts \\ %{}) do
+    user = opts[:user] || UserBuilder.build
+    activity = %{
       "id" => 1,
       "actor" => user.ap_id,
       "to" => ["https://www.w3.org/ns/activitystreams#Public"],
@@ -14,16 +13,26 @@ defmodule Pleroma.Builders.ActivityBuilder do
         "content" => "test"
       }
     }
+    Map.merge(activity, data)
+  end
 
-    non_public = %{
-      "id" => 2,
-      "actor" => user.ap_id,
-      "to" => [],
-      "object" => %{
-        "type" => "Note",
-        "content" => "test"
-      }
-    }
+  def insert(data \\ %{}, opts \\ %{}) do
+    activity = build(data, opts)
+    ActivityPub.insert(activity)
+  end
+
+  def insert_list(times, data \\ %{}, opts \\ %{}) do
+    Enum.map(1..times, fn (n) ->
+      {:ok, activity} = insert(%{"id" => n})
+      activity
+    end)
+  end
+
+  def public_and_non_public do
+    {:ok, user} = UserBuilder.insert
+
+    public = build(%{"id" => 1}, %{user: user})
+    non_public = build(%{"id" => 2, "to" => []}, %{user: user})
 
     {:ok, public} = ActivityPub.insert(public)
     {:ok, non_public} = ActivityPub.insert(non_public)
index aa4abbec19b0a212a9f706a70a51e111cb6fd1e9..d640bfa2bf9fc0c6fb3df342f01b5dc9c33499b2 100644 (file)
@@ -15,13 +15,24 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     end
   end
 
-  describe "fetch activities" do
-    test "retrieves all public activities" do
+  describe "public fetch activities" do
+    test "retrieves public activities" do
       %{public: public} = ActivityBuilder.public_and_non_public
 
       activities = ActivityPub.fetch_public_activities
       assert length(activities) == 1
       assert Enum.at(activities, 0) == public
     end
+
+    test "retrieves a maximum of 20 activities" do
+      activities = ActivityBuilder.insert_list(30)
+      last_expected = List.last(activities)
+
+      activities = ActivityPub.fetch_public_activities
+      last = List.last(activities)
+
+      assert length(activities) == 20
+      assert last == last_expected
+    end
   end
 end