CommonValidations: Refactor `same_domain?`
authorlain <lain@soykaf.club>
Wed, 5 Aug 2020 15:36:27 +0000 (17:36 +0200)
committerlain <lain@soykaf.club>
Wed, 5 Aug 2020 15:36:27 +0000 (17:36 +0200)
lib/pleroma/web/activity_pub/object_validators/common_validations.ex

index e4c5d961928c1d84a31459e2f1b1a00ebd5606ad..82a9d39b59b31d0d22724dad018915c7476edb5f 100644 (file)
@@ -126,18 +126,21 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
     end
   end
 
-  def same_domain?(cng, field_one \\ :actor, field_two \\ :object) do
-    actor_uri =
-      cng
-      |> get_field(field_one)
-      |> URI.parse()
+  def same_domain?(cng, fields \\ [:actor, :object]) do
+    unique_domains =
+      fields
+      |> Enum.map(fn field ->
+        %URI{host: host} =
+          cng
+          |> get_field(field)
+          |> URI.parse()
 
-    object_uri =
-      cng
-      |> get_field(field_two)
-      |> URI.parse()
+        host
+      end)
+      |> Enum.uniq()
+      |> Enum.count()
 
-    object_uri.host == actor_uri.host
+    unique_domains == 1
   end
 
   # This figures out if a user is able to create, delete or modify something