From: rinpatch Date: Sat, 4 May 2019 09:46:42 +0000 (+0300) Subject: Preload bookmarks wherever the object is preloaded X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=f841eb7cdb83afc444dfe260581b6be6e690a717;p=akkoma Preload bookmarks wherever the object is preloaded --- diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex index 73e63bb14..7845c264a 100644 --- a/lib/pleroma/activity.ex +++ b/lib/pleroma/activity.ex @@ -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 diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 6c737d0a4..bd2544470 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -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) diff --git a/test/activity_test.exs b/test/activity_test.exs index ad889f544..e2a8baada 100644 --- a/test/activity_test.exs +++ b/test/activity_test.exs @@ -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