Preload bookmarks wherever the object is preloaded
authorrinpatch <rinpatch@sdf.org>
Sat, 4 May 2019 09:46:42 +0000 (12:46 +0300)
committerWilliam Pitcock <nenolod@dereferenced.org>
Tue, 7 May 2019 19:33:22 +0000 (19:33 +0000)
lib/pleroma/activity.ex
lib/pleroma/web/activity_pub/activity_pub.ex
test/activity_test.exs

index 73e63bb144b6e83ef5cce904ade31ca6e452a0a5..7845c264a22a1255d634fea1510b055775d5fe51 100644 (file)
@@ -6,6 +6,7 @@ defmodule Pleroma.Activity do
   use Ecto.Schema
 
   alias Pleroma.Activity
+  alias Pleroma.Bookmark
   alias Pleroma.Notification
   alias Pleroma.Object
   alias Pleroma.Repo
@@ -36,6 +37,7 @@ defmodule Pleroma.Activity do
     field(:actor, :string)
     field(:recipients, {:array, :string}, default: [])
     has_many(:notifications, Notification, on_delete: :delete_all)
+    has_many(:bookmarks, Bookmark, on_delete: :delete_all)
 
     # Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
     # The foreign key is embedded in a jsonb field.
@@ -71,6 +73,7 @@ defmodule Pleroma.Activity do
         )
     )
     |> preload([activity, object], object: object)
+    |> preload(:bookmarks)
   end
 
   def get_by_ap_id(ap_id) do
@@ -102,7 +105,8 @@ defmodule Pleroma.Activity do
             activity.data,
             activity.data
           ),
-        preload: [object: o]
+        preload: [object: o],
+        preload: :bookmarks
       )
     )
   end
@@ -122,7 +126,8 @@ defmodule Pleroma.Activity do
           activity.data,
           activity.data
         ),
-      preload: [object: o]
+      preload: [object: o],
+      preload: :bookmarks
     )
     |> Repo.one()
   end
@@ -200,7 +205,8 @@ defmodule Pleroma.Activity do
           activity.data,
           activity.data
         ),
-      preload: [object: o]
+      preload: [object: o],
+      preload: :bookmarks
     )
   end
 
index 6c737d0a47302ac9903fd18d5c60dc92649e993d..bd2544470df295b2fc435d838269c13286080915 100644 (file)
@@ -137,6 +137,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
           activity
         end
 
+      activity =
+        if activity.data["type"] in ["Create", "Announce"] do
+          Repo.preload(activity, :bookmarks)
+        else
+          activity
+        end
+
       Task.start(fn ->
         Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
       end)
index ad889f544830a5ed8463312461c5ee35bdd3b7af..e2a8baada672a8a1be45d5bf1a663b7735ce8daa 100644 (file)
@@ -5,6 +5,8 @@
 defmodule Pleroma.ActivityTest do
   use Pleroma.DataCase
   alias Pleroma.Activity
+  alias Pleroma.Bookmark
+  alias Pleroma.Object
   import Pleroma.Factory
 
   test "returns an activity by it's AP id" do
@@ -28,4 +30,31 @@ defmodule Pleroma.ActivityTest do
 
     assert activity == found_activity
   end
+
+  test "preloading object preloads bookmarks" do
+    user1 = insert(:user)
+    user2 = insert(:user)
+    activity = insert(:note_activity)
+    {:ok, bookmark1} = Bookmark.create(user1.id, activity.id)
+    {:ok, bookmark2} = Bookmark.create(user2.id, activity.id)
+    bookmarks = Enum.sort([bookmark1, bookmark2])
+
+    queried_activity =
+      Ecto.Query.from(a in Activity, where: a.id == ^activity.id)
+      |> Activity.with_preloaded_object()
+      |> Repo.one()
+
+    assert Enum.sort(queried_activity.bookmarks) == bookmarks
+
+    queried_activity = Activity.get_by_ap_id_with_object(activity.data["id"])
+    assert Enum.sort(queried_activity.bookmarks) == bookmarks
+
+    queried_activity = Activity.get_by_id_with_object(activity.id)
+    assert Enum.sort(queried_activity.bookmarks) == bookmarks
+
+    queried_activity =
+      Activity.get_create_by_object_ap_id_with_object(Object.normalize(activity).data["id"])
+
+    assert Enum.sort(queried_activity.bookmarks) == bookmarks
+  end
 end