Remove compile-time dependencies for some config values

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-06-07 10:24:57 +02:00
parent 4f0041ae31
commit 005f7e20ca
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
8 changed files with 34 additions and 28 deletions

View file

@ -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()

View file

@ -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()

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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
_ ->

View file

@ -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
_ ->