summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormarcin mikołajczak <git@mkljczk.pl>2023-09-07 15:14:18 +0200
committermarcin mikołajczak <git@mkljczk.pl>2023-09-11 00:33:53 +0200
commita3b17dac0bf5da57cbd08335379ddfe4f8919bc3 (patch)
tree35fa5b1b1b0effe547fa86e38356b239412ff936 /lib
parentc5ed684273fa329bc955c59dbc7beed9804fb0f3 (diff)
downloadpleroma-a3b17dac0bf5da57cbd08335379ddfe4f8919bc3.tar.gz
pleroma-a3b17dac0bf5da57cbd08335379ddfe4f8919bc3.zip
Rename test
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/ecto_type/activity_pub/object_validators/content_language_map.ex49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/pleroma/ecto_type/activity_pub/object_validators/content_language_map.ex b/lib/pleroma/ecto_type/activity_pub/object_validators/content_language_map.ex
new file mode 100644
index 000000000..2cc0fda00
--- /dev/null
+++ b/lib/pleroma/ecto_type/activity_pub/object_validators/content_language_map.ex
@@ -0,0 +1,49 @@
+# 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.ContentLanguageMap do
+ use Ecto.Type
+
+ import Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCode,
+ only: [is_good_locale_code?: 1]
+
+ def type, do: :map
+
+ def cast(%{} = object) do
+ with {status, %{} = data} when status in [:modified, :ok] <- validate_map(object) do
+ {:ok, data}
+ else
+ {_, nil} -> {:ok, nil}
+ {:error, _} -> :error
+ end
+ end
+
+ def cast(_), do: :error
+
+ def dump(data), do: {:ok, data}
+
+ def load(data), do: {:ok, data}
+
+ defp validate_map(%{} = object) do
+ {status, data} =
+ object
+ |> Enum.reduce({:ok, %{}}, fn
+ {lang, value}, {status, acc} when is_binary(lang) and is_binary(value) ->
+ if is_good_locale_code?(lang) do
+ {status, Map.put(acc, lang, value)}
+ else
+ {:modified, acc}
+ end
+
+ _, {_status, acc} ->
+ {:modified, acc}
+ end)
+
+ if data == %{} do
+ {status, nil}
+ else
+ {status, data}
+ end
+ end
+end