Activity Expiration: Switch to 'expires_in' system.
authorlain <lain@soykaf.club>
Sat, 24 Aug 2019 15:22:48 +0000 (17:22 +0200)
committerlain <lain@soykaf.club>
Sat, 24 Aug 2019 15:22:48 +0000 (17:22 +0200)
lib/pleroma/web/common_api/common_api.ex
test/web/common_api/common_api_test.exs
test/web/mastodon_api/mastodon_api_controller_test.exs

index 69120cc19338548bf6f9fd59b51e6a70826f3d7f..5faddc9f4e37f2e714104a193301b96a33a0dbef 100644 (file)
@@ -201,16 +201,23 @@ defmodule Pleroma.Web.CommonAPI 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
+
+  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])
 
@@ -237,7 +244,7 @@ defmodule Pleroma.Web.CommonAPI 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 <-
index 5fda91438a7c514119132ee3122f2f6936b19530..f28a6609070c99063bf1ca3189a9f084dbb361a0 100644 (file)
@@ -213,10 +213,8 @@ defmodule Pleroma.Web.CommonAPITest 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
index c05c39db6df72d4dc3988d29230c0c43ec179d4a..6fcdc19aaa2a56da12be2efb8b590e902fbb6f4c 100644 (file)
@@ -153,7 +153,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest 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 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest 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