summaryrefslogtreecommitdiff
path: root/lib/pleroma/ecto_type/activity_pub/object_validators/recipients.ex
blob: af4b0e52779524291822a94ec552d97e020e0a88 (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
35
36
37
38
39
40
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

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

  alias Pleroma.EctoType.ActivityPub.ObjectValidators.ObjectID

  def type, do: {:array, ObjectID}

  def cast(object) when is_binary(object) do
    cast([object])
  end

  def cast(data) when is_list(data) do
    data
    |> Enum.reduce_while({:ok, []}, fn element, {:ok, list} ->
      case ObjectID.cast(element) do
        {:ok, id} ->
          {:cont, {:ok, [id | list]}}

        _ ->
          {:halt, :error}
      end
    end)
  end

  def cast(_) do
    :error
  end

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

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