- Actually use preloaded bookmarks in views

- Preload bookmarks in bookmark timeline
- Rework bookmark preload tests
This commit is contained in:
rinpatch 2019-05-04 13:42:54 +03:00 committed by William Pitcock
parent f841eb7cdb
commit 3a7c14645e
6 changed files with 77 additions and 28 deletions

View File

@ -73,6 +73,11 @@ defmodule Pleroma.Activity do
) )
) )
|> preload([activity, object], object: object) |> preload([activity, object], object: object)
|> with_preloaded_bookmarks()
end
def with_preloaded_bookmarks(query) do
query
|> preload(:bookmarks) |> preload(:bookmarks)
end end
@ -105,9 +110,9 @@ defmodule Pleroma.Activity do
activity.data, activity.data,
activity.data activity.data
), ),
preload: [object: o], preload: [object: o]
preload: :bookmarks
) )
|> with_preloaded_bookmarks()
) )
end end
@ -126,9 +131,9 @@ defmodule Pleroma.Activity do
activity.data, activity.data,
activity.data activity.data
), ),
preload: [object: o], preload: [object: o]
preload: :bookmarks
) )
|> with_preloaded_bookmarks()
|> Repo.one() |> Repo.one()
end end

View File

@ -38,7 +38,8 @@ defmodule Pleroma.Bookmark do
Bookmark Bookmark
|> where(user_id: ^user_id) |> where(user_id: ^user_id)
|> join(:inner, [b], activity in assoc(b, :activity)) |> join(:inner, [b], activity in assoc(b, :activity))
|> preload([b, a], activity: a) |> join(:inner, [_b, a], bookmarks in assoc(a, :bookmarks))
|> preload([b, a, b2], activity: {a, bookmarks: b2})
end end
def get(user_id, activity_id) do def get(user_id, activity_id) do

View File

@ -563,7 +563,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
with %Activity{} = activity <- Activity.get_by_id_with_object(id), with %Activity{} = activity <- Activity.get_by_id_with_object(id),
%User{} = user <- User.get_cached_by_nickname(user.nickname), %User{} = user <- User.get_cached_by_nickname(user.nickname),
true <- Visibility.visible_for_user?(activity, user), true <- Visibility.visible_for_user?(activity, user),
{:ok, _bookmark} <- Bookmark.create(user.id, activity.id) do {:ok, bookmark} <- Bookmark.create(user.id, activity.id) do
activity = %{activity | bookmarks: [bookmark | activity.bookmarks]}
conn conn
|> put_view(StatusView) |> put_view(StatusView)
|> try_render("status.json", %{activity: activity, for: user, as: :activity}) |> try_render("status.json", %{activity: activity, for: user, as: :activity})
@ -575,6 +577,11 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
%User{} = user <- User.get_cached_by_nickname(user.nickname), %User{} = user <- User.get_cached_by_nickname(user.nickname),
true <- Visibility.visible_for_user?(activity, user), true <- Visibility.visible_for_user?(activity, user),
{:ok, _bookmark} <- Bookmark.destroy(user.id, activity.id) do {:ok, _bookmark} <- Bookmark.destroy(user.id, activity.id) do
bookmarks =
Enum.filter(activity.bookmarks, fn %{user_id: user_id} -> user_id != user.id end)
activity = %{activity | bookmarks: bookmarks}
conn conn
|> put_view(StatusView) |> put_view(StatusView)
|> try_render("status.json", %{activity: activity, for: user, as: :activity}) |> try_render("status.json", %{activity: activity, for: user, as: :activity})

View File

@ -80,13 +80,21 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
user = get_user(activity.data["actor"]) user = get_user(activity.data["actor"])
created_at = Utils.to_masto_date(activity.data["published"]) created_at = Utils.to_masto_date(activity.data["published"])
reblogged_activity = Activity.get_create_by_object_ap_id(object) reblogged_activity =
Activity.create_by_object_ap_id(object)
|> Activity.with_preloaded_bookmarks()
|> Repo.one()
reblogged = render("status.json", Map.put(opts, :activity, reblogged_activity)) reblogged = render("status.json", Map.put(opts, :activity, reblogged_activity))
activity_object = Object.normalize(activity) activity_object = Object.normalize(activity)
favorited = opts[:for] && opts[:for].ap_id in (activity_object.data["likes"] || []) favorited = opts[:for] && opts[:for].ap_id in (activity_object.data["likes"] || [])
bookmarked = opts[:for] && CommonAPI.bookmarked?(opts[:for], reblogged_activity) bookmarked =
opts[:for] && Ecto.assoc_loaded?(reblogged_activity.bookmarks) &&
Enum.any?(reblogged_activity.bookmarks, fn %{user_id: user_id} ->
user_id == opts[:for].id
end)
mentions = mentions =
activity.recipients activity.recipients
@ -149,7 +157,11 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
favorited = opts[:for] && opts[:for].ap_id in (object.data["likes"] || []) favorited = opts[:for] && opts[:for].ap_id in (object.data["likes"] || [])
bookmarked = opts[:for] && CommonAPI.bookmarked?(opts[:for], activity) bookmarked =
opts[:for] && Ecto.assoc_loaded?(activity.bookmarks) &&
Enum.any?(activity.bookmarks, fn %{user_id: user_id} ->
user_id == opts[:for].id
end)
attachment_data = object.data["attachment"] || [] attachment_data = object.data["attachment"] || []
attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment) attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)

View File

@ -31,30 +31,52 @@ defmodule Pleroma.ActivityTest do
assert activity == found_activity assert activity == found_activity
end end
test "preloading object preloads bookmarks" do describe "preloading bookmarks" do
user1 = insert(:user) setup do
user2 = insert(:user) user1 = insert(:user)
activity = insert(:note_activity) user2 = insert(:user)
{:ok, bookmark1} = Bookmark.create(user1.id, activity.id) activity = insert(:note_activity)
{:ok, bookmark2} = Bookmark.create(user2.id, activity.id) {:ok, bookmark1} = Bookmark.create(user1.id, activity.id)
bookmarks = Enum.sort([bookmark1, bookmark2]) {:ok, bookmark2} = Bookmark.create(user2.id, activity.id)
[activity: activity, bookmarks: Enum.sort([bookmark1, bookmark2])]
end
queried_activity = test "using with_preloaded_bookmarks", %{activity: activity, bookmarks: bookmarks} do
Ecto.Query.from(a in Activity, where: a.id == ^activity.id) queried_activity =
|> Activity.with_preloaded_object() Ecto.Query.from(a in Activity, where: a.id == ^activity.id)
|> Repo.one() |> Activity.with_preloaded_bookmarks()
|> Repo.one()
assert Enum.sort(queried_activity.bookmarks) == bookmarks assert Enum.sort(queried_activity.bookmarks) == bookmarks
end
queried_activity = Activity.get_by_ap_id_with_object(activity.data["id"]) test "using with_preloaded_object", %{activity: activity, bookmarks: bookmarks} do
assert Enum.sort(queried_activity.bookmarks) == bookmarks queried_activity =
Ecto.Query.from(a in Activity, where: a.id == ^activity.id)
|> Activity.with_preloaded_object()
|> Repo.one()
queried_activity = Activity.get_by_id_with_object(activity.id) assert Enum.sort(queried_activity.bookmarks) == bookmarks
assert Enum.sort(queried_activity.bookmarks) == bookmarks end
queried_activity = test "using get_by_ap_id_with_object", %{activity: activity, bookmarks: bookmarks} do
Activity.get_create_by_object_ap_id_with_object(Object.normalize(activity).data["id"]) queried_activity = Activity.get_by_ap_id_with_object(activity.data["id"])
assert Enum.sort(queried_activity.bookmarks) == bookmarks
end
assert Enum.sort(queried_activity.bookmarks) == bookmarks test "using get_by_id_with_object", %{activity: activity, bookmarks: bookmarks} do
queried_activity = Activity.get_by_id_with_object(activity.id)
assert Enum.sort(queried_activity.bookmarks) == bookmarks
end
test "using get_create_by_object_ap_id_with_object", %{
activity: activity,
bookmarks: bookmarks
} do
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
end end

View File

@ -168,6 +168,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
{:ok, _bookmark} = Bookmark.create(user.id, activity.id) {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
activity = Activity.get_by_id_with_object(activity.id)
status = StatusView.render("status.json", %{activity: activity, for: user}) status = StatusView.render("status.json", %{activity: activity, for: user})
assert status.bookmarked == true assert status.bookmarked == true