Merge branch 'fix-prameter-name-of-accounts-update-credentials' into 'develop'
Fix custom fields of accounts update credentials See merge request pleroma/pleroma!1597
This commit is contained in:
commit
9e34919dcd
|
@ -105,6 +105,17 @@ def update_credentials(%{assigns: %{user: original_user}} = conn, params) do
|
||||||
|> Enum.concat(Emoji.Formatter.get_emoji_map(emojis_text))
|
|> Enum.concat(Emoji.Formatter.get_emoji_map(emojis_text))
|
||||||
|> Enum.dedup()
|
|> Enum.dedup()
|
||||||
|
|
||||||
|
params =
|
||||||
|
if Map.has_key?(params, "fields_attributes") do
|
||||||
|
Map.update!(params, "fields_attributes", fn fields ->
|
||||||
|
fields
|
||||||
|
|> normalize_fields_attributes()
|
||||||
|
|> Enum.filter(fn %{"name" => n} -> n != "" end)
|
||||||
|
end)
|
||||||
|
else
|
||||||
|
params
|
||||||
|
end
|
||||||
|
|
||||||
info_params =
|
info_params =
|
||||||
[
|
[
|
||||||
:no_rich_text,
|
:no_rich_text,
|
||||||
|
@ -122,12 +133,12 @@ def update_credentials(%{assigns: %{user: original_user}} = conn, params) do
|
||||||
add_if_present(acc, params, to_string(key), key, &{:ok, truthy_param?(&1)})
|
add_if_present(acc, params, to_string(key), key, &{:ok, truthy_param?(&1)})
|
||||||
end)
|
end)
|
||||||
|> add_if_present(params, "default_scope", :default_scope)
|
|> add_if_present(params, "default_scope", :default_scope)
|
||||||
|> add_if_present(params, "fields", :fields, fn fields ->
|
|> add_if_present(params, "fields_attributes", :fields, fn fields ->
|
||||||
fields = Enum.map(fields, fn f -> Map.update!(f, "value", &AutoLinker.link(&1)) end)
|
fields = Enum.map(fields, fn f -> Map.update!(f, "value", &AutoLinker.link(&1)) end)
|
||||||
|
|
||||||
{:ok, fields}
|
{:ok, fields}
|
||||||
end)
|
end)
|
||||||
|> add_if_present(params, "fields", :raw_fields)
|
|> add_if_present(params, "fields_attributes", :raw_fields)
|
||||||
|> add_if_present(params, "pleroma_settings_store", :pleroma_settings_store, fn value ->
|
|> add_if_present(params, "pleroma_settings_store", :pleroma_settings_store, fn value ->
|
||||||
{:ok, Map.merge(user.info.pleroma_settings_store, value)}
|
{:ok, Map.merge(user.info.pleroma_settings_store, value)}
|
||||||
end)
|
end)
|
||||||
|
@ -168,6 +179,14 @@ defp add_if_present(map, params, params_field, map_field, value_function \\ &{:o
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp normalize_fields_attributes(fields) do
|
||||||
|
if Enum.all?(fields, &is_tuple/1) do
|
||||||
|
Enum.map(fields, fn {_, v} -> v end)
|
||||||
|
else
|
||||||
|
fields
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@doc "GET /api/v1/accounts/relationships"
|
@doc "GET /api/v1/accounts/relationships"
|
||||||
def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||||
targets = User.get_all_by_ids(List.wrap(id))
|
targets = User.get_all_by_ids(List.wrap(id))
|
||||||
|
|
|
@ -328,7 +328,7 @@ test "update fields", %{conn: conn} do
|
||||||
account =
|
account =
|
||||||
conn
|
conn
|
||||||
|> assign(:user, user)
|
|> assign(:user, user)
|
||||||
|> patch("/api/v1/accounts/update_credentials", %{"fields" => fields})
|
|> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
|
||||||
|> json_response(200)
|
|> json_response(200)
|
||||||
|
|
||||||
assert account["fields"] == [
|
assert account["fields"] == [
|
||||||
|
@ -344,6 +344,35 @@ test "update fields", %{conn: conn} do
|
||||||
%{"name" => "link", "value" => "cofe.io"}
|
%{"name" => "link", "value" => "cofe.io"}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
fields =
|
||||||
|
[
|
||||||
|
"fields_attributes[1][name]=link",
|
||||||
|
"fields_attributes[1][value]=cofe.io",
|
||||||
|
"fields_attributes[0][name]=<a href=\"http://google.com\">foo</a>",
|
||||||
|
"fields_attributes[0][value]=bar"
|
||||||
|
]
|
||||||
|
|> Enum.join("&")
|
||||||
|
|
||||||
|
account =
|
||||||
|
conn
|
||||||
|
|> put_req_header("content-type", "application/x-www-form-urlencoded")
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> patch("/api/v1/accounts/update_credentials", fields)
|
||||||
|
|> json_response(200)
|
||||||
|
|
||||||
|
assert account["fields"] == [
|
||||||
|
%{"name" => "foo", "value" => "bar"},
|
||||||
|
%{"name" => "link", "value" => ~S(<a href="http://cofe.io" rel="ugc">cofe.io</a>)}
|
||||||
|
]
|
||||||
|
|
||||||
|
assert account["source"]["fields"] == [
|
||||||
|
%{
|
||||||
|
"name" => "<a href=\"http://google.com\">foo</a>",
|
||||||
|
"value" => "bar"
|
||||||
|
},
|
||||||
|
%{"name" => "link", "value" => "cofe.io"}
|
||||||
|
]
|
||||||
|
|
||||||
name_limit = Pleroma.Config.get([:instance, :account_field_name_length])
|
name_limit = Pleroma.Config.get([:instance, :account_field_name_length])
|
||||||
value_limit = Pleroma.Config.get([:instance, :account_field_value_length])
|
value_limit = Pleroma.Config.get([:instance, :account_field_value_length])
|
||||||
|
|
||||||
|
@ -354,7 +383,7 @@ test "update fields", %{conn: conn} do
|
||||||
assert %{"error" => "Invalid request"} ==
|
assert %{"error" => "Invalid request"} ==
|
||||||
conn
|
conn
|
||||||
|> assign(:user, user)
|
|> assign(:user, user)
|
||||||
|> patch("/api/v1/accounts/update_credentials", %{"fields" => fields})
|
|> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
|
||||||
|> json_response(403)
|
|> json_response(403)
|
||||||
|
|
||||||
long_name = Enum.map(0..name_limit, fn _ -> "x" end) |> Enum.join()
|
long_name = Enum.map(0..name_limit, fn _ -> "x" end) |> Enum.join()
|
||||||
|
@ -364,7 +393,7 @@ test "update fields", %{conn: conn} do
|
||||||
assert %{"error" => "Invalid request"} ==
|
assert %{"error" => "Invalid request"} ==
|
||||||
conn
|
conn
|
||||||
|> assign(:user, user)
|
|> assign(:user, user)
|
||||||
|> patch("/api/v1/accounts/update_credentials", %{"fields" => fields})
|
|> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
|
||||||
|> json_response(403)
|
|> json_response(403)
|
||||||
|
|
||||||
Pleroma.Config.put([:instance, :max_account_fields], 1)
|
Pleroma.Config.put([:instance, :max_account_fields], 1)
|
||||||
|
@ -377,8 +406,23 @@ test "update fields", %{conn: conn} do
|
||||||
assert %{"error" => "Invalid request"} ==
|
assert %{"error" => "Invalid request"} ==
|
||||||
conn
|
conn
|
||||||
|> assign(:user, user)
|
|> assign(:user, user)
|
||||||
|> patch("/api/v1/accounts/update_credentials", %{"fields" => fields})
|
|> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
|
||||||
|> json_response(403)
|
|> json_response(403)
|
||||||
|
|
||||||
|
fields = [
|
||||||
|
%{"name" => "foo", "value" => ""},
|
||||||
|
%{"name" => "", "value" => "bar"}
|
||||||
|
]
|
||||||
|
|
||||||
|
account =
|
||||||
|
conn
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})
|
||||||
|
|> json_response(200)
|
||||||
|
|
||||||
|
assert account["fields"] == [
|
||||||
|
%{"name" => "foo", "value" => ""}
|
||||||
|
]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue