end
end
- defp check_expiry_date(expiry_str) do
- {:ok, expiry} = Ecto.Type.cast(:naive_datetime, expiry_str)
+ defp check_expiry_date({:ok, nil} = res), do: res
+
+ defp check_expiry_date({:ok, in_seconds}) do
+ expiry = NaiveDateTime.utc_now() |> NaiveDateTime.add(in_seconds)
- if is_nil(expiry) || ActivityExpiration.expires_late_enough?(expiry) do
+ if ActivityExpiration.expires_late_enough?(expiry) do
{:ok, expiry}
else
{:error, "Expiry date is too soon"}
end
end
+ defp check_expiry_date(expiry_str) do
+ Ecto.Type.cast(:integer, expiry_str)
+ |> check_expiry_date()
+ end
+
def post(user, %{"status" => status} = data) do
limit = Pleroma.Config.get([:instance, :limit])
context <- make_context(in_reply_to, in_reply_to_conversation),
cw <- data["spoiler_text"] || "",
sensitive <- data["sensitive"] || Enum.member?(tags, {"#nsfw", "nsfw"}),
- {:ok, expires_at} <- check_expiry_date(data["expires_at"]),
+ {:ok, expires_at} <- check_expiry_date(data["expires_in"]),
full_payload <- String.trim(status <> cw),
:ok <- validate_character_limit(full_payload, attachments, limit),
object <-
|> NaiveDateTime.truncate(:second)
|> NaiveDateTime.add(1_000_000, :second)
- expires_at_iso8601 = expires_at |> NaiveDateTime.to_iso8601()
-
assert {:ok, activity} =
- CommonAPI.post(user, %{"status" => "chai", "expires_at" => expires_at_iso8601})
+ CommonAPI.post(user, %{"status" => "chai", "expires_in" => 1_000_000})
assert expiration = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
assert expiration.scheduled_at == expires_at
refute id == third_id
# An activity that will expire:
- expires_in = 120
+ # 2 hours
+ expires_in = 120 * 60
conn_four =
conn
estimated_expires_at =
NaiveDateTime.utc_now()
- |> NaiveDateTime.add(:timer.minutes(expires_in), :millisecond)
+ |> NaiveDateTime.add(expires_in)
|> NaiveDateTime.truncate(:second)
# This assert will fail if the test takes longer than a minute. I sure hope it never does:
assert abs(NaiveDateTime.diff(expiration.scheduled_at, estimated_expires_at, :second)) < 60
- assert fourth_response["pleroma"]["expires_at"] == NaiveDateTime.to_iso8601(expiration.scheduled_at)
+
+ assert fourth_response["pleroma"]["expires_at"] ==
+ NaiveDateTime.to_iso8601(expiration.scheduled_at)
end
test "replying to a status", %{conn: conn} do