Move is_good_locale_code? to object validator

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2023-08-31 11:35:09 +02:00
commit b52d189fcc
5 changed files with 13 additions and 9 deletions

View file

@ -5,8 +5,6 @@
defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCode do defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCode do
use Ecto.Type use Ecto.Type
import Pleroma.Web.CommonAPI.Utils, only: [is_good_locale_code?: 1]
def type, do: :string def type, do: :string
def cast(language) when is_binary(language) do def cast(language) when is_binary(language) do
@ -22,4 +20,8 @@ defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCode do
def dump(data), do: {:ok, data} def dump(data), do: {:ok, data}
def load(data), do: {:ok, data} def load(data), do: {:ok, data}
def is_good_locale_code?(code) when is_binary(code), do: code =~ ~r<^[a-zA-Z0-9\-]+$>
def is_good_locale_code?(_code), do: false
end end

View file

@ -5,7 +5,8 @@
defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.MapOfString do defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.MapOfString do
use Ecto.Type use Ecto.Type
import Pleroma.Web.CommonAPI.Utils, only: [is_good_locale_code?: 1] import Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCode,
only: [is_good_locale_code?: 1]
def type, do: :map def type, do: :map

View file

@ -10,7 +10,9 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes do
alias Pleroma.Web.ActivityPub.Transmogrifier alias Pleroma.Web.ActivityPub.Transmogrifier
alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.ActivityPub.Utils
import Pleroma.Web.CommonAPI.Utils, only: [is_good_locale_code?: 1] import Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCode,
only: [is_good_locale_code?: 1]
import Pleroma.Web.Utils.Guards, only: [not_empty_string: 1] import Pleroma.Web.Utils.Guards, only: [not_empty_string: 1]
def cast_and_filter_recipients(message, field, follower_collection, field_fallback \\ []) do def cast_and_filter_recipients(message, field, follower_collection, field_fallback \\ []) do

View file

@ -10,6 +10,9 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
alias Pleroma.Web.CommonAPI.Utils alias Pleroma.Web.CommonAPI.Utils
import Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCode,
only: [is_good_locale_code?: 1]
import Pleroma.Web.Gettext import Pleroma.Web.Gettext
defstruct valid?: true, defstruct valid?: true,
@ -195,7 +198,7 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
defp language(draft) do defp language(draft) do
language = draft.params[:language] language = draft.params[:language]
if Utils.is_good_locale_code?(language) do if is_good_locale_code?(language) do
%__MODULE__{draft | language: language} %__MODULE__{draft | language: language}
else else
draft draft

View file

@ -494,8 +494,4 @@ defmodule Pleroma.Web.CommonAPI.Utils do
{:error, dgettext("errors", "Too many attachments")} {:error, dgettext("errors", "Too many attachments")}
end end
end end
def is_good_locale_code?(code) when is_binary(code), do: code =~ ~r<^[a-zA-Z0-9\-]+$>
def is_good_locale_code?(_code), do: false
end end