Preload bookmarks wherever the object is preloaded

This commit is contained in:
rinpatch 2019-05-04 12:46:42 +03:00 committed by William Pitcock
parent 06947c9147
commit f841eb7cdb
3 changed files with 45 additions and 3 deletions

View file

@ -6,6 +6,7 @@ defmodule Pleroma.Activity do
use Ecto.Schema use Ecto.Schema
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Bookmark
alias Pleroma.Notification alias Pleroma.Notification
alias Pleroma.Object alias Pleroma.Object
alias Pleroma.Repo alias Pleroma.Repo
@ -36,6 +37,7 @@ defmodule Pleroma.Activity do
field(:actor, :string) field(:actor, :string)
field(:recipients, {:array, :string}, default: []) field(:recipients, {:array, :string}, default: [])
has_many(:notifications, Notification, on_delete: :delete_all) 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! # 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. # The foreign key is embedded in a jsonb field.
@ -71,6 +73,7 @@ defmodule Pleroma.Activity do
) )
) )
|> preload([activity, object], object: object) |> preload([activity, object], object: object)
|> preload(:bookmarks)
end end
def get_by_ap_id(ap_id) do def get_by_ap_id(ap_id) do
@ -102,7 +105,8 @@ defmodule Pleroma.Activity do
activity.data, activity.data,
activity.data activity.data
), ),
preload: [object: o] preload: [object: o],
preload: :bookmarks
) )
) )
end end
@ -122,7 +126,8 @@ defmodule Pleroma.Activity do
activity.data, activity.data,
activity.data activity.data
), ),
preload: [object: o] preload: [object: o],
preload: :bookmarks
) )
|> Repo.one() |> Repo.one()
end end
@ -200,7 +205,8 @@ defmodule Pleroma.Activity do
activity.data, activity.data,
activity.data activity.data
), ),
preload: [object: o] preload: [object: o],
preload: :bookmarks
) )
end end

View file

@ -137,6 +137,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
activity activity
end end
activity =
if activity.data["type"] in ["Create", "Announce"] do
Repo.preload(activity, :bookmarks)
else
activity
end
Task.start(fn -> Task.start(fn ->
Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity) Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
end) end)

View file

@ -5,6 +5,8 @@
defmodule Pleroma.ActivityTest do defmodule Pleroma.ActivityTest do
use Pleroma.DataCase use Pleroma.DataCase
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Bookmark
alias Pleroma.Object
import Pleroma.Factory import Pleroma.Factory
test "returns an activity by it's AP id" do test "returns an activity by it's AP id" do
@ -28,4 +30,31 @@ defmodule Pleroma.ActivityTest do
assert activity == found_activity assert activity == found_activity
end 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 end