summaryrefslogtreecommitdiff
path: root/lib/pleroma/ecto_type/activity_pub/object_validators/emoji.ex
blob: 4aacc5c88f621c674c7ca2eea71b0370ac89fffe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.Emoji do
  use Ecto.Type

  def type, do: :map

  def cast(data) when is_map(data) do
    has_invalid_emoji? =
      Enum.find(data, fn
        {name, uri} when is_binary(name) and is_binary(uri) ->
          # based on ObjectValidators.Uri.cast()
          case URI.parse(uri) do
            %URI{host: nil} -> true
            %URI{host: ""} -> true
            %URI{scheme: scheme} when scheme in ["https", "http"] -> false
            _ -> true
          end

        {_name, _uri} ->
          true
      end)

    if has_invalid_emoji?, do: :error, else: {:ok, data}
  end

  def cast(_data), do: :error

  def dump(data), do: {:ok, data}

  def load(data), do: {:ok, data}
end