forked from YokaiRick/akkoma
Fix masto api user updating.
This commit is contained in:
parent
c443c9bd72
commit
347df6421d
4 changed files with 66 additions and 59 deletions
|
@ -112,10 +112,9 @@ def remote_user_creation(params) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: Check if this still used
|
|
||||||
def update_changeset(struct, params \\ %{}) do
|
def update_changeset(struct, params \\ %{}) do
|
||||||
struct
|
struct
|
||||||
|> cast(params, [:bio, :name])
|
|> cast(params, [:bio, :name, :avatar])
|
||||||
|> unique_constraint(:nickname)
|
|> unique_constraint(:nickname)
|
||||||
|> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
|
|> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
|
||||||
|> validate_length(:bio, max: 5000)
|
|> validate_length(:bio, max: 5000)
|
||||||
|
|
|
@ -128,6 +128,14 @@ def profile_update(info, params) do
|
||||||
])
|
])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def mastodon_profile_update(info, params) do
|
||||||
|
info
|
||||||
|
|> cast(params, [
|
||||||
|
:locked,
|
||||||
|
:banner
|
||||||
|
])
|
||||||
|
end
|
||||||
|
|
||||||
def set_source_data(info, source_data) do
|
def set_source_data(info, source_data) do
|
||||||
params = %{source_data: source_data}
|
params = %{source_data: source_data}
|
||||||
|
|
||||||
|
|
|
@ -32,67 +32,55 @@ def create_app(conn, params) do
|
||||||
end
|
end
|
||||||
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
|
def update_credentials(%{assigns: %{user: user}} = conn, params) do
|
||||||
original_user = user
|
original_user = user
|
||||||
|
|
||||||
params =
|
user_params =
|
||||||
if bio = params["note"] do
|
%{}
|
||||||
Map.put(params, "bio", bio)
|
|> add_if_present(params, "display_name", :name)
|
||||||
else
|
|> add_if_present(params, "note", :bio)
|
||||||
params
|
|> add_if_present(params, "avatar", :avatar, fn value ->
|
||||||
end
|
with %Plug.Upload{} <- value,
|
||||||
|
{:ok, object} <- ActivityPub.upload(value, type: :avatar) do
|
||||||
params =
|
{:ok, object.data}
|
||||||
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
|
|
||||||
else
|
else
|
||||||
_e -> user
|
_ -> :error
|
||||||
end
|
end
|
||||||
else
|
end)
|
||||||
user
|
|
||||||
end
|
|
||||||
|
|
||||||
# user =
|
info_params =
|
||||||
# if banner = params["header"] do
|
%{}
|
||||||
# with %Plug.Upload{} <- banner,
|
|> add_if_present(params, "locked", :locked, fn value -> {:ok, value == "true"} end)
|
||||||
# {:ok, object} <- ActivityPub.upload(banner, type: :banner),
|
|> add_if_present(params, "header", :banner, fn value ->
|
||||||
# new_info <- Map.put(user.info, "banner", object.data),
|
with %Plug.Upload{} <- value,
|
||||||
# change <- User.info_changeset(user, %{info: new_info}),
|
{:ok, object} <- ActivityPub.upload(value, type: :banner) do
|
||||||
# {:ok, user} <- User.update_and_set_cache(change) do
|
{:ok, object.data}
|
||||||
# user
|
else
|
||||||
# else
|
_ -> :error
|
||||||
# _e -> user
|
end
|
||||||
# end
|
end)
|
||||||
# else
|
|
||||||
# user
|
|
||||||
# end
|
|
||||||
|
|
||||||
# user =
|
info_cng = User.Info.mastodon_profile_update(user.info, info_params)
|
||||||
# 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
|
|
||||||
|
|
||||||
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
|
{:ok, user} <- User.update_and_set_cache(changeset) do
|
||||||
if original_user != user do
|
if original_user != user do
|
||||||
CommonAPI.update(user)
|
CommonAPI.update(user)
|
||||||
|
|
|
@ -1263,6 +1263,18 @@ test "updates the user's bio", %{conn: conn} do
|
||||||
assert user["note"] == "I drink #cofe"
|
assert user["note"] == "I drink #cofe"
|
||||||
end
|
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
|
test "updates the user's name", %{conn: conn} do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
|
||||||
|
@ -1289,8 +1301,8 @@ test "updates the user's avatar", %{conn: conn} do
|
||||||
|> assign(:user, user)
|
|> assign(:user, user)
|
||||||
|> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
|
|> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
|
||||||
|
|
||||||
assert user = json_response(conn, 200)
|
assert user_response = json_response(conn, 200)
|
||||||
assert user["avatar"] != "https://placehold.it/48x48"
|
assert user_response["avatar"] != User.avatar_url(user)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "updates the user's banner", %{conn: conn} do
|
test "updates the user's banner", %{conn: conn} do
|
||||||
|
@ -1307,8 +1319,8 @@ test "updates the user's banner", %{conn: conn} do
|
||||||
|> assign(:user, user)
|
|> assign(:user, user)
|
||||||
|> patch("/api/v1/accounts/update_credentials", %{"header" => new_header})
|
|> patch("/api/v1/accounts/update_credentials", %{"header" => new_header})
|
||||||
|
|
||||||
assert user = json_response(conn, 200)
|
assert user_response = json_response(conn, 200)
|
||||||
assert user["header"] != "https://placehold.it/700x335"
|
assert user_response["header"] != User.banner_url(user)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue