From 005f7e20cae5ebc8960638e1a8f4982dad616401 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 7 Jun 2021 10:24:57 +0200 Subject: [PATCH] Remove compile-time dependencies for some config values Signed-off-by: Thomas Citharel --- lib/mix/tasks/mobilizon/media/clean_orphan.ex | 7 ++++--- lib/mix/tasks/mobilizon/users/clean.ex | 6 ++++-- lib/mobilizon/actors/actors.ex | 13 +++++-------- lib/service/clean_orphan_media.ex | 7 ++++--- lib/service/clean_unconfirmed_users.ex | 7 ++++--- lib/service/export/icalendar.ex | 8 +++++--- lib/service/workers/clean_orphan_media_worker.ex | 7 ++++--- .../workers/clean_unconfirmed_users_worker.ex | 7 ++++--- 8 files changed, 34 insertions(+), 28 deletions(-) diff --git a/lib/mix/tasks/mobilizon/media/clean_orphan.ex b/lib/mix/tasks/mobilizon/media/clean_orphan.ex index 7c0f84e5..7b3ee34e 100644 --- a/lib/mix/tasks/mobilizon/media/clean_orphan.ex +++ b/lib/mix/tasks/mobilizon/media/clean_orphan.ex @@ -8,8 +8,6 @@ defmodule Mix.Tasks.Mobilizon.Media.CleanOrphan do @shortdoc "Clean orphan media" - @grace_period Mobilizon.Config.get([:instance, :orphan_upload_grace_period_hours], 48) - @impl Mix.Task def run(options) do {options, [], []} = @@ -26,9 +24,12 @@ defmodule Mix.Tasks.Mobilizon.Media.CleanOrphan do ] ) + default_grace_period = + Mobilizon.Config.get([:instance, :orphan_upload_grace_period_hours], 48) + dry_run = Keyword.get(options, :dry_run, false) grace_period = Keyword.get(options, :days) - grace_period = if is_nil(grace_period), do: @grace_period, else: grace_period * 24 + grace_period = if is_nil(grace_period), do: default_grace_period, else: grace_period * 24 verbose = Keyword.get(options, :verbose, false) start_mobilizon() diff --git a/lib/mix/tasks/mobilizon/users/clean.ex b/lib/mix/tasks/mobilizon/users/clean.ex index 883d6de9..ff8fe764 100644 --- a/lib/mix/tasks/mobilizon/users/clean.ex +++ b/lib/mix/tasks/mobilizon/users/clean.ex @@ -8,7 +8,6 @@ defmodule Mix.Tasks.Mobilizon.Users.Clean do alias Mobilizon.Service.CleanUnconfirmedUsers @shortdoc "Clean unconfirmed users from Mobilizon" - @grace_period Mobilizon.Config.get([:instance, :unconfirmed_user_grace_period_hours], 48) @impl Mix.Task def run(options) do @@ -26,9 +25,12 @@ defmodule Mix.Tasks.Mobilizon.Users.Clean do ] ) + default_grace_period = + Mobilizon.Config.get([:instance, :unconfirmed_user_grace_period_hours], 48) + dry_run = Keyword.get(options, :dry_run, false) grace_period = Keyword.get(options, :days) - grace_period = if is_nil(grace_period), do: @grace_period, else: grace_period * 24 + grace_period = if is_nil(grace_period), do: default_grace_period, else: grace_period * 24 verbose = Keyword.get(options, :verbose, false) start_mobilizon() diff --git a/lib/mobilizon/actors/actors.ex b/lib/mobilizon/actors/actors.ex index ed5af1a6..ad4e454e 100644 --- a/lib/mobilizon/actors/actors.ex +++ b/lib/mobilizon/actors/actors.ex @@ -1740,17 +1740,14 @@ defmodule Mobilizon.Actors do from(a in query, where: a.preferred_username == ^name and is_nil(a.domain)) end - @own_domain Mobilizon.Config.instance_hostname() - - defp filter_by_name(query, [name, @own_domain]) do - filter_by_name(query, [name]) - end - defp filter_by_name(query, [name, domain]) do - from(a in query, where: a.preferred_username == ^name and a.domain == ^domain) + if domain == Mobilizon.Config.instance_hostname() do + filter_by_name(query, [name]) + else + where(query, [a], a.preferred_username == ^name and a.domain == ^domain) + end end - @spec filter_by_name(Ecto.Query.t(), boolean | nil) :: Ecto.Query.t() defp filter_followed_by_approved_status(query, nil), do: query defp filter_followed_by_approved_status(query, approved) do diff --git a/lib/service/clean_orphan_media.ex b/lib/service/clean_orphan_media.ex index 144d7496..6aa01e3e 100644 --- a/lib/service/clean_orphan_media.ex +++ b/lib/service/clean_orphan_media.ex @@ -9,8 +9,6 @@ defmodule Mobilizon.Service.CleanOrphanMedia do alias Mobilizon.Storage.Repo import Ecto.Query - @grace_period Mobilizon.Config.get([:instance, :orphan_upload_grace_period_hours], 48) - @doc """ Clean orphan media @@ -37,7 +35,10 @@ defmodule Mobilizon.Service.CleanOrphanMedia do @spec find_media(Keyword.t()) :: list(Media.t()) defp find_media(opts) do - grace_period = Keyword.get(opts, :grace_period, @grace_period) + default_grace_period = + Mobilizon.Config.get([:instance, :orphan_upload_grace_period_hours], 48) + + grace_period = Keyword.get(opts, :grace_period, default_grace_period) expiration_date = DateTime.add(DateTime.utc_now(), grace_period * -3600) Media diff --git a/lib/service/clean_unconfirmed_users.ex b/lib/service/clean_unconfirmed_users.ex index 145fe11c..d9e6e9ea 100644 --- a/lib/service/clean_unconfirmed_users.ex +++ b/lib/service/clean_unconfirmed_users.ex @@ -9,8 +9,6 @@ defmodule Mobilizon.Service.CleanUnconfirmedUsers do alias Mobilizon.Users.User import Ecto.Query - @grace_period Mobilizon.Config.get([:instance, :unconfirmed_user_grace_period_hours], 48) - @doc """ Clean unattached media @@ -49,7 +47,10 @@ defmodule Mobilizon.Service.CleanUnconfirmedUsers do @spec find_unconfirmed_users_to_clean(Keyword.t()) :: list(User.t()) defp find_unconfirmed_users_to_clean(opts) do - grace_period = Keyword.get(opts, :grace_period, @grace_period) + default_grace_period = + Mobilizon.Config.get([:instance, :unconfirmed_user_grace_period_hours], 48) + + grace_period = Keyword.get(opts, :grace_period, default_grace_period) expiration_date = DateTime.add(DateTime.utc_now(), grace_period * -3600) User diff --git a/lib/service/export/icalendar.ex b/lib/service/export/icalendar.ex index 750a2ac2..4984239f 100644 --- a/lib/service/export/icalendar.ex +++ b/lib/service/export/icalendar.ex @@ -10,8 +10,6 @@ defmodule Mobilizon.Service.Export.ICalendar do alias Mobilizon.Service.Export.Common alias Mobilizon.Service.Formatter.HTML - @vendor "Mobilizon #{Config.instance_version()}" - @doc """ Create cache for an actor, an event or an user token """ @@ -119,7 +117,7 @@ defmodule Mobilizon.Service.Export.ICalendar do @spec events_to_ics(list(Events.t())) :: String.t() defp events_to_ics(events) do %ICalendar{events: events |> Enum.map(&do_export_event/1)} - |> ICalendar.to_ics(vendor: @vendor) + |> ICalendar.to_ics(vendor: vendor()) end @spec do_export_event(Event.t()) :: ICalendar.Event.t() @@ -137,4 +135,8 @@ defmodule Mobilizon.Service.Export.ICalendar do categories: event.tags |> Enum.map(& &1.title) } end + + defp vendor do + "Mobilizon #{Config.instance_version()}" + end end diff --git a/lib/service/workers/clean_orphan_media_worker.ex b/lib/service/workers/clean_orphan_media_worker.ex index 04b6fa1a..566120b2 100644 --- a/lib/service/workers/clean_orphan_media_worker.ex +++ b/lib/service/workers/clean_orphan_media_worker.ex @@ -6,8 +6,6 @@ defmodule Mobilizon.Service.Workers.CleanOrphanMediaWorker do use Oban.Worker, queue: "background" alias Mobilizon.Service.CleanOrphanMedia - @grace_period Mobilizon.Config.get([:instance, :orphan_upload_grace_period_hours], 48) - @impl Oban.Worker def perform(%Job{}) do if Mobilizon.Config.get!([:instance, :remove_orphan_uploads]) and should_perform?() do @@ -19,9 +17,12 @@ defmodule Mobilizon.Service.Workers.CleanOrphanMediaWorker do defp should_perform? do case Cachex.get(:key_value, "last_media_cleanup") do {:ok, %DateTime{} = last_media_cleanup} -> + default_grace_period = + Mobilizon.Config.get([:instance, :orphan_upload_grace_period_hours], 48) + DateTime.compare( last_media_cleanup, - DateTime.add(DateTime.utc_now(), @grace_period * -3600) + DateTime.add(DateTime.utc_now(), default_grace_period * -3600) ) == :lt _ -> diff --git a/lib/service/workers/clean_unconfirmed_users_worker.ex b/lib/service/workers/clean_unconfirmed_users_worker.ex index 472f3a03..ef324caf 100644 --- a/lib/service/workers/clean_unconfirmed_users_worker.ex +++ b/lib/service/workers/clean_unconfirmed_users_worker.ex @@ -6,8 +6,6 @@ defmodule Mobilizon.Service.Workers.CleanUnconfirmedUsersWorker do use Oban.Worker, queue: "background" alias Mobilizon.Service.CleanUnconfirmedUsers - @grace_period Mobilizon.Config.get([:instance, :unconfirmed_user_grace_period_hours], 48) - @impl Oban.Worker def perform(%Job{}) do if Mobilizon.Config.get!([:instance, :remove_unconfirmed_users]) and should_perform?() do @@ -19,9 +17,12 @@ defmodule Mobilizon.Service.Workers.CleanUnconfirmedUsersWorker do defp should_perform? do case Cachex.get(:key_value, "last_media_cleanup") do {:ok, %DateTime{} = last_media_cleanup} -> + default_grace_period = + Mobilizon.Config.get([:instance, :unconfirmed_user_grace_period_hours], 48) + DateTime.compare( last_media_cleanup, - DateTime.add(DateTime.utc_now(), @grace_period * -3600) + DateTime.add(DateTime.utc_now(), default_grace_period * -3600) ) == :lt _ ->