diff options
author | lain <lain@soykaf.club> | 2020-08-05 17:56:12 +0200 |
---|---|---|
committer | lain <lain@soykaf.club> | 2020-08-05 17:56:12 +0200 |
commit | 9d7ce1a6d014499eb4d55190b81e55da849b5ad0 (patch) | |
tree | 38b0c7427fecb1bd1252c3bba07bf8969d888a3b | |
parent | 3655175639a004976ef8296a0838e72642ba0b11 (diff) | |
download | pleroma-9d7ce1a6d014499eb4d55190b81e55da849b5ad0.tar.gz |
CommonValidations: More refactors.
-rw-r--r-- | lib/pleroma/web/activity_pub/object_validators/common_validations.ex | 51 |
1 files changed, 17 insertions, 34 deletions
diff --git a/lib/pleroma/web/activity_pub/object_validators/common_validations.ex b/lib/pleroma/web/activity_pub/object_validators/common_validations.ex index 82a9d39b5..603d87b8e 100644 --- a/lib/pleroma/web/activity_pub/object_validators/common_validations.ex +++ b/lib/pleroma/web/activity_pub/object_validators/common_validations.ex @@ -84,20 +84,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do end def validate_host_match(cng, fields \\ [:id, :actor]) do - unique_hosts = - fields - |> Enum.map(fn field -> - %URI{host: host} = - cng - |> get_field(field) - |> URI.parse() - - host - end) - |> Enum.uniq() - |> Enum.count() - - if unique_hosts == 1 do + if same_domain?(cng, fields) do cng else fields @@ -109,13 +96,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do end def validate_fields_match(cng, fields) do - unique_fields = - fields - |> Enum.map(fn field -> get_field(cng, field) end) - |> Enum.uniq() - |> Enum.count() - - if unique_fields == 1 do + if map_unique?(cng, fields) do cng else fields @@ -126,21 +107,23 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do end end - def same_domain?(cng, fields \\ [:actor, :object]) do - unique_domains = - fields - |> Enum.map(fn field -> - %URI{host: host} = - cng - |> get_field(field) - |> URI.parse() + defp map_unique?(cng, fields, func \\ & &1) do + Enum.reduce_while(fields, nil, fn field, acc -> + value = + cng + |> get_field(field) + |> func.() - host - end) - |> Enum.uniq() - |> Enum.count() + case {value, acc} do + {value, nil} -> {:cont, value} + {value, value} -> {:cont, value} + _ -> {:halt, false} + end + end) + end - unique_domains == 1 + def same_domain?(cng, fields \\ [:actor, :object]) do + map_unique?(cng, fields, fn value -> URI.parse(value).host end) end # This figures out if a user is able to create, delete or modify something |