First
[anni] / lib / pleroma / ecto_type / activity_pub / object_validators / emoji.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.Emoji do
6   use Ecto.Type
7
8   def type, do: :map
9
10   def cast(data) when is_map(data) do
11     has_invalid_emoji? =
12       Enum.find(data, fn
13         {name, uri} when is_binary(name) and is_binary(uri) ->
14           # based on ObjectValidators.Uri.cast()
15           case URI.parse(uri) do
16             %URI{host: nil} -> true
17             %URI{host: ""} -> true
18             %URI{scheme: scheme} when scheme in ["https", "http"] -> false
19             _ -> true
20           end
21
22         {_name, _uri} ->
23           true
24       end)
25
26     if has_invalid_emoji?, do: :error, else: {:ok, data}
27   end
28
29   def cast(_data), do: :error
30
31   def dump(data), do: {:ok, data}
32
33   def load(data), do: {:ok, data}
34 end