From: lain Date: Tue, 5 Jan 2021 12:10:14 +0000 (+0100) Subject: User: Use ObjectID type to validate also-known-as field X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=e802b48d558ccd4a65a6da2bcc6dacb057b7fd09;p=akkoma User: Use ObjectID type to validate also-known-as field --- diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 230845662..52730fd8d 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -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 diff --git a/test/pleroma/web/mastodon_api/update_credentials_test.exs b/test/pleroma/web/mastodon_api/update_credentials_test.exs index ff0147244..e3e437a19 100644 --- a/test/pleroma/web/mastodon_api/update_credentials_test.exs +++ b/test/pleroma/web/mastodon_api/update_credentials_test.exs @@ -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",