summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/datetime.ex3
-rw-r--r--lib/pleroma/datetime/impl.ex6
-rw-r--r--lib/pleroma/user_relationship.ex10
3 files changed, 17 insertions, 2 deletions
diff --git a/lib/pleroma/datetime.ex b/lib/pleroma/datetime.ex
new file mode 100644
index 000000000..d79cb848b
--- /dev/null
+++ b/lib/pleroma/datetime.ex
@@ -0,0 +1,3 @@
+defmodule Pleroma.DateTime do
+ @callback utc_now() :: NaiveDateTime.t()
+end
diff --git a/lib/pleroma/datetime/impl.ex b/lib/pleroma/datetime/impl.ex
new file mode 100644
index 000000000..102be047b
--- /dev/null
+++ b/lib/pleroma/datetime/impl.ex
@@ -0,0 +1,6 @@
+defmodule Pleroma.DateTime.Impl do
+ @behaviour Pleroma.DateTime
+
+ @impl true
+ def utc_now, do: NaiveDateTime.utc_now()
+end
diff --git a/lib/pleroma/user_relationship.ex b/lib/pleroma/user_relationship.ex
index 82fcc1cdd..5b48d321a 100644
--- a/lib/pleroma/user_relationship.ex
+++ b/lib/pleroma/user_relationship.ex
@@ -55,9 +55,13 @@ defmodule Pleroma.UserRelationship do
def user_relationship_mappings, do: Pleroma.UserRelationship.Type.__enum_map__()
+ def datetime_impl do
+ Application.get_env(:pleroma, :datetime_impl, Pleroma.DateTime.Impl)
+ end
+
def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
user_relationship
- |> cast(params, [:relationship_type, :source_id, :target_id, :expires_at])
+ |> cast(params, [:relationship_type, :source_id, :target_id, :expires_at, :inserted_at])
|> validate_required([:relationship_type, :source_id, :target_id])
|> unique_constraint(:relationship_type,
name: :user_relationships_source_id_relationship_type_target_id_index
@@ -65,6 +69,7 @@ defmodule Pleroma.UserRelationship do
|> validate_not_self_relationship()
end
+ @spec exists?(any(), Pleroma.User.t(), Pleroma.User.t()) :: boolean()
def exists?(relationship_type, %User{} = source, %User{} = target) do
UserRelationship
|> where(relationship_type: ^relationship_type, source_id: ^source.id, target_id: ^target.id)
@@ -90,7 +95,8 @@ defmodule Pleroma.UserRelationship do
relationship_type: relationship_type,
source_id: source.id,
target_id: target.id,
- expires_at: expires_at
+ expires_at: expires_at,
+ inserted_at: datetime_impl().utc_now()
})
|> Repo.insert(
on_conflict: {:replace_all_except, [:id, :inserted_at]},