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