pleroma/lib/pleroma/ecto_type/activity_pub/object_validators/object_id.ex

28 lines
741 B
Elixir
Raw Normal View History

# Pleroma: A lightweight social networking server
2022-02-26 07:11:42 +01:00
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.ObjectID do
use Ecto.Type
def type, do: :string
def cast(object) when is_binary(object) do
2020-04-06 13:53:24 +02:00
# Host has to be present and scheme has to be an http scheme (for now)
case URI.parse(object) do
%URI{host: nil} -> :error
%URI{host: ""} -> :error
%URI{scheme: scheme} when scheme in ["https", "http"] -> {:ok, object}
_ -> :error
2020-03-31 16:11:38 +02:00
end
end
2020-03-31 16:11:38 +02:00
def cast(%{"id" => object}), do: cast(object)
2020-04-09 13:01:35 +02:00
def cast(_), do: :error
2020-04-09 13:01:35 +02:00
def dump(data), do: {:ok, data}
2020-04-09 13:01:35 +02:00
def load(data), do: {:ok, data}
end