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