Activity Expiration: Switch to 'expires_in' system.

This commit is contained in:
lain 2019-08-24 17:22:48 +02:00
parent 1692fa8945
commit efb8818e9e
3 changed files with 18 additions and 10 deletions

View File

@ -201,16 +201,23 @@ defmodule Pleroma.Web.CommonAPI do
end end
end end
defp check_expiry_date(expiry_str) do defp check_expiry_date({:ok, nil} = res), do: res
{:ok, expiry} = Ecto.Type.cast(:naive_datetime, expiry_str)
if is_nil(expiry) || ActivityExpiration.expires_late_enough?(expiry) do defp check_expiry_date({:ok, in_seconds}) do
expiry = NaiveDateTime.utc_now() |> NaiveDateTime.add(in_seconds)
if ActivityExpiration.expires_late_enough?(expiry) do
{:ok, expiry} {:ok, expiry}
else else
{:error, "Expiry date is too soon"} {:error, "Expiry date is too soon"}
end end
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 def post(user, %{"status" => status} = data) do
limit = Pleroma.Config.get([:instance, :limit]) limit = Pleroma.Config.get([:instance, :limit])
@ -237,7 +244,7 @@ defmodule Pleroma.Web.CommonAPI do
context <- make_context(in_reply_to, in_reply_to_conversation), context <- make_context(in_reply_to, in_reply_to_conversation),
cw <- data["spoiler_text"] || "", cw <- data["spoiler_text"] || "",
sensitive <- data["sensitive"] || Enum.member?(tags, {"#nsfw", "nsfw"}), 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), full_payload <- String.trim(status <> cw),
:ok <- validate_character_limit(full_payload, attachments, limit), :ok <- validate_character_limit(full_payload, attachments, limit),
object <- object <-

View File

@ -213,10 +213,8 @@ defmodule Pleroma.Web.CommonAPITest do
|> NaiveDateTime.truncate(:second) |> NaiveDateTime.truncate(:second)
|> NaiveDateTime.add(1_000_000, :second) |> NaiveDateTime.add(1_000_000, :second)
expires_at_iso8601 = expires_at |> NaiveDateTime.to_iso8601()
assert {:ok, activity} = 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 = Pleroma.ActivityExpiration.get_by_activity_id(activity.id)
assert expiration.scheduled_at == expires_at assert expiration.scheduled_at == expires_at

View File

@ -153,7 +153,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
refute id == third_id refute id == third_id
# An activity that will expire: # An activity that will expire:
expires_in = 120 # 2 hours
expires_in = 120 * 60
conn_four = conn_four =
conn conn
@ -168,12 +169,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
estimated_expires_at = estimated_expires_at =
NaiveDateTime.utc_now() NaiveDateTime.utc_now()
|> NaiveDateTime.add(:timer.minutes(expires_in), :millisecond) |> NaiveDateTime.add(expires_in)
|> NaiveDateTime.truncate(:second) |> NaiveDateTime.truncate(:second)
# This assert will fail if the test takes longer than a minute. I sure hope it never does: # 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 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 end
test "replying to a status", %{conn: conn} do test "replying to a status", %{conn: conn} do