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
lib/pleroma/web/common_api
test/web

View File

@ -201,16 +201,23 @@ def get_replied_to_visibility(activity) do
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
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}
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])
@ -237,7 +244,7 @@ def post(user, %{"status" => status} = data) do
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 <-

View File

@ -213,10 +213,8 @@ test "it can handle activities that expire" do
|> 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

View File

@ -153,7 +153,8 @@ test "posting a status", %{conn: conn} do
refute id == third_id
# An activity that will expire:
expires_in = 120
# 2 hours
expires_in = 120 * 60
conn_four =
conn
@ -168,12 +169,14 @@ test "posting a status", %{conn: conn} do
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