mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-11-10 02:05:12 +00:00
Preload bookmarks wherever the object is preloaded
This commit is contained in:
parent
06947c9147
commit
f841eb7cdb
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue