From a92b1fbdedf1fafdc4a29993ffacd5bf70bfd84e Mon Sep 17 00:00:00 2001 From: Lain Soykaf Date: Sun, 23 Feb 2025 17:51:25 +0400 Subject: UserRelationshipTest: Don't use Mock. --- lib/pleroma/datetime.ex | 3 +++ lib/pleroma/datetime/impl.ex | 6 ++++++ lib/pleroma/user_relationship.ex | 10 ++++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 lib/pleroma/datetime.ex create mode 100644 lib/pleroma/datetime/impl.ex (limited to 'lib') 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]}, -- cgit v1.2.3 From ee291f08e82eee596e5e8651a8bbcf7dad147dc7 Mon Sep 17 00:00:00 2001 From: Lain Soykaf Date: Tue, 25 Feb 2025 16:40:45 +0400 Subject: AnonymizeFilename: Asyncify --- lib/pleroma/upload/filter/anonymize_filename.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/upload/filter/anonymize_filename.ex b/lib/pleroma/upload/filter/anonymize_filename.ex index 234ccb6bb..c0ad70368 100644 --- a/lib/pleroma/upload/filter/anonymize_filename.ex +++ b/lib/pleroma/upload/filter/anonymize_filename.ex @@ -10,7 +10,7 @@ defmodule Pleroma.Upload.Filter.AnonymizeFilename do """ @behaviour Pleroma.Upload.Filter - alias Pleroma.Config + @config_impl Application.compile_env(:pleroma, [__MODULE__, :config_impl], Pleroma.Config) alias Pleroma.Upload def filter(%Upload{name: name} = upload) do @@ -23,7 +23,7 @@ defmodule Pleroma.Upload.Filter.AnonymizeFilename do @spec predefined_name(String.t()) :: String.t() | nil defp predefined_name(extension) do - with name when not is_nil(name) <- Config.get([__MODULE__, :text]), + with name when not is_nil(name) <- @config_impl.get([__MODULE__, :text]), do: String.replace(name, "{extension}", extension) end -- cgit v1.2.3 From c31fabdebddbd434a9fcff57cb36613f52ebc6a2 Mon Sep 17 00:00:00 2001 From: Lain Soykaf Date: Tue, 25 Feb 2025 17:08:21 +0400 Subject: Mogrify/Mogrifun: Asyncify --- lib/pleroma/config.ex | 1 + lib/pleroma/config/getting.ex | 3 +++ lib/pleroma/mogrify.ex | 42 ++++++++++++++++++++++++++++++++++++ lib/pleroma/upload/filter/mogrify.ex | 17 ++++++++++----- 4 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 lib/pleroma/mogrify.ex (limited to 'lib') diff --git a/lib/pleroma/config.ex b/lib/pleroma/config.ex index cf1453c9b..1bc371dec 100644 --- a/lib/pleroma/config.ex +++ b/lib/pleroma/config.ex @@ -27,6 +27,7 @@ defmodule Pleroma.Config do Application.get_env(:pleroma, key, default) end + @impl true def get!(key) do value = get(key, nil) diff --git a/lib/pleroma/config/getting.ex b/lib/pleroma/config/getting.ex index ec93fd02a..adf764f89 100644 --- a/lib/pleroma/config/getting.ex +++ b/lib/pleroma/config/getting.ex @@ -5,10 +5,13 @@ defmodule Pleroma.Config.Getting do @callback get(any()) :: any() @callback get(any(), any()) :: any() + @callback get!(any()) :: any() def get(key), do: get(key, nil) def get(key, default), do: impl().get(key, default) + def get!(key), do: impl().get!(key) + def impl do Application.get_env(:pleroma, :config_impl, Pleroma.Config) end diff --git a/lib/pleroma/mogrify.ex b/lib/pleroma/mogrify.ex new file mode 100644 index 000000000..77725e8f2 --- /dev/null +++ b/lib/pleroma/mogrify.ex @@ -0,0 +1,42 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2022 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.MogrifyBehaviour do + @moduledoc """ + Behaviour for Mogrify operations. + This module defines the interface for Mogrify operations that can be mocked in tests. + """ + + @callback open(binary()) :: map() + @callback custom(map(), binary()) :: map() + @callback custom(map(), binary(), binary()) :: map() + @callback save(map(), keyword()) :: map() +end + +defmodule Pleroma.MogrifyWrapper do + @moduledoc """ + Default implementation of MogrifyBehaviour that delegates to Mogrify. + """ + @behaviour Pleroma.MogrifyBehaviour + + @impl true + def open(file) do + Mogrify.open(file) + end + + @impl true + def custom(image, action) do + Mogrify.custom(image, action) + end + + @impl true + def custom(image, action, options) do + Mogrify.custom(image, action, options) + end + + @impl true + def save(image, opts) do + Mogrify.save(image, opts) + end +end diff --git a/lib/pleroma/upload/filter/mogrify.ex b/lib/pleroma/upload/filter/mogrify.ex index d1e166022..7c7431db6 100644 --- a/lib/pleroma/upload/filter/mogrify.ex +++ b/lib/pleroma/upload/filter/mogrify.ex @@ -8,9 +8,16 @@ defmodule Pleroma.Upload.Filter.Mogrify do @type conversion :: action :: String.t() | {action :: String.t(), opts :: String.t()} @type conversions :: conversion() | [conversion()] + @config_impl Application.compile_env(:pleroma, [__MODULE__, :config_impl], Pleroma.Config) + @mogrify_impl Application.compile_env( + :pleroma, + [__MODULE__, :mogrify_impl], + Pleroma.MogrifyWrapper + ) + def filter(%Pleroma.Upload{tempfile: file, content_type: "image" <> _}) do try do - do_filter(file, Pleroma.Config.get!([__MODULE__, :args])) + do_filter(file, @config_impl.get!([__MODULE__, :args])) {:ok, :filtered} rescue e in ErlangError -> @@ -22,9 +29,9 @@ defmodule Pleroma.Upload.Filter.Mogrify do def do_filter(file, filters) do file - |> Mogrify.open() + |> @mogrify_impl.open() |> mogrify_filter(filters) - |> Mogrify.save(in_place: true) + |> @mogrify_impl.save(in_place: true) end defp mogrify_filter(mogrify, nil), do: mogrify @@ -38,10 +45,10 @@ defmodule Pleroma.Upload.Filter.Mogrify do defp mogrify_filter(mogrify, []), do: mogrify defp mogrify_filter(mogrify, {action, options}) do - Mogrify.custom(mogrify, action, options) + @mogrify_impl.custom(mogrify, action, options) end defp mogrify_filter(mogrify, action) when is_binary(action) do - Mogrify.custom(mogrify, action) + @mogrify_impl.custom(mogrify, action) end end -- cgit v1.2.3