add verification to profile fields
This commit is contained in:
parent
af7c3fab98
commit
96899f6095
2 changed files with 51 additions and 9 deletions
|
@ -159,7 +159,8 @@ defp cachex_children do
|
|||
build_cachex("banned_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000),
|
||||
build_cachex("translations", default_ttl: :timer.hours(24 * 30), limit: 2500),
|
||||
build_cachex("instances", default_ttl: :timer.hours(24), ttl_interval: 1000, limit: 2500),
|
||||
build_cachex("request_signatures", default_ttl: :timer.hours(24 * 30), limit: 3000)
|
||||
build_cachex("request_signatures", default_ttl: :timer.hours(24 * 30), limit: 3000),
|
||||
build_cachex("rel_me", default_ttl: :timer.hours(24 * 30), limit: 300)
|
||||
]
|
||||
end
|
||||
|
||||
|
|
|
@ -479,7 +479,7 @@ def remote_user_changeset(struct \\ %User{local: false}, params) do
|
|||
|> validate_format(:nickname, @email_regex)
|
||||
|> validate_length(:bio, max: bio_limit)
|
||||
|> validate_length(:name, max: name_limit)
|
||||
|> validate_fields(true)
|
||||
|> validate_fields(true, struct)
|
||||
|> validate_non_local()
|
||||
end
|
||||
|
||||
|
@ -549,7 +549,7 @@ def update_changeset(struct, params \\ %{}) do
|
|||
:pleroma_settings_store,
|
||||
&{:ok, Map.merge(struct.pleroma_settings_store, &1)}
|
||||
)
|
||||
|> validate_fields(false)
|
||||
|> validate_fields(false, struct)
|
||||
end
|
||||
|
||||
defp put_fields(changeset) do
|
||||
|
@ -2359,7 +2359,8 @@ def update_background(user, background) do
|
|||
|> update_and_set_cache()
|
||||
end
|
||||
|
||||
def validate_fields(changeset, remote? \\ false) do
|
||||
@spec validate_fields(Ecto.Changeset.t(), Boolean.t(), User.t()) :: Ecto.Changeset.t()
|
||||
def validate_fields(changeset, remote? \\ false, struct) do
|
||||
limit_name = if remote?, do: :max_remote_account_fields, else: :max_account_fields
|
||||
limit = Config.get([:instance, limit_name], 0)
|
||||
|
||||
|
@ -2372,6 +2373,7 @@ def validate_fields(changeset, remote? \\ false) do
|
|||
[fields: "invalid"]
|
||||
end
|
||||
end)
|
||||
|> maybe_validate_rel_me_field(struct)
|
||||
end
|
||||
|
||||
defp valid_field?(%{"name" => name, "value" => value}) do
|
||||
|
@ -2384,6 +2386,48 @@ defp valid_field?(%{"name" => name, "value" => value}) do
|
|||
|
||||
defp valid_field?(_), do: false
|
||||
|
||||
defp is_url(uri) do
|
||||
case URI.parse(uri) do
|
||||
%URI{host: nil} -> false
|
||||
%URI{scheme: nil} -> false
|
||||
_ -> true
|
||||
end
|
||||
end
|
||||
|
||||
@spec maybe_validate_rel_me_field(Changeset.t(), User.t()) :: Changeset.t()
|
||||
defp maybe_validate_rel_me_field(changeset, %User{ap_id: _ap_id} = struct) do
|
||||
fields = get_change(changeset, :fields)
|
||||
|
||||
if is_nil(fields) do
|
||||
changeset
|
||||
else
|
||||
validate_rel_me_field(changeset, fields, struct)
|
||||
end
|
||||
end
|
||||
|
||||
defp maybe_validate_rel_me_field(changeset, _), do: changeset
|
||||
|
||||
@spec validate_rel_me_field(Changeset.t(), [Map.t()], User.t()) :: Changeset.t()
|
||||
defp validate_rel_me_field(changeset, fields, %User{ap_id: ap_id}) do
|
||||
fields =
|
||||
fields
|
||||
|> Enum.map(fn %{"name" => name, "value" => value} ->
|
||||
if is_url(value) do
|
||||
with "me" <- RelMe.maybe_put_rel_me(value, [ap_id]) do
|
||||
%{"name" => name, "value" => value, "verified_at" => DateTime.to_iso8601(DateTime.utc_now())}
|
||||
else
|
||||
e ->
|
||||
Logger.error("Could not check for rel=me, #{inspect(e)}")
|
||||
%{"name" => name, "value" => value}
|
||||
end
|
||||
else
|
||||
%{"name" => name, "value" => value}
|
||||
end
|
||||
end)
|
||||
|
||||
put_change(changeset, :fields, fields)
|
||||
end
|
||||
|
||||
defp truncate_field(%{"name" => name, "value" => value}) do
|
||||
{name, _chopped} =
|
||||
String.split_at(name, Config.get([:instance, :account_field_name_length], 255))
|
||||
|
@ -2551,11 +2595,8 @@ def sanitize_html(%User{} = user) do
|
|||
# - display name
|
||||
def sanitize_html(%User{} = user, filter) do
|
||||
fields =
|
||||
Enum.map(user.fields, fn %{"name" => name, "value" => value} ->
|
||||
%{
|
||||
"name" => name,
|
||||
"value" => HTML.filter_tags(value, Pleroma.HTML.Scrubber.LinksOnly)
|
||||
}
|
||||
Enum.map(user.fields, fn %{"value" => value} = field ->
|
||||
Map.put(field, "value", HTML.filter_tags(value, Pleroma.HTML.Scrubber.LinksOnly))
|
||||
end)
|
||||
|
||||
user
|
||||
|
|
Loading…
Reference in a new issue