Transmogrifier: Extract user update handling tests.
[akkoma] / lib / pleroma / ecto_type / activity_pub / object_validators / date_time.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.DateTime do
6 @moduledoc """
7 The AP standard defines the date fields in AP as xsd:DateTime. Elixir's
8 DateTime can't parse this, but it can parse the related iso8601. This
9 module punches the date until it looks like iso8601 and normalizes to
10 it.
11
12 DateTimes without a timezone offset are treated as UTC.
13
14 Reference: https://www.w3.org/TR/activitystreams-vocabulary/#dfn-published
15 """
16 use Ecto.Type
17
18 def type, do: :string
19
20 def cast(datetime) when is_binary(datetime) do
21 with {:ok, datetime, _} <- DateTime.from_iso8601(datetime) do
22 {:ok, DateTime.to_iso8601(datetime)}
23 else
24 {:error, :missing_offset} -> cast("#{datetime}Z")
25 _e -> :error
26 end
27 end
28
29 def cast(_), do: :error
30
31 def dump(data) do
32 {:ok, data}
33 end
34
35 def load(data) do
36 {:ok, data}
37 end
38 end