summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormarcin mikołajczak <git@mkljczk.pl>2023-08-11 13:44:30 +0200
committermarcin mikołajczak <git@mkljczk.pl>2023-08-11 13:44:30 +0200
commit04c8f6b4d1e2a9a30f66b0ffb99d7a17a1510a3e (patch)
tree1cde0fb615327b8b6a6f7c6d4bbc6b4b650d2639
parent049045cf2ac90dcca074be9b5cf2d8264828f834 (diff)
downloadpleroma-04c8f6b4d1e2a9a30f66b0ffb99d7a17a1510a3e.tar.gz
pleroma-04c8f6b4d1e2a9a30f66b0ffb99d7a17a1510a3e.zip
Add ObjectValidators.LanguageCode type
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
-rw-r--r--lib/pleroma/ecto_type/activity_pub/object_validators/language_code.ex25
-rw-r--r--lib/pleroma/web/activity_pub/object_validators/common_fields.ex2
-rw-r--r--test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.exs (renamed from test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.ex)10
-rw-r--r--test/pleroma/ecto_type/activity_pub/object_validators/language_code.exs28
4 files changed, 59 insertions, 6 deletions
diff --git a/lib/pleroma/ecto_type/activity_pub/object_validators/language_code.ex b/lib/pleroma/ecto_type/activity_pub/object_validators/language_code.ex
new file mode 100644
index 000000000..327279bf8
--- /dev/null
+++ b/lib/pleroma/ecto_type/activity_pub/object_validators/language_code.ex
@@ -0,0 +1,25 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCode do
+ use Ecto.Type
+
+ import Pleroma.Web.CommonAPI.Utils, only: [is_good_locale_code?: 1]
+
+ def type, do: :string
+
+ def cast(language) when is_binary(language) do
+ if is_good_locale_code?(language) do
+ {:ok, language}
+ else
+ {:error, :invalid_language}
+ end
+ end
+
+ def cast(_), do: :error
+
+ def dump(data), do: {:ok, data}
+
+ def load(data), do: {:ok, data}
+end
diff --git a/lib/pleroma/web/activity_pub/object_validators/common_fields.ex b/lib/pleroma/web/activity_pub/object_validators/common_fields.ex
index 5ed3ea023..7ba393270 100644
--- a/lib/pleroma/web/activity_pub/object_validators/common_fields.ex
+++ b/lib/pleroma/web/activity_pub/object_validators/common_fields.ex
@@ -57,7 +57,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonFields do
field(:replies_count, :integer, default: 0)
field(:like_count, :integer, default: 0)
field(:announcement_count, :integer, default: 0)
- field(:language, :string)
+ field(:language, ObjectValidators.LanguageCode)
field(:inReplyTo, ObjectValidators.ObjectID)
field(:url, ObjectValidators.BareUri)
diff --git a/test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.ex b/test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.exs
index 226383c3c..760ecb465 100644
--- a/test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.ex
+++ b/test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.exs
@@ -9,17 +9,17 @@ defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.BareUriTest do
test "diaspora://" do
text = "diaspora://alice@fediverse.example/post/deadbeefdeadbeefdeadbeefdeadbeef"
- assert {:ok, text} = BareUri.cast(text)
+ assert {:ok, ^text} = BareUri.cast(text)
end
test "nostr:" do
text = "nostr:note1gwdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
- assert {:ok, text} = BareUri.cast(text)
+ assert {:ok, ^text} = BareUri.cast(text)
end
test "errors for non-URIs" do
- assert :error == SafeText.cast(1)
- assert :error == SafeText.cast("foo")
- assert :error == SafeText.cast("foo bar")
+ assert :error == BareUri.cast(1)
+ assert :error == BareUri.cast("foo")
+ assert :error == BareUri.cast("foo bar")
end
end
diff --git a/test/pleroma/ecto_type/activity_pub/object_validators/language_code.exs b/test/pleroma/ecto_type/activity_pub/object_validators/language_code.exs
new file mode 100644
index 000000000..2261cc209
--- /dev/null
+++ b/test/pleroma/ecto_type/activity_pub/object_validators/language_code.exs
@@ -0,0 +1,28 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCodeTest do
+ use Pleroma.DataCase, async: true
+
+ alias Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCode
+
+ test "it accepts language code" do
+ text = "pl"
+ assert {:ok, ^text} = LanguageCode.cast(text)
+ end
+
+ test "it accepts language code with region" do
+ text = "pl-PL"
+ assert {:ok, ^text} = LanguageCode.cast(text)
+ end
+
+ test "errors for invalid language code" do
+ assert {:error, :invalid_language} = LanguageCode.cast("ru_RU")
+ assert {:error, :invalid_language} = LanguageCode.cast(" ")
+ end
+
+ test "errors for non-text" do
+ assert :error == LanguageCode.cast(42)
+ end
+end