From 569d9bac178f22e763afc9db5000415bebf70304 Mon Sep 17 00:00:00 2001
From: Roger Braun <roger@rogerbraun.net>
Date: Wed, 22 Mar 2017 14:45:17 +0100
Subject: [PATCH] Add fetching for recipients.

---
 lib/pleroma/web/activity_pub/activity_pub.ex | 16 ++++++++++++++--
 lib/pleroma/web/router.ex                    |  1 +
 test/web/activity_pub/activity_pub_test.exs  | 13 +++++++++++++
 3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex
index 5e4f3bbf5..121c46c51 100644
--- a/lib/pleroma/web/activity_pub/activity_pub.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub.ex
@@ -8,14 +8,26 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   end
 
   def fetch_public_activities(opts \\ %{}) do
+    public = ["https://www.w3.org/ns/activitystreams#Public"]
+    fetch_activities(public, opts)
+  end
+
+  def fetch_activities(recipients, 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]
 
+    query = Enum.reduce(recipients, query, fn (recipient, q) ->
+      map = %{ to: [recipient] }
+      from activity in q,
+      or_where: fragment(~s(? @> ?), activity.data, ^map)
+    end)
+
+    query = from activity in query,
+      where: activity.id > ^since_id
+
     Repo.all(query)
     |> Enum.reverse
   end
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index 0440af8c1..aa2f7865e 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -22,6 +22,7 @@ defmodule Pleroma.Web.Router do
   scope "/api", Pleroma.Web do
     pipe_through :api
     get "/statuses/public_timeline.json", TwitterAPI.Controller, :public_timeline
+    get "/statuses/public_and_external_timeline.json", TwitterAPI.Controller, :public_timeline
   end
 
   scope "/api", Pleroma.Web do
diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs
index ba157f191..754bd72f3 100644
--- a/test/web/activity_pub/activity_pub_test.exs
+++ b/test/web/activity_pub/activity_pub_test.exs
@@ -15,6 +15,19 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     end
   end
 
+
+  describe "fetch activities for recipients" do
+    test "retrieve the activities for certain recipients" do
+      {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
+      {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
+      {:ok, activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
+
+      activities = ActivityPub.fetch_activities(["someone", "someone_else"])
+      assert length(activities) == 2
+      assert activities == [activity_one, activity_two]
+    end
+  end
+
   describe "public fetch activities" do
     test "retrieves public activities" do
       %{public: public} = ActivityBuilder.public_and_non_public
-- 
GitLab