X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fpleroma%2Factivity_expiration.ex;h=db9c88d84b4a5039e22620d3333b6247e8a3662f;hb=1be6b3056e97654612f377eaf3c8d80de6d8d77f;hp=a0af5255b7ab8415a3bf06ea32e7fe361f119687;hpb=704960b3c135d2e050308c68f5ccf5d7b7df40f8;p=akkoma
diff --git a/lib/pleroma/activity_expiration.ex b/lib/pleroma/activity_expiration.ex
index a0af5255b..db9c88d84 100644
--- a/lib/pleroma/activity_expiration.ex
+++ b/lib/pleroma/activity_expiration.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.ActivityExpiration do
@@ -7,16 +7,16 @@ defmodule Pleroma.ActivityExpiration do
alias Pleroma.Activity
alias Pleroma.ActivityExpiration
- alias Pleroma.FlakeId
alias Pleroma.Repo
import Ecto.Changeset
import Ecto.Query
@type t :: %__MODULE__{}
+ @min_activity_lifetime :timer.hours(1)
schema "activity_expirations" do
- belongs_to(:activity, Activity, type: FlakeId)
+ belongs_to(:activity, Activity, type: FlakeId.Ecto.CompatType)
field(:scheduled_at, :naive_datetime)
end
@@ -24,6 +24,7 @@ defmodule Pleroma.ActivityExpiration do
expiration
|> cast(attrs, [:scheduled_at])
|> validate_required([:scheduled_at])
+ |> validate_scheduled_at()
end
def get_by_activity_id(activity_id) do
@@ -47,4 +48,20 @@ defmodule Pleroma.ActivityExpiration do
|> where([exp], exp.scheduled_at < ^naive_datetime)
|> Repo.all()
end
+
+ def validate_scheduled_at(changeset) do
+ validate_change(changeset, :scheduled_at, fn _, scheduled_at ->
+ if not expires_late_enough?(scheduled_at) do
+ [scheduled_at: "an ephemeral activity must live for at least one hour"]
+ else
+ []
+ end
+ end)
+ end
+
+ def expires_late_enough?(scheduled_at) do
+ now = NaiveDateTime.utc_now()
+ diff = NaiveDateTime.diff(scheduled_at, now, :millisecond)
+ diff > @min_activity_lifetime
+ end
end