From 2ce4f96f6ef535ede6dd4fa6a91fff45690f053d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charlotte=20=F0=9F=A6=9D=20Delenk?= Date: Sat, 7 Jan 2023 21:58:56 +0100 Subject: [PATCH 1/2] Use actual ISO8601 timestamps for masto API Elixir uses an incompatible superset of ISO8601 that breaks some clients --- lib/pleroma/web/common_api/utils.ex | 13 ++++++++++--- test/pleroma/web/common_api/utils_test.exs | 8 ++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index aee19a840..d0452c428 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -328,9 +328,16 @@ defmodule Pleroma.Web.CommonAPI.Utils do end def to_masto_date(%NaiveDateTime{} = date) do - date - |> NaiveDateTime.to_iso8601() - |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false) + # NOTE: Elixir’s ISO 8601 format is a superset of the real standard + # It supports negative years for example. + # ISO8601 only supports years before 1583 with mutual agreement + if date.year < 1583 do + "" + else + date + |> NaiveDateTime.to_iso8601() + |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false) + end end def to_masto_date(date) when is_binary(date) do diff --git a/test/pleroma/web/common_api/utils_test.exs b/test/pleroma/web/common_api/utils_test.exs index a88d7681a..40137a10e 100644 --- a/test/pleroma/web/common_api/utils_test.exs +++ b/test/pleroma/web/common_api/utils_test.exs @@ -498,6 +498,14 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do test "returns empty string when date invalid" do assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == "" end + + test "returns empty string when date is before the introduction of the Gregorian Calendar" do + assert Utils.to_masto_date("0621-01-01T00:00:00Z") == "" + end + + test "returns empty string when date is BCE" do + assert Utils.to_masto_date("-420-01-01T00:00:00Z") == "" + end end describe "maybe_notify_mentioned_recipients/2" do -- 2.34.1 From 8d55b8c6d94794c4566d1867a1c3600d522c68d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charlotte=20=F0=9F=A6=9D=20Delenk?= Date: Sun, 8 Jan 2023 09:15:23 +0100 Subject: [PATCH 2/2] Replace invalid/unsupported dates with with epoch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Masto API requires “ISO 8601 Datetime”, not “ISO 8601 Datetime or empty” --- lib/pleroma/web/common_api/utils.ex | 6 +++--- test/pleroma/web/common_api/utils_test.exs | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index d0452c428..345c5d10d 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -332,7 +332,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do # It supports negative years for example. # ISO8601 only supports years before 1583 with mutual agreement if date.year < 1583 do - "" + "1970-01-01T00:00:00Z" else date |> NaiveDateTime.to_iso8601() @@ -344,11 +344,11 @@ defmodule Pleroma.Web.CommonAPI.Utils do with {:ok, date} <- NaiveDateTime.from_iso8601(date) do to_masto_date(date) else - _ -> "" + _ -> "1970-01-01T00:00:00Z" end end - def to_masto_date(_), do: "" + def to_masto_date(_), do: "1970-01-01T00:00:00Z" defp shortname(name) do with max_length when max_length > 0 <- diff --git a/test/pleroma/web/common_api/utils_test.exs b/test/pleroma/web/common_api/utils_test.exs index 40137a10e..f56d21c70 100644 --- a/test/pleroma/web/common_api/utils_test.exs +++ b/test/pleroma/web/common_api/utils_test.exs @@ -495,16 +495,16 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z" end - test "returns empty string when date invalid" do - assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == "" + test "returns unix epoch when date invalid" do + assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == "1970-01-01T00:00:00Z" end - test "returns empty string when date is before the introduction of the Gregorian Calendar" do - assert Utils.to_masto_date("0621-01-01T00:00:00Z") == "" + test "returns unix epoch when date is before the introduction of the Gregorian Calendar" do + assert Utils.to_masto_date("0621-01-01T00:00:00Z") == "1970-01-01T00:00:00Z" end - test "returns empty string when date is BCE" do - assert Utils.to_masto_date("-420-01-01T00:00:00Z") == "" + test "returns unix epoch when date is BCE" do + assert Utils.to_masto_date("-0420-01-01T00:00:00Z") == "1970-01-01T00:00:00Z" end end -- 2.34.1