1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-22 07:52:43 +00:00

Improve changing current actor

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-10-25 13:20:33 +02:00
parent d0b45de175
commit bac6628aea
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
3 changed files with 21 additions and 28 deletions

View file

@ -311,19 +311,20 @@ defmodule Mobilizon.GraphQL.Resolvers.User do
def change_default_actor( def change_default_actor(
_parent, _parent,
%{preferred_username: username}, %{preferred_username: username},
%{context: %{current_user: %User{id: user_id} = user}} %{context: %{current_user: %User{} = user}}
) do ) do
with %Actor{id: actor_id} = actor <- Actors.get_local_actor_by_name(username), case Actors.get_local_actor_by_name(username) do
{:user_actor, true} <- %Actor{id: actor_id} = actor ->
{:user_actor, actor_id in Enum.map(Users.get_actors_for_user(user), & &1.id)}, if actor_id in Enum.map(Users.get_actors_for_user(user), & &1.id) do
%User{} = user <- Users.update_user_default_actor(user_id, actor) do %User{} = user = Users.update_user_default_actor(user, actor)
{:ok, user} {:ok, user}
else else
{:user_actor, _} -> {:error, dgettext("errors", "This profile does not belong to you")}
{:error, :actor_not_from_user} end
_error -> nil ->
{:error, :unable_to_change_default_actor} {:error,
dgettext("errors", "Profile with username %{username} not found", %{username: username})}
end end
end end

View file

@ -250,19 +250,15 @@ defmodule Mobilizon.Users do
Updates user's default actor. Updates user's default actor.
Raises `Ecto.NoResultsError` if the user does not exist. Raises `Ecto.NoResultsError` if the user does not exist.
""" """
@spec update_user_default_actor(integer | String.t(), Actor.t() | nil) :: User.t() @spec update_user_default_actor(User.t(), Actor.t() | nil) :: User.t()
def update_user_default_actor(user_id, actor) do def update_user_default_actor(%User{id: user_id} = user, actor) do
actor_id = if is_nil(actor), do: nil, else: actor.id actor_id = if is_nil(actor), do: nil, else: actor.id
user_id user_id
|> update_user_default_actor_query(actor_id) |> update_user_default_actor_query()
|> Repo.update_all([]) |> Repo.update_all(set: [default_actor_id: actor_id])
Cachex.put(:default_actors, to_string(user_id), actor) %User{user | default_actor: actor}
user_id
|> get_user!()
|> Repo.preload([:default_actor])
end end
@doc """ @doc """
@ -524,13 +520,9 @@ defmodule Mobilizon.Users do
from(a in Actor, where: a.user_id == ^user_id) from(a in Actor, where: a.user_id == ^user_id)
end end
@spec update_user_default_actor_query(integer | String.t(), integer | String.t() | nil) :: @spec update_user_default_actor_query(integer | String.t()) ::
Ecto.Query.t() Ecto.Query.t()
defp update_user_default_actor_query(user_id, actor_id) do defp update_user_default_actor_query(user_id) do
from( where(User, [u], u.id == ^user_id)
u in User,
where: u.id == ^user_id,
update: [set: [default_actor_id: ^actor_id]]
)
end end
end end

View file

@ -32,7 +32,7 @@ defmodule Mobilizon.Web.Auth.Guardian do
try do try do
case Integer.parse(uid_str) do case Integer.parse(uid_str) do
{uid, ""} -> {uid, ""} ->
{:ok, Users.get_user_with_actors!(uid)} {:ok, Users.get_user!(uid)}
_ -> _ ->
{:error, :invalid_id} {:error, :invalid_id}