From b9d0e34506ed759dfe096242a62078940dc1a8bb Mon Sep 17 00:00:00 2001
From: Roger Braun <roger@rogerbraun.net>
Date: Tue, 21 Mar 2017 20:31:48 +0100
Subject: [PATCH] Add since_id for activitypub fetching.

---
 lib/pleroma/web/activity_pub/activity_pub.ex |  5 ++++-
 test/web/activity_pub/activity_pub_test.exs  | 13 +++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex
index 1f6ee4744..94e52a0fb 100644
--- a/lib/pleroma/web/activity_pub/activity_pub.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub.ex
@@ -7,9 +7,12 @@ def insert(map) when is_map(map) do
     Repo.insert(%Activity{data: map})
   end
 
-  def fetch_public_activities do
+  def fetch_public_activities(opts \\ %{}) do
+    since_id = opts[:since_id] || 0
+
     query = from activity in Activity,
       where: fragment(~s(? @> '{"to": ["https://www.w3.org/ns/activitystreams#Public"]}'), activity.data),
+      where: activity.id > ^since_id,
       limit: 20,
       order_by: [desc: :inserted_at]
 
diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs
index d640bfa2b..6f636d5d1 100644
--- a/test/web/activity_pub/activity_pub_test.exs
+++ b/test/web/activity_pub/activity_pub_test.exs
@@ -34,5 +34,18 @@ test "retrieves a maximum of 20 activities" do
       assert length(activities) == 20
       assert last == last_expected
     end
+
+    test "retrieves ids starting from a since_id" do
+      activities = ActivityBuilder.insert_list(30)
+      later_activities = ActivityBuilder.insert_list(10)
+      since_id = List.last(activities).id
+      last_expected = List.last(later_activities)
+
+      activities = ActivityPub.fetch_public_activities(%{since_id: since_id})
+      last = List.last(activities)
+
+      assert length(activities) == 10
+      assert last == last_expected
+    end
   end
 end