1
0
mirror of https://git.pleroma.social/sjw/pleroma.git synced 2024-12-26 01:25:24 +01:00

Fix masto api user updating.

This commit is contained in:
lain 2018-12-01 10:40:01 +01:00
parent c443c9bd72
commit 347df6421d
4 changed files with 66 additions and 59 deletions

View File

@ -112,10 +112,9 @@ defmodule Pleroma.User do
end
end
# TODO: Check if this still used
def update_changeset(struct, params \\ %{}) do
struct
|> cast(params, [:bio, :name])
|> cast(params, [:bio, :name, :avatar])
|> unique_constraint(:nickname)
|> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
|> validate_length(:bio, max: 5000)

View File

@ -128,6 +128,14 @@ defmodule Pleroma.User.Info do
])
end
def mastodon_profile_update(info, params) do
info
|> cast(params, [
:locked,
:banner
])
end
def set_source_data(info, source_data) do
params = %{source_data: source_data}

View File

@ -32,67 +32,55 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end
end
defp add_if_present(
map,
params,
params_field,
map_field,
value_function \\ fn x -> {:ok, x} end
) do
if Map.has_key?(params, params_field) do
case value_function.(params[params_field]) do
{:ok, new_value} -> Map.put(map, map_field, new_value)
:error -> map
end
else
map
end
end
def update_credentials(%{assigns: %{user: user}} = conn, params) do
original_user = user
params =
if bio = params["note"] do
Map.put(params, "bio", bio)
else
params
end
params =
if name = params["display_name"] do
Map.put(params, "name", name)
else
params
end
user =
if avatar = params["avatar"] do
with %Plug.Upload{} <- avatar,
{:ok, object} <- ActivityPub.upload(avatar, type: :avatar),
change = Ecto.Changeset.change(user, %{avatar: object.data}),
{:ok, user} = User.update_and_set_cache(change) do
user
user_params =
%{}
|> add_if_present(params, "display_name", :name)
|> add_if_present(params, "note", :bio)
|> add_if_present(params, "avatar", :avatar, fn value ->
with %Plug.Upload{} <- value,
{:ok, object} <- ActivityPub.upload(value, type: :avatar) do
{:ok, object.data}
else
_e -> user
_ -> :error
end
else
user
end
end)
# user =
# if banner = params["header"] do
# with %Plug.Upload{} <- banner,
# {:ok, object} <- ActivityPub.upload(banner, type: :banner),
# new_info <- Map.put(user.info, "banner", object.data),
# change <- User.info_changeset(user, %{info: new_info}),
# {:ok, user} <- User.update_and_set_cache(change) do
# user
# else
# _e -> user
# end
# else
# user
# end
info_params =
%{}
|> add_if_present(params, "locked", :locked, fn value -> {:ok, value == "true"} end)
|> add_if_present(params, "header", :banner, fn value ->
with %Plug.Upload{} <- value,
{:ok, object} <- ActivityPub.upload(value, type: :banner) do
{:ok, object.data}
else
_ -> :error
end
end)
# user =
# if locked = params["locked"] do
# with locked <- locked == "true",
# new_info <- Map.put(user.info, "locked", locked),
# change <- User.info_changeset(user, %{info: new_info}),
# {:ok, user} <- User.update_and_set_cache(change) do
# user
# else
# _e -> user
# end
# else
# user
# end
info_cng = User.Info.mastodon_profile_update(user.info, info_params)
with changeset <- User.update_changeset(user, params),
with changeset <- User.update_changeset(user, user_params),
changeset <- Ecto.Changeset.put_embed(changeset, :info, info_cng),
{:ok, user} <- User.update_and_set_cache(changeset) do
if original_user != user do
CommonAPI.update(user)

View File

@ -1263,6 +1263,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
assert user["note"] == "I drink #cofe"
end
test "updates the user's locking status", %{conn: conn} do
user = insert(:user)
conn =
conn
|> assign(:user, user)
|> patch("/api/v1/accounts/update_credentials", %{locked: "true"})
assert user = json_response(conn, 200)
assert user["locked"] == true
end
test "updates the user's name", %{conn: conn} do
user = insert(:user)
@ -1289,8 +1301,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
|> assign(:user, user)
|> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
assert user = json_response(conn, 200)
assert user["avatar"] != "https://placehold.it/48x48"
assert user_response = json_response(conn, 200)
assert user_response["avatar"] != User.avatar_url(user)
end
test "updates the user's banner", %{conn: conn} do
@ -1307,8 +1319,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
|> assign(:user, user)
|> patch("/api/v1/accounts/update_credentials", %{"header" => new_header})
assert user = json_response(conn, 200)
assert user["header"] != "https://placehold.it/700x335"
assert user_response = json_response(conn, 200)
assert user_response["header"] != User.banner_url(user)
end
end