2019-07-22 12:16:20 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-02 05:08:45 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-07-22 12:16:20 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.ActivityExpiration do
|
|
|
|
use Ecto.Schema
|
|
|
|
|
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.ActivityExpiration
|
|
|
|
alias Pleroma.Repo
|
|
|
|
|
2019-07-22 14:46:20 +00:00
|
|
|
import Ecto.Changeset
|
2019-07-22 12:16:20 +00:00
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
@type t :: %__MODULE__{}
|
2019-07-23 14:33:45 +00:00
|
|
|
@min_activity_lifetime :timer.hours(1)
|
2019-07-22 12:16:20 +00:00
|
|
|
|
|
|
|
schema "activity_expirations" do
|
2019-09-18 14:54:31 +00:00
|
|
|
belongs_to(:activity, Activity, type: FlakeId.Ecto.CompatType)
|
2019-07-22 12:16:20 +00:00
|
|
|
field(:scheduled_at, :naive_datetime)
|
|
|
|
end
|
|
|
|
|
2020-08-08 12:29:40 +00:00
|
|
|
def changeset(%ActivityExpiration{} = expiration, attrs, validate_scheduled_at) do
|
2019-07-22 14:46:20 +00:00
|
|
|
expiration
|
|
|
|
|> cast(attrs, [:scheduled_at])
|
|
|
|
|> validate_required([:scheduled_at])
|
2020-08-08 12:29:40 +00:00
|
|
|
|> validate_scheduled_at(validate_scheduled_at)
|
2019-07-22 14:46:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_by_activity_id(activity_id) do
|
|
|
|
ActivityExpiration
|
|
|
|
|> where([exp], exp.activity_id == ^activity_id)
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2020-08-08 12:29:40 +00:00
|
|
|
def create(%Activity{} = activity, scheduled_at, validate_scheduled_at \\ true) do
|
2019-07-22 14:46:20 +00:00
|
|
|
%ActivityExpiration{activity_id: activity.id}
|
2020-08-08 12:29:40 +00:00
|
|
|
|> changeset(%{scheduled_at: scheduled_at}, validate_scheduled_at)
|
2019-07-22 14:46:20 +00:00
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
2019-07-22 12:16:20 +00:00
|
|
|
def due_expirations(offset \\ 0) do
|
|
|
|
naive_datetime =
|
|
|
|
NaiveDateTime.utc_now()
|
|
|
|
|> NaiveDateTime.add(offset, :millisecond)
|
|
|
|
|
|
|
|
ActivityExpiration
|
|
|
|
|> where([exp], exp.scheduled_at < ^naive_datetime)
|
2020-08-08 17:49:02 +00:00
|
|
|
|> limit(50)
|
2019-07-22 12:16:20 +00:00
|
|
|
|> Repo.all()
|
|
|
|
end
|
2019-07-23 14:33:45 +00:00
|
|
|
|
2020-08-08 12:29:40 +00:00
|
|
|
def validate_scheduled_at(changeset, false), do: changeset
|
|
|
|
|
|
|
|
def validate_scheduled_at(changeset, true) do
|
2019-07-23 14:33:45 +00:00
|
|
|
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)
|
2020-02-12 15:43:07 +00:00
|
|
|
diff > @min_activity_lifetime
|
2019-07-23 14:33:45 +00:00
|
|
|
end
|
2019-07-22 12:16:20 +00:00
|
|
|
end
|