use Ecto.Schema
alias Pleroma.Activity
+ alias Pleroma.Bookmark
alias Pleroma.Notification
alias Pleroma.Object
alias Pleroma.Repo
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.
)
)
|> preload([activity, object], object: object)
+ |> preload(:bookmarks)
end
def get_by_ap_id(ap_id) do
activity.data,
activity.data
),
- preload: [object: o]
+ preload: [object: o],
+ preload: :bookmarks
)
)
end
activity.data,
activity.data
),
- preload: [object: o]
+ preload: [object: o],
+ preload: :bookmarks
)
|> Repo.one()
end
activity.data,
activity.data
),
- preload: [object: o]
+ preload: [object: o],
+ preload: :bookmarks
)
end
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)
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
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