2019-09-08 00:05:54 +02:00
|
|
|
defmodule Mobilizon.Config do
|
|
|
|
@moduledoc """
|
|
|
|
Configuration wrapper.
|
|
|
|
"""
|
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
alias Mobilizon.Actors
|
2023-08-17 13:16:58 +02:00
|
|
|
alias Mobilizon.Admin.Setting
|
2020-06-19 19:27:10 +02:00
|
|
|
alias Mobilizon.Service.GitStatus
|
2021-09-10 11:36:05 +02:00
|
|
|
require Logger
|
2021-10-04 18:59:41 +02:00
|
|
|
import Mobilizon.Service.Export.Participants.Common, only: [enabled_formats: 0]
|
2019-12-20 13:04:34 +01:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@type mobilizon_config :: [
|
|
|
|
name: String.t(),
|
|
|
|
description: String.t(),
|
|
|
|
hostname: String.t(),
|
|
|
|
registrations_open: boolean(),
|
|
|
|
languages: list(String.t()),
|
|
|
|
default_language: String.t(),
|
|
|
|
registration_email_allowlist: list(String.t()),
|
|
|
|
registration_email_denylist: list(String.t()),
|
|
|
|
demo: boolean(),
|
|
|
|
repository: String.t(),
|
|
|
|
email_from: String.t(),
|
|
|
|
email_reply_to: String.t(),
|
|
|
|
federating: boolean(),
|
|
|
|
remove_orphan_uploads: boolean()
|
|
|
|
]
|
|
|
|
|
|
|
|
@spec instance_config :: mobilizon_config
|
2019-09-08 00:05:54 +02:00
|
|
|
def instance_config, do: Application.get_env(:mobilizon, :instance)
|
|
|
|
|
2023-08-17 13:16:58 +02:00
|
|
|
@spec db_instance_config :: list(Setting.t())
|
|
|
|
def db_instance_config, do: Mobilizon.Admin.get_all_admin_settings()
|
|
|
|
|
|
|
|
@spec config_cache :: map()
|
|
|
|
def config_cache do
|
|
|
|
case Cachex.fetch(:config, :all_db_config, fn _key ->
|
|
|
|
value =
|
|
|
|
Enum.reduce(
|
|
|
|
Mobilizon.Admin.get_all_admin_settings(),
|
|
|
|
%{},
|
|
|
|
&arrange_values/2
|
|
|
|
)
|
|
|
|
|
|
|
|
{:commit, value}
|
|
|
|
end) do
|
|
|
|
{status, value} when status in [:ok, :commit] -> value
|
|
|
|
_err -> %{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec arrange_values(Setting.t(), map()) :: map()
|
|
|
|
defp arrange_values(setting, acc) do
|
|
|
|
{_, new_data} =
|
|
|
|
Map.get_and_update(acc, setting.group, fn current_value ->
|
|
|
|
new_value = current_value || %{}
|
|
|
|
|
|
|
|
{current_value, Map.put(new_value, setting.name, process_value(setting.value))}
|
|
|
|
end)
|
|
|
|
|
|
|
|
new_data
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec process_value(String.t() | nil) :: any()
|
|
|
|
defp process_value(nil), do: nil
|
|
|
|
defp process_value(""), do: nil
|
|
|
|
|
|
|
|
defp process_value(value) do
|
|
|
|
case Jason.decode(value) do
|
|
|
|
{:ok, val} ->
|
|
|
|
val
|
|
|
|
|
|
|
|
{:error, _} ->
|
|
|
|
case value do
|
|
|
|
"true" -> true
|
|
|
|
"false" -> false
|
|
|
|
value -> value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec config_cached_value(String.t(), String.t(), String.t()) :: any()
|
|
|
|
def config_cached_value(group, name, fallback \\ nil) do
|
|
|
|
config_cache()
|
|
|
|
|> Map.get(group, %{})
|
|
|
|
|> Map.get(name, fallback)
|
|
|
|
end
|
|
|
|
|
2019-09-08 00:05:54 +02:00
|
|
|
@spec instance_name :: String.t()
|
2019-12-20 13:04:34 +01:00
|
|
|
def instance_name,
|
|
|
|
do:
|
2023-08-17 13:16:58 +02:00
|
|
|
config_cached_value(
|
2019-12-20 13:04:34 +01:00
|
|
|
"instance",
|
|
|
|
"instance_name",
|
|
|
|
instance_config()[:name]
|
|
|
|
)
|
2019-09-08 00:05:54 +02:00
|
|
|
|
|
|
|
@spec instance_description :: String.t()
|
2019-12-20 13:04:34 +01:00
|
|
|
def instance_description,
|
|
|
|
do:
|
2023-08-17 13:16:58 +02:00
|
|
|
config_cached_value(
|
2019-12-20 13:04:34 +01:00
|
|
|
"instance",
|
|
|
|
"instance_description",
|
|
|
|
instance_config()[:description]
|
|
|
|
)
|
|
|
|
|
2020-06-19 19:27:10 +02:00
|
|
|
@spec instance_long_description :: String.t()
|
|
|
|
def instance_long_description,
|
|
|
|
do:
|
2023-08-17 13:16:58 +02:00
|
|
|
config_cached_value(
|
2020-06-19 19:27:10 +02:00
|
|
|
"instance",
|
|
|
|
"instance_long_description"
|
|
|
|
)
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec instance_slogan :: String.t() | nil
|
2023-08-17 13:16:58 +02:00
|
|
|
def instance_slogan, do: config_cached_value("instance", "instance_slogan")
|
2020-10-21 17:49:18 +02:00
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec contact :: String.t() | nil
|
2020-06-19 19:27:10 +02:00
|
|
|
def contact do
|
2023-08-17 13:16:58 +02:00
|
|
|
config_cached_value("instance", "contact")
|
2020-06-19 19:27:10 +02:00
|
|
|
end
|
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
@spec instance_terms(String.t()) :: String.t()
|
|
|
|
def instance_terms(locale \\ "en") do
|
2023-08-17 13:16:58 +02:00
|
|
|
config_cached_value("instance", "instance_terms", generate_terms(locale))
|
2019-12-20 13:04:34 +01:00
|
|
|
end
|
|
|
|
|
2020-06-19 19:27:10 +02:00
|
|
|
@spec instance_terms_type :: String.t()
|
2019-12-20 13:04:34 +01:00
|
|
|
def instance_terms_type do
|
2023-08-17 13:16:58 +02:00
|
|
|
config_cached_value("instance", "instance_terms_type", "DEFAULT")
|
2019-12-20 13:04:34 +01:00
|
|
|
end
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec instance_terms_url :: String.t() | nil
|
2019-12-20 13:04:34 +01:00
|
|
|
def instance_terms_url do
|
2023-08-17 13:16:58 +02:00
|
|
|
config_cached_value("instance", "instance_terms_url")
|
2019-12-20 13:04:34 +01:00
|
|
|
end
|
2019-09-08 00:05:54 +02:00
|
|
|
|
2020-06-19 19:27:10 +02:00
|
|
|
@spec instance_privacy(String.t()) :: String.t()
|
|
|
|
def instance_privacy(locale \\ "en") do
|
2023-08-17 13:16:58 +02:00
|
|
|
config_cached_value(
|
2020-06-19 19:27:10 +02:00
|
|
|
"instance",
|
|
|
|
"instance_privacy_policy",
|
|
|
|
generate_privacy(locale)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec instance_privacy_type :: String.t()
|
|
|
|
def instance_privacy_type do
|
2023-08-17 13:16:58 +02:00
|
|
|
config_cached_value("instance", "instance_privacy_policy_type", "DEFAULT")
|
2020-06-19 19:27:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec instance_privacy_url :: String.t()
|
|
|
|
def instance_privacy_url do
|
2023-08-17 13:16:58 +02:00
|
|
|
config_cached_value("instance", "instance_privacy_policy_url")
|
2020-06-19 19:27:10 +02:00
|
|
|
end
|
|
|
|
|
2020-06-15 11:01:49 +02:00
|
|
|
@spec instance_rules :: String.t()
|
|
|
|
def instance_rules do
|
2023-08-17 13:16:58 +02:00
|
|
|
config_cached_value("instance", "instance_rules")
|
2020-06-15 11:01:49 +02:00
|
|
|
end
|
|
|
|
|
2019-09-08 00:05:54 +02:00
|
|
|
@spec instance_version :: String.t()
|
2020-06-19 19:27:10 +02:00
|
|
|
def instance_version do
|
|
|
|
GitStatus.commit()
|
|
|
|
end
|
2019-09-08 00:05:54 +02:00
|
|
|
|
|
|
|
@spec instance_hostname :: String.t()
|
|
|
|
def instance_hostname, do: instance_config()[:hostname]
|
|
|
|
|
|
|
|
@spec instance_registrations_open? :: boolean
|
2019-12-20 13:04:34 +01:00
|
|
|
def instance_registrations_open?,
|
|
|
|
do:
|
|
|
|
to_boolean(
|
2023-08-17 13:16:58 +02:00
|
|
|
config_cached_value(
|
2019-12-20 13:04:34 +01:00
|
|
|
"instance",
|
|
|
|
"registrations_open",
|
|
|
|
instance_config()[:registrations_open]
|
|
|
|
)
|
|
|
|
)
|
2019-09-08 00:05:54 +02:00
|
|
|
|
2020-10-07 15:37:23 +02:00
|
|
|
@spec instance_languages :: list(String.t())
|
|
|
|
def instance_languages,
|
|
|
|
do:
|
2023-08-17 13:16:58 +02:00
|
|
|
config_cached_value(
|
2020-10-07 15:37:23 +02:00
|
|
|
"instance",
|
|
|
|
"instance_languages",
|
|
|
|
instance_config()[:languages]
|
|
|
|
)
|
|
|
|
|
2021-08-09 19:29:15 +02:00
|
|
|
@spec default_language :: String.t()
|
|
|
|
def default_language, do: instance_config()[:default_language]
|
|
|
|
|
2020-09-29 09:53:48 +02:00
|
|
|
@spec instance_registrations_allowlist :: list(String.t())
|
|
|
|
def instance_registrations_allowlist, do: instance_config()[:registration_email_allowlist]
|
2019-12-17 12:09:24 +01:00
|
|
|
|
2020-09-29 09:53:48 +02:00
|
|
|
@spec instance_registrations_allowlist? :: boolean
|
|
|
|
def instance_registrations_allowlist?, do: length(instance_registrations_allowlist()) > 0
|
2019-12-17 12:09:24 +01:00
|
|
|
|
2021-08-08 19:46:39 +02:00
|
|
|
@spec instance_registrations_denylist :: list(String.t())
|
|
|
|
def instance_registrations_denylist, do: instance_config()[:registration_email_denylist]
|
|
|
|
|
2019-11-21 16:07:43 +01:00
|
|
|
@spec instance_demo_mode? :: boolean
|
|
|
|
def instance_demo_mode?, do: to_boolean(instance_config()[:demo])
|
|
|
|
|
2019-09-08 00:05:54 +02:00
|
|
|
@spec instance_repository :: String.t()
|
|
|
|
def instance_repository, do: instance_config()[:repository]
|
|
|
|
|
|
|
|
@spec instance_email_from :: String.t()
|
|
|
|
def instance_email_from, do: instance_config()[:email_from]
|
|
|
|
|
|
|
|
@spec instance_email_reply_to :: String.t()
|
|
|
|
def instance_email_reply_to, do: instance_config()[:email_reply_to]
|
|
|
|
|
2019-10-11 17:20:03 +02:00
|
|
|
@spec instance_user_agent :: String.t()
|
|
|
|
def instance_user_agent,
|
2020-07-09 17:24:28 +02:00
|
|
|
do: "#{instance_hostname()} - Mobilizon #{instance_version()}"
|
2020-06-19 19:27:10 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec instance_federating :: boolean()
|
2020-06-19 19:27:10 +02:00
|
|
|
def instance_federating, do: instance_config()[:federating]
|
2019-10-11 17:20:03 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec instance_geocoding_provider :: module()
|
2019-11-20 13:49:57 +01:00
|
|
|
def instance_geocoding_provider,
|
|
|
|
do: get_in(Application.get_env(:mobilizon, Mobilizon.Service.Geospatial), [:service])
|
|
|
|
|
|
|
|
@spec instance_geocoding_autocomplete :: boolean
|
|
|
|
def instance_geocoding_autocomplete,
|
|
|
|
do: instance_geocoding_provider() !== Mobilizon.Service.Geospatial.Nominatim
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec maps_config :: [
|
|
|
|
tiles: [endpoint: String.t(), attribution: String.t()],
|
|
|
|
rounting: [type: atom]
|
|
|
|
]
|
|
|
|
defp maps_config, do: Application.get_env(:mobilizon, :maps)
|
|
|
|
|
2019-11-20 13:49:57 +01:00
|
|
|
@spec instance_maps_tiles_endpoint :: String.t()
|
2021-09-24 16:46:42 +02:00
|
|
|
def instance_maps_tiles_endpoint, do: maps_config()[:tiles][:endpoint]
|
2019-11-20 13:49:57 +01:00
|
|
|
|
|
|
|
@spec instance_maps_tiles_attribution :: String.t()
|
|
|
|
def instance_maps_tiles_attribution,
|
2021-09-24 16:46:42 +02:00
|
|
|
do: maps_config()[:tiles][:attribution]
|
2019-11-20 13:49:57 +01:00
|
|
|
|
2020-12-17 11:26:25 +01:00
|
|
|
@spec instance_maps_routing_type :: atom()
|
|
|
|
def instance_maps_routing_type,
|
2021-09-24 16:46:42 +02:00
|
|
|
do: maps_config()[:routing][:type]
|
|
|
|
|
|
|
|
@typep anonymous_config_type :: [
|
|
|
|
participation: [
|
|
|
|
allowed: boolean,
|
|
|
|
validation: [
|
|
|
|
email: [enabled: boolean(), confirmation_required: boolean()],
|
|
|
|
captcha: [enabled: boolean()]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
event_creation: [
|
|
|
|
allowed: boolean,
|
|
|
|
validation: [
|
|
|
|
email: [enabled: boolean(), confirmation_required: boolean()],
|
|
|
|
captcha: [enabled: boolean()]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
reports: [
|
|
|
|
allowed: boolean()
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
@spec anonymous_config :: anonymous_config_type
|
|
|
|
defp anonymous_config, do: Application.get_env(:mobilizon, :anonymous)
|
2020-12-17 11:26:25 +01:00
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
@spec anonymous_participation? :: boolean
|
|
|
|
def anonymous_participation?,
|
2021-09-24 16:46:42 +02:00
|
|
|
do: anonymous_config()[:participation][:allowed]
|
2019-12-20 13:04:34 +01:00
|
|
|
|
|
|
|
@spec anonymous_participation_email_required? :: boolean
|
|
|
|
def anonymous_participation_email_required?,
|
2021-09-24 16:46:42 +02:00
|
|
|
do: anonymous_config()[:participation][:validation][:email][:enabled]
|
2019-12-20 13:04:34 +01:00
|
|
|
|
|
|
|
@spec anonymous_participation_email_confirmation_required? :: boolean
|
|
|
|
def anonymous_participation_email_confirmation_required?,
|
|
|
|
do:
|
2021-09-24 16:46:42 +02:00
|
|
|
anonymous_config()[:participation][:validation][:email][
|
2019-12-20 13:04:34 +01:00
|
|
|
:confirmation_required
|
|
|
|
]
|
|
|
|
|
|
|
|
@spec anonymous_event_creation? :: boolean
|
|
|
|
def anonymous_event_creation?,
|
2021-09-24 16:46:42 +02:00
|
|
|
do: anonymous_config()[:event_creation][:allowed]
|
2019-12-20 13:04:34 +01:00
|
|
|
|
|
|
|
@spec anonymous_event_creation_email_required? :: boolean
|
|
|
|
def anonymous_event_creation_email_required?,
|
2021-09-24 16:46:42 +02:00
|
|
|
do: anonymous_config()[:event_creation][:validation][:email][:enabled]
|
2019-12-20 13:04:34 +01:00
|
|
|
|
|
|
|
@spec anonymous_event_creation_email_confirmation_required? :: boolean
|
|
|
|
def anonymous_event_creation_email_confirmation_required?,
|
|
|
|
do:
|
2021-09-24 16:46:42 +02:00
|
|
|
anonymous_config()[:event_creation][:validation][:email][
|
2019-12-20 13:04:34 +01:00
|
|
|
:confirmation_required
|
|
|
|
]
|
|
|
|
|
|
|
|
@spec anonymous_event_creation_email_captcha_required? :: boolean
|
|
|
|
def anonymous_event_creation_email_captcha_required?,
|
|
|
|
do:
|
2021-09-24 16:46:42 +02:00
|
|
|
anonymous_config()[:event_creation][:validation][:captcha][
|
2019-12-20 13:04:34 +01:00
|
|
|
:enabled
|
|
|
|
]
|
|
|
|
|
2020-06-09 14:07:49 +02:00
|
|
|
@spec anonymous_reporting? :: boolean
|
|
|
|
def anonymous_reporting?,
|
2021-09-24 16:46:42 +02:00
|
|
|
do: anonymous_config()[:reports][:allowed]
|
2020-06-09 14:07:49 +02:00
|
|
|
|
2020-06-27 19:12:45 +02:00
|
|
|
@spec oauth_consumer_strategies() :: list({atom(), String.t()})
|
|
|
|
def oauth_consumer_strategies do
|
|
|
|
[:auth, :oauth_consumer_strategies]
|
|
|
|
|> get([])
|
|
|
|
|> Enum.map(fn strategy ->
|
|
|
|
case strategy do
|
|
|
|
{id, label} when is_atom(id) -> %{id: id, label: label}
|
|
|
|
id when is_atom(id) -> %{id: id, label: nil}
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec ldap_enabled? :: boolean()
|
|
|
|
def ldap_enabled?, do: get([:ldap, :enabled], false)
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec instance_resource_providers :: list(%{type: atom, software: atom, endpoint: String.t()})
|
2020-02-18 08:57:00 +01:00
|
|
|
def instance_resource_providers do
|
|
|
|
types = get_in(Application.get_env(:mobilizon, Mobilizon.Service.ResourceProviders), [:types])
|
|
|
|
|
|
|
|
providers =
|
|
|
|
get_in(Application.get_env(:mobilizon, Mobilizon.Service.ResourceProviders), [:providers])
|
|
|
|
|
|
|
|
providers_map = :maps.filter(fn key, _value -> key in Keyword.values(types) end, providers)
|
|
|
|
|
|
|
|
case Enum.count(providers_map) do
|
|
|
|
0 ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Enum.map(providers_map, fn {key, value} ->
|
|
|
|
%{
|
|
|
|
type: key,
|
|
|
|
software: types |> Enum.find(fn {_key, val} -> val == key end) |> elem(0),
|
|
|
|
endpoint: value
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-06 18:00:50 +02:00
|
|
|
# config :mobilizon, :groups, enabled: true
|
|
|
|
# config :mobilizon, :events, creation: true
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec instance_group_feature_enabled? :: boolean
|
2020-09-02 15:08:47 +02:00
|
|
|
def instance_group_feature_enabled?,
|
|
|
|
do: :mobilizon |> Application.get_env(:groups) |> Keyword.get(:enabled)
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec instance_event_creation_enabled? :: boolean
|
2020-09-02 15:08:47 +02:00
|
|
|
def instance_event_creation_enabled?,
|
|
|
|
do: :mobilizon |> Application.get_env(:events) |> Keyword.get(:creation)
|
2020-06-08 16:47:57 +02:00
|
|
|
|
2021-10-04 18:59:41 +02:00
|
|
|
@spec instance_export_formats :: %{event_participants: list(String.t())}
|
|
|
|
def instance_export_formats do
|
|
|
|
%{
|
|
|
|
event_participants: enabled_formats()
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-10-06 18:00:50 +02:00
|
|
|
@spec only_admin_can_create_groups? :: boolean
|
|
|
|
def only_admin_can_create_groups?,
|
|
|
|
do:
|
|
|
|
:mobilizon
|
|
|
|
|> Application.get_env(:restrictions)
|
|
|
|
|> Keyword.get(:only_admin_can_create_groups)
|
|
|
|
|
|
|
|
@spec only_groups_can_create_events? :: boolean
|
|
|
|
def only_groups_can_create_events?,
|
|
|
|
do:
|
|
|
|
:mobilizon
|
|
|
|
|> Application.get_env(:restrictions)
|
|
|
|
|> Keyword.get(:only_groups_can_create_events)
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec anonymous_actor_id :: integer
|
2019-12-20 13:04:34 +01:00
|
|
|
def anonymous_actor_id, do: get_cached_value(:anonymous_actor_id)
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec get(keys :: module | atom | [module | atom]) :: any
|
2019-09-08 00:05:54 +02:00
|
|
|
def get(key), do: get(key, nil)
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec get(keys :: [module | atom], default :: any) :: any
|
2019-09-08 00:05:54 +02:00
|
|
|
def get([key], default), do: get(key, default)
|
|
|
|
|
|
|
|
def get([parent_key | keys], default) do
|
|
|
|
case get_in(Application.get_env(:mobilizon, parent_key), keys) do
|
|
|
|
nil -> default
|
|
|
|
any -> any
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec get(key :: module | atom, default :: any) :: any
|
2019-09-08 00:05:54 +02:00
|
|
|
def get(key, default), do: Application.get_env(:mobilizon, key, default)
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec get!(key :: module | atom) :: any | no_return
|
2019-09-08 00:05:54 +02:00
|
|
|
def get!(key) do
|
|
|
|
value = get(key, nil)
|
|
|
|
|
|
|
|
if value == nil do
|
|
|
|
raise("Missing configuration value: #{inspect(key)}")
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec put(keys :: [module | atom], value :: any) :: :ok
|
2019-09-08 00:05:54 +02:00
|
|
|
def put([key], value), do: put(key, value)
|
|
|
|
|
|
|
|
def put([parent_key | keys], value) do
|
2020-10-25 12:00:00 +01:00
|
|
|
parent =
|
|
|
|
Application.get_env(:mobilizon, parent_key, [])
|
|
|
|
|> put_in(keys, value)
|
2019-09-08 00:05:54 +02:00
|
|
|
|
|
|
|
Application.put_env(:mobilizon, parent_key, parent)
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec put(keys :: module | atom, value :: any) :: :ok
|
2020-10-25 12:00:00 +01:00
|
|
|
def put(key, value) do
|
|
|
|
Application.put_env(:mobilizon, key, value)
|
|
|
|
end
|
2019-09-08 00:05:54 +02:00
|
|
|
|
|
|
|
@spec to_boolean(boolean | String.t()) :: boolean
|
|
|
|
defp to_boolean(boolean), do: "true" == String.downcase("#{boolean}")
|
2019-12-20 13:04:34 +01:00
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec get_cached_value(atom) :: String.t() | integer | map
|
2019-12-20 13:04:34 +01:00
|
|
|
defp get_cached_value(key) do
|
|
|
|
case Cachex.fetch(:config, key, fn key ->
|
|
|
|
case create_cache(key) do
|
2021-09-10 11:36:05 +02:00
|
|
|
{:ok, value} when not is_nil(value) ->
|
|
|
|
{:commit, value}
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
Logger.debug("Failed to cache config value, returned: #{inspect(err)}")
|
|
|
|
{:ignore, err}
|
2019-12-20 13:04:34 +01:00
|
|
|
end
|
|
|
|
end) do
|
|
|
|
{status, value} when status in [:ok, :commit] -> value
|
|
|
|
_err -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec create_cache(atom()) :: {:ok, integer() | map()} | {:error, Ecto.Changeset.t()}
|
2019-12-20 13:04:34 +01:00
|
|
|
defp create_cache(:anonymous_actor_id) do
|
2021-09-10 11:36:05 +02:00
|
|
|
case Actors.get_or_create_internal_actor("anonymous") do
|
|
|
|
{:ok, %{id: actor_id}} ->
|
|
|
|
{:ok, actor_id}
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
2020-02-07 16:28:49 +01:00
|
|
|
end
|
|
|
|
end
|
2020-07-07 15:51:42 +02:00
|
|
|
|
2023-08-17 13:16:58 +02:00
|
|
|
defp create_cache(_), do: {:error, :cache_key_not_handled}
|
|
|
|
|
|
|
|
@spec admin_settings :: map()
|
|
|
|
def admin_settings do
|
|
|
|
%{
|
2020-07-07 15:51:42 +02:00
|
|
|
instance_description: instance_description(),
|
|
|
|
instance_long_description: instance_long_description(),
|
|
|
|
instance_name: instance_name(),
|
2020-10-21 17:49:18 +02:00
|
|
|
instance_slogan: instance_slogan(),
|
2020-07-07 15:51:42 +02:00
|
|
|
registrations_open: instance_registrations_open?(),
|
|
|
|
contact: contact(),
|
|
|
|
instance_terms: instance_terms(),
|
|
|
|
instance_terms_type: instance_terms_type(),
|
|
|
|
instance_terms_url: instance_terms_url(),
|
|
|
|
instance_privacy_policy: instance_privacy(),
|
|
|
|
instance_privacy_policy_type: instance_privacy_type(),
|
|
|
|
instance_privacy_policy_url: instance_privacy_url(),
|
2020-10-07 15:37:23 +02:00
|
|
|
instance_rules: instance_rules(),
|
|
|
|
instance_languages: instance_languages()
|
2020-07-07 15:51:42 +02:00
|
|
|
}
|
|
|
|
end
|
2020-02-07 16:28:49 +01:00
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec clear_config_cache :: {:ok | :error, integer}
|
2019-12-20 13:04:34 +01:00
|
|
|
def clear_config_cache do
|
|
|
|
Cachex.clear(:config)
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec generate_terms(String.t()) :: String.t()
|
2019-12-20 13:04:34 +01:00
|
|
|
def generate_terms(locale) do
|
2021-07-27 19:47:54 +02:00
|
|
|
Gettext.put_locale(locale)
|
2019-12-20 13:04:34 +01:00
|
|
|
|
|
|
|
Phoenix.View.render_to_string(
|
|
|
|
Mobilizon.Web.APIView,
|
|
|
|
"terms.html",
|
2020-06-19 19:27:10 +02:00
|
|
|
%{
|
|
|
|
instance_name: instance_name(),
|
|
|
|
instance_url: instance_hostname(),
|
|
|
|
instance_contact: instance_contact_html()
|
|
|
|
}
|
2019-12-20 13:04:34 +01:00
|
|
|
)
|
|
|
|
end
|
2020-06-19 19:27:10 +02:00
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec generate_privacy(String.t()) :: String.t()
|
2020-06-19 19:27:10 +02:00
|
|
|
def generate_privacy(locale) do
|
2021-07-27 19:47:54 +02:00
|
|
|
Gettext.put_locale(locale)
|
2020-06-19 19:27:10 +02:00
|
|
|
|
|
|
|
Phoenix.View.render_to_string(
|
|
|
|
Mobilizon.Web.APIView,
|
|
|
|
"privacy.html",
|
|
|
|
%{instance_name: instance_name()}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
@spec instance_contact_html :: String.t()
|
2020-06-19 19:27:10 +02:00
|
|
|
defp instance_contact_html do
|
|
|
|
contact = contact()
|
|
|
|
|
|
|
|
cond do
|
2020-06-23 10:58:48 +02:00
|
|
|
is_nil(contact) ->
|
2021-09-07 17:41:27 +02:00
|
|
|
"<b>Contact information not filled</b>"
|
2020-06-23 10:58:48 +02:00
|
|
|
|
2020-06-19 19:27:10 +02:00
|
|
|
String.contains?(contact, "@") ->
|
|
|
|
"<a href=\"mailto:#{contact}\">#{contact}</a>"
|
|
|
|
|
|
|
|
String.match?(contact, ~r/^https?:\/\//) ->
|
|
|
|
%URI{host: host} = URI.parse(contact)
|
|
|
|
"<a href=\"#{contact}\">#{host}</a>"
|
|
|
|
|
|
|
|
true ->
|
|
|
|
contact
|
|
|
|
end
|
|
|
|
end
|
2019-09-08 00:05:54 +02:00
|
|
|
end
|