User: Use ObjectID type to validate also-known-as field
authorlain <lain@soykaf.club>
Tue, 5 Jan 2021 12:10:14 +0000 (13:10 +0100)
committerlain <lain@soykaf.club>
Tue, 5 Jan 2021 12:10:14 +0000 (13:10 +0100)
lib/pleroma/user.ex
test/pleroma/web/mastodon_api/update_credentials_test.exs

index 2308456628a7007968f08c7f594c72bfdc6fe746..52730fd8d63654b2c3ae8302a011a2b0d61a813f 100644 (file)
@@ -51,7 +51,6 @@ defmodule Pleroma.User do
 
   # credo:disable-for-next-line Credo.Check.Readability.MaxLineLength
   @email_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
-  @url_regex ~r/^https?:\/\/[^\s]{1,256}$/
 
   @strict_local_nickname_regex ~r/^[a-zA-Z\d]+$/
   @extended_local_nickname_regex ~r/^[a-zA-Z\d_-]+$/
@@ -143,7 +142,7 @@ defmodule Pleroma.User do
     field(:allow_following_move, :boolean, default: true)
     field(:skip_thread_containment, :boolean, default: false)
     field(:actor_type, :string, default: "Person")
-    field(:also_known_as, {:array, :string}, default: [])
+    field(:also_known_as, {:array, ObjectValidators.ObjectID}, default: [])
     field(:inbox, :string)
     field(:shared_inbox, :string)
     field(:accepts_chat_messages, :boolean, default: nil)
@@ -530,7 +529,6 @@ defmodule Pleroma.User do
     )
     |> unique_constraint(:nickname)
     |> validate_format(:nickname, local_nickname_regex())
-    |> validate_also_known_as()
     |> validate_length(:bio, max: bio_limit)
     |> validate_length(:name, min: 1, max: name_limit)
     |> validate_inclusion(:actor_type, ["Person", "Service"])
@@ -2456,16 +2454,6 @@ defmodule Pleroma.User do
     |> Map.put(:fields, fields)
   end
 
-  defp validate_also_known_as(changeset) do
-    validate_change(changeset, :also_known_as, fn :also_known_as, also_known_as ->
-      if Enum.all?(also_known_as, fn a -> Regex.match?(@url_regex, a) end) do
-        []
-      else
-        [also_known_as: "Invalid ap_id format. Must be a URL."]
-      end
-    end)
-  end
-
   def get_host(%User{ap_id: ap_id} = _user) do
     URI.parse(ap_id).host
   end
index ff0147244a5a9c1193115347f0c8d7eaa5d34176..e3e437a19c538e105c57f9314d8d9b8c08f18cad 100644 (file)
@@ -228,6 +228,15 @@ defmodule Pleroma.Web.MastodonAPI.UpdateCredentialsTest do
       assert user_data["pleroma"]["also_known_as"] == ["https://mushroom.kingdom/users/mario"]
     end
 
+    test "doesn't update non-url akas", %{conn: conn} do
+      conn =
+        patch(conn, "/api/v1/accounts/update_credentials", %{
+          "also_known_as" => ["aReallyCoolGuy"]
+        })
+
+      assert json_response_and_validate_schema(conn, 403)
+    end
+
     test "updates the user's avatar", %{user: user, conn: conn} do
       new_avatar = %Plug.Upload{
         content_type: "image/jpeg",