Make sure every relation of actor is loaded when operating on it

Closes #1049

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2022-04-01 10:03:43 +02:00
parent 9b7cd5831a
commit b1ac997f8a
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773

View File

@ -58,6 +58,8 @@ defmodule Mobilizon.Actors do
@moderator_roles [:moderator] ++ @administrator_roles @moderator_roles [:moderator] ++ @administrator_roles
@member_roles [:member] ++ @moderator_roles @member_roles [:member] ++ @moderator_roles
@associations_to_preload [:organized_events, :followers, :followings, :user, :physical_address]
@doc """ @doc """
Gets a single actor. Gets a single actor.
""" """
@ -169,7 +171,7 @@ defmodule Mobilizon.Actors do
def get_local_actor_by_name_with_preload(name) do def get_local_actor_by_name_with_preload(name) do
name name
|> get_local_actor_by_name() |> get_local_actor_by_name()
|> Repo.preload([:organized_events, :followers, :followings]) |> Repo.preload(@associations_to_preload)
end end
@doc """ @doc """
@ -179,7 +181,7 @@ defmodule Mobilizon.Actors do
def get_actor_by_name_with_preload(name, type \\ nil) do def get_actor_by_name_with_preload(name, type \\ nil) do
name name
|> get_actor_by_name(type) |> get_actor_by_name(type)
|> Repo.preload([:organized_events, :user, :physical_address]) |> Repo.preload(@associations_to_preload)
end end
@doc """ @doc """
@ -246,7 +248,7 @@ defmodule Mobilizon.Actors do
@spec update_actor(Actor.t(), map) :: {:ok, Actor.t()} | {:error, Ecto.Changeset.t()} @spec update_actor(Actor.t(), map) :: {:ok, Actor.t()} | {:error, Ecto.Changeset.t()}
def update_actor(%Actor{} = actor, attrs) do def update_actor(%Actor{} = actor, attrs) do
actor actor
|> Repo.preload([:physical_address]) |> Repo.preload(@associations_to_preload)
|> Actor.update_changeset(attrs) |> Actor.update_changeset(attrs)
|> delete_files_if_media_changed() |> delete_files_if_media_changed()
|> Repo.update() |> Repo.update()
@ -271,7 +273,7 @@ defmodule Mobilizon.Actors do
case insert do case insert do
{:ok, actor} -> {:ok, actor} ->
actor = if preload, do: Repo.preload(actor, [:followers]), else: actor actor = if preload, do: Repo.preload(actor, @associations_to_preload), else: actor
{:ok, actor} {:ok, actor}
@ -1304,7 +1306,7 @@ defmodule Mobilizon.Actors do
defp actor_with_preload_query(actor_id, true) do defp actor_with_preload_query(actor_id, true) do
Actor Actor
|> where([a], a.id == ^actor_id) |> where([a], a.id == ^actor_id)
|> preload([a], [:organized_events, :followers, :followings]) |> preload([a], ^@associations_to_preload)
end end
@spec actor_by_username_query(String.t()) :: Ecto.Query.t() @spec actor_by_username_query(String.t()) :: Ecto.Query.t()