aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/mix/tasks/pleroma/config.ex68
-rw-r--r--lib/mix/tasks/pleroma/emoji.ex8
-rw-r--r--lib/mix/tasks/pleroma/instance.ex15
-rw-r--r--lib/mix/tasks/pleroma/sample_config.eex3
-rw-r--r--lib/pleroma/application.ex3
-rw-r--r--lib/pleroma/config/transfer_task.ex41
-rw-r--r--lib/pleroma/emoji.ex29
-rw-r--r--lib/pleroma/instances.ex2
-rw-r--r--lib/pleroma/plugs/uploaded_media.ex2
-rw-r--r--lib/pleroma/reverse_proxy.ex6
-rw-r--r--lib/pleroma/user.ex4
-rw-r--r--lib/pleroma/web/activity_pub/publisher.ex2
-rw-r--r--lib/pleroma/web/admin_api/admin_api_controller.ex37
-rw-r--r--lib/pleroma/web/admin_api/config.ex144
-rw-r--r--lib/pleroma/web/admin_api/views/config_view.ex16
-rw-r--r--lib/pleroma/web/endpoint.ex2
-rw-r--r--lib/pleroma/web/oauth/token.ex5
-rw-r--r--lib/pleroma/web/oauth/token/response.ex8
-rw-r--r--lib/pleroma/web/router.ex3
19 files changed, 361 insertions, 37 deletions
diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex
new file mode 100644
index 000000000..1fe03088d
--- /dev/null
+++ b/lib/mix/tasks/pleroma/config.ex
@@ -0,0 +1,68 @@
+defmodule Mix.Tasks.Pleroma.Config do
+ use Mix.Task
+ alias Mix.Tasks.Pleroma.Common
+ alias Pleroma.Repo
+ alias Pleroma.Web.AdminAPI.Config
+ @shortdoc "Manages the location of the config"
+ @moduledoc """
+ Manages the location of the config.
+
+ ## Transfers config from file to DB.
+
+ mix pleroma.config migrate_to_db
+
+ ## Transfers config from DB to file.
+
+ mix pleroma.config migrate_from_db ENV
+ """
+
+ def run(["migrate_to_db"]) do
+ Common.start_pleroma()
+
+ if Pleroma.Config.get([:instance, :dynamic_configuration]) do
+ Application.get_all_env(:pleroma)
+ |> Enum.reject(fn {k, _v} -> k in [Pleroma.Repo, :env] end)
+ |> Enum.each(fn {k, v} ->
+ key = to_string(k) |> String.replace("Elixir.", "")
+ {:ok, _} = Config.update_or_create(%{key: key, value: v})
+ Mix.shell().info("#{key} is migrated.")
+ end)
+
+ Mix.shell().info("Settings migrated.")
+ else
+ Mix.shell().info(
+ "Migration is not allowed by config. You can change this behavior in instance settings."
+ )
+ end
+ end
+
+ def run(["migrate_from_db", env]) do
+ Common.start_pleroma()
+
+ if Pleroma.Config.get([:instance, :dynamic_configuration]) do
+ config_path = "config/#{env}.migrated.secret.exs"
+
+ {:ok, file} = File.open(config_path, [:write])
+
+ Repo.all(Config)
+ |> Enum.each(fn config ->
+ mark = if String.starts_with?(config.key, "Pleroma."), do: ",", else: ":"
+
+ IO.write(
+ file,
+ "config :pleroma, #{config.key}#{mark} #{inspect(Config.from_binary(config.value))}\r\n"
+ )
+
+ {:ok, _} = Repo.delete(config)
+ Mix.shell().info("#{config.key} deleted from DB.")
+ end)
+
+ File.close(file)
+ System.cmd("mix", ["format", config_path])
+ else
+ Mix.shell().info(
+ "Migration is not allowed by config. You can change this behavior in instance settings."
+ )
+ end
+ end
+end
diff --git a/lib/mix/tasks/pleroma/emoji.ex b/lib/mix/tasks/pleroma/emoji.ex
index d2ddf450a..c2225af7d 100644
--- a/lib/mix/tasks/pleroma/emoji.ex
+++ b/lib/mix/tasks/pleroma/emoji.ex
@@ -55,15 +55,13 @@ defmodule Mix.Tasks.Pleroma.Emoji do
are extracted).
"""
- @default_manifest Pleroma.Config.get!([:emoji, :default_manifest])
-
def run(["ls-packs" | args]) do
Application.ensure_all_started(:hackney)
{options, [], []} = parse_global_opts(args)
manifest =
- fetch_manifest(if options[:manifest], do: options[:manifest], else: @default_manifest)
+ fetch_manifest(if options[:manifest], do: options[:manifest], else: default_manifest())
Enum.each(manifest, fn {name, info} ->
to_print = [
@@ -88,7 +86,7 @@ defmodule Mix.Tasks.Pleroma.Emoji do
{options, pack_names, []} = parse_global_opts(args)
- manifest_url = if options[:manifest], do: options[:manifest], else: @default_manifest
+ manifest_url = if options[:manifest], do: options[:manifest], else: default_manifest()
manifest = fetch_manifest(manifest_url)
@@ -298,4 +296,6 @@ defmodule Mix.Tasks.Pleroma.Emoji do
Tesla.client(middleware)
end
+
+ defp default_manifest, do: Pleroma.Config.get!([:emoji, :default_manifest])
end
diff --git a/lib/mix/tasks/pleroma/instance.ex b/lib/mix/tasks/pleroma/instance.ex
index 88925dbaf..44e49cb69 100644
--- a/lib/mix/tasks/pleroma/instance.ex
+++ b/lib/mix/tasks/pleroma/instance.ex
@@ -30,6 +30,7 @@ defmodule Mix.Tasks.Pleroma.Instance do
- `--dbuser DBUSER` - the user (aka role) to use for the database connection
- `--dbpass DBPASS` - the password to use for the database connection
- `--indexable Y/N` - Allow/disallow indexing site by search engines
+ - `--db-configurable Y/N` - Allow/disallow configuring instance from admin part
"""
def run(["gen" | rest]) do
@@ -48,7 +49,8 @@ defmodule Mix.Tasks.Pleroma.Instance do
dbname: :string,
dbuser: :string,
dbpass: :string,
- indexable: :string
+ indexable: :string,
+ db_configurable: :string
],
aliases: [
o: :output,
@@ -101,6 +103,14 @@ defmodule Mix.Tasks.Pleroma.Instance do
"y"
) === "y"
+ db_configurable? =
+ Common.get_option(
+ options,
+ :db_configurable,
+ "Do you want to be able to configure instance from admin part? (y/n)",
+ "y"
+ ) === "y"
+
dbhost =
Common.get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
@@ -144,7 +154,8 @@ defmodule Mix.Tasks.Pleroma.Instance do
secret: secret,
signing_salt: signing_salt,
web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
- web_push_private_key: Base.url_encode64(web_push_private_key, padding: false)
+ web_push_private_key: Base.url_encode64(web_push_private_key, padding: false),
+ db_configurable?: db_configurable?
)
result_psql =
diff --git a/lib/mix/tasks/pleroma/sample_config.eex b/lib/mix/tasks/pleroma/sample_config.eex
index 52bd57cb7..73d9217be 100644
--- a/lib/mix/tasks/pleroma/sample_config.eex
+++ b/lib/mix/tasks/pleroma/sample_config.eex
@@ -16,7 +16,8 @@ config :pleroma, :instance,
notify_email: "<%= notify_email %>",
limit: 5000,
registrations_open: true,
- dedupe_media: false
+ dedupe_media: false,
+ dynamic_configuration: <%= db_configurable? %>
config :pleroma, :media_proxy,
enabled: false,
diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex
index 9c93c7a35..ba4cf8486 100644
--- a/lib/pleroma/application.ex
+++ b/lib/pleroma/application.ex
@@ -31,6 +31,7 @@ defmodule Pleroma.Application do
[
# Start the Ecto repository
%{id: Pleroma.Repo, start: {Pleroma.Repo, :start_link, []}, type: :supervisor},
+ %{id: Pleroma.Config.TransferTask, start: {Pleroma.Config.TransferTask, :start_link, []}},
%{id: Pleroma.Emoji, start: {Pleroma.Emoji, :start_link, []}},
%{id: Pleroma.Captcha, start: {Pleroma.Captcha, :start_link, []}},
%{
@@ -186,7 +187,7 @@ defmodule Pleroma.Application do
else
[]
end ++
- if Pleroma.Config.get([Pleroma.Uploader, :proxy_remote]) do
+ if Pleroma.Config.get([Pleroma.Upload, :proxy_remote]) do
[:upload]
else
[]
diff --git a/lib/pleroma/config/transfer_task.ex b/lib/pleroma/config/transfer_task.ex
new file mode 100644
index 000000000..0d6ece807
--- /dev/null
+++ b/lib/pleroma/config/transfer_task.ex
@@ -0,0 +1,41 @@
+defmodule Pleroma.Config.TransferTask do
+ use Task
+ alias Pleroma.Web.AdminAPI.Config
+
+ def start_link do
+ load_and_update_env()
+ if Pleroma.Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Pleroma.Repo)
+ :ignore
+ end
+
+ def load_and_update_env do
+ if Pleroma.Config.get([:instance, :dynamic_configuration]) do
+ Pleroma.Repo.all(Config)
+ |> Enum.each(&update_env(&1))
+ end
+ end
+
+ defp update_env(setting) do
+ try do
+ key =
+ if String.starts_with?(setting.key, "Pleroma.") do
+ "Elixir." <> setting.key
+ else
+ setting.key
+ end
+
+ Application.put_env(
+ :pleroma,
+ String.to_existing_atom(key),
+ Config.from_binary(setting.value)
+ )
+ rescue
+ e ->
+ require Logger
+
+ Logger.warn(
+ "updating env causes error, key: #{inspect(setting.key)}, error: #{inspect(e)}"
+ )
+ end
+ end
+end
diff --git a/lib/pleroma/emoji.ex b/lib/pleroma/emoji.ex
index b77b26f7f..854d46b1a 100644
--- a/lib/pleroma/emoji.ex
+++ b/lib/pleroma/emoji.ex
@@ -22,7 +22,6 @@ defmodule Pleroma.Emoji do
@ets __MODULE__.Ets
@ets_options [:ordered_set, :protected, :named_table, {:read_concurrency, true}]
- @groups Pleroma.Config.get([:emoji, :groups])
@doc false
def start_link do
@@ -87,6 +86,8 @@ defmodule Pleroma.Emoji do
"emoji"
)
+ emoji_groups = Pleroma.Config.get([:emoji, :groups])
+
case File.ls(emoji_dir_path) do
{:error, :enoent} ->
# The custom emoji directory doesn't exist,
@@ -118,7 +119,7 @@ defmodule Pleroma.Emoji do
emojis =
Enum.flat_map(
packs,
- fn pack -> load_pack(Path.join(emoji_dir_path, pack)) end
+ fn pack -> load_pack(Path.join(emoji_dir_path, pack), emoji_groups) end
)
true = :ets.insert(@ets, emojis)
@@ -129,9 +130,9 @@ defmodule Pleroma.Emoji do
shortcode_globs = Pleroma.Config.get([:emoji, :shortcode_globs], [])
emojis =
- (load_from_file("config/emoji.txt") ++
- load_from_file("config/custom_emoji.txt") ++
- load_from_globs(shortcode_globs))
+ (load_from_file("config/emoji.txt", emoji_groups) ++
+ load_from_file("config/custom_emoji.txt", emoji_groups) ++
+ load_from_globs(shortcode_globs, emoji_groups))
|> Enum.reject(fn value -> value == nil end)
true = :ets.insert(@ets, emojis)
@@ -139,13 +140,13 @@ defmodule Pleroma.Emoji do
:ok
end
- defp load_pack(pack_dir) do
+ defp load_pack(pack_dir, emoji_groups) do
pack_name = Path.basename(pack_dir)
emoji_txt = Path.join(pack_dir, "emoji.txt")
if File.exists?(emoji_txt) do
- load_from_file(emoji_txt)
+ load_from_file(emoji_txt, emoji_groups)
else
Logger.info(
"No emoji.txt found for pack \"#{pack_name}\", assuming all .png files are emoji"
@@ -155,7 +156,7 @@ defmodule Pleroma.Emoji do
|> Enum.map(fn {shortcode, rel_file} ->
filename = Path.join("/emoji/#{pack_name}", rel_file)
- {shortcode, filename, [to_string(match_extra(@groups, filename))]}
+ {shortcode, filename, [to_string(match_extra(emoji_groups, filename))]}
end)
end
end
@@ -184,21 +185,21 @@ defmodule Pleroma.Emoji do
|> Enum.filter(fn f -> Path.extname(f) in exts end)
end
- defp load_from_file(file) do
+ defp load_from_file(file, emoji_groups) do
if File.exists?(file) do
- load_from_file_stream(File.stream!(file))
+ load_from_file_stream(File.stream!(file), emoji_groups)
else
[]
end
end
- defp load_from_file_stream(stream) do
+ defp load_from_file_stream(stream, emoji_groups) do
stream
|> Stream.map(&String.trim/1)
|> Stream.map(fn line ->
case String.split(line, ~r/,\s*/) do
[name, file] ->
- {name, file, [to_string(match_extra(@groups, file))]}
+ {name, file, [to_string(match_extra(emoji_groups, file))]}
[name, file | tags] ->
{name, file, tags}
@@ -210,7 +211,7 @@ defmodule Pleroma.Emoji do
|> Enum.to_list()
end
- defp load_from_globs(globs) do
+ defp load_from_globs(globs, emoji_groups) do
static_path = Path.join(:code.priv_dir(:pleroma), "static")
paths =
@@ -221,7 +222,7 @@ defmodule Pleroma.Emoji do
|> Enum.concat()
Enum.map(paths, fn path ->
- tag = match_extra(@groups, Path.join("/", Path.relative_to(path, static_path)))
+ tag = match_extra(emoji_groups, Path.join("/", Path.relative_to(path, static_path)))
shortcode = Path.basename(path, Path.extname(path))
external_path = Path.join("/", Path.relative_to(path, static_path))
{shortcode, external_path, [to_string(tag)]}
diff --git a/lib/pleroma/instances.ex b/lib/pleroma/instances.ex
index 5e107f4c9..fa5043bc5 100644
--- a/lib/pleroma/instances.ex
+++ b/lib/pleroma/instances.ex
@@ -13,7 +13,7 @@ defmodule Pleroma.Instances do
def reachability_datetime_threshold do
federation_reachability_timeout_days =
- Pleroma.Config.get(:instance)[:federation_reachability_timeout_days] || 0
+ Pleroma.Config.get([:instance, :federation_reachability_timeout_days], 0)
if federation_reachability_timeout_days > 0 do
NaiveDateTime.add(
diff --git a/lib/pleroma/plugs/uploaded_media.ex b/lib/pleroma/plugs/uploaded_media.ex
index fd77b8d8f..8d0fac7ee 100644
--- a/lib/pleroma/plugs/uploaded_media.ex
+++ b/lib/pleroma/plugs/uploaded_media.ex
@@ -36,7 +36,7 @@ defmodule Pleroma.Plugs.UploadedMedia do
conn
end
- config = Pleroma.Config.get([Pleroma.Upload])
+ config = Pleroma.Config.get(Pleroma.Upload)
with uploader <- Keyword.fetch!(config, :uploader),
proxy_remote = Keyword.get(config, :proxy_remote, false),
diff --git a/lib/pleroma/reverse_proxy.ex b/lib/pleroma/reverse_proxy.ex
index 285d57309..de0f6e1bc 100644
--- a/lib/pleroma/reverse_proxy.ex
+++ b/lib/pleroma/reverse_proxy.ex
@@ -146,7 +146,7 @@ defmodule Pleroma.ReverseProxy do
Logger.debug("#{__MODULE__} #{method} #{url} #{inspect(headers)}")
method = method |> String.downcase() |> String.to_existing_atom()
- case :hackney.request(method, url, headers, "", hackney_opts) do
+ case hackney().request(method, url, headers, "", hackney_opts) do
{:ok, code, headers, client} when code in @valid_resp_codes ->
{:ok, code, downcase_headers(headers), client}
@@ -196,7 +196,7 @@ defmodule Pleroma.ReverseProxy do
duration,
Keyword.get(opts, :max_read_duration, @max_read_duration)
),
- {:ok, data} <- :hackney.stream_body(client),
+ {:ok, data} <- hackney().stream_body(client),
{:ok, duration} <- increase_read_duration(duration),
sent_so_far = sent_so_far + byte_size(data),
:ok <- body_size_constraint(sent_so_far, Keyword.get(opts, :max_body_size)),
@@ -377,4 +377,6 @@ defmodule Pleroma.ReverseProxy do
defp increase_read_duration(_) do
{:ok, :no_duration_limit, :no_duration_limit}
end
+
+ defp hackney, do: Pleroma.Config.get(:hackney, :hackney)
end
diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex
index 9449a88d0..3a9ae8d73 100644
--- a/lib/pleroma/user.ex
+++ b/lib/pleroma/user.ex
@@ -1036,9 +1036,7 @@ defmodule Pleroma.User do
Pleroma.HTML.Scrubber.TwitterText
end
- @default_scrubbers Pleroma.Config.get([:markup, :scrub_policy])
-
- def html_filter_policy(_), do: @default_scrubbers
+ def html_filter_policy(_), do: Pleroma.Config.get([:markup, :scrub_policy])
def fetch_by_ap_id(ap_id) do
ap_try = ActivityPub.make_user_from_ap_id(ap_id)
diff --git a/lib/pleroma/web/activity_pub/publisher.ex b/lib/pleroma/web/activity_pub/publisher.ex
index 8f1399ce6..a05e03263 100644
--- a/lib/pleroma/web/activity_pub/publisher.ex
+++ b/lib/pleroma/web/activity_pub/publisher.ex
@@ -88,7 +88,7 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
true
else
inbox_info = URI.parse(inbox)
- !Enum.member?(Pleroma.Config.get([:instance, :quarantined_instances], []), inbox_info.host)
+ !Enum.member?(Config.get([:instance, :quarantined_instances], []), inbox_info.host)
end
end
diff --git a/lib/pleroma/web/admin_api/admin_api_controller.ex b/lib/pleroma/web/admin_api/admin_api_controller.ex
index de2a13c01..03dfdca82 100644
--- a/lib/pleroma/web/admin_api/admin_api_controller.ex
+++ b/lib/pleroma/web/admin_api/admin_api_controller.ex
@@ -10,6 +10,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Relay
alias Pleroma.Web.AdminAPI.AccountView
+ alias Pleroma.Web.AdminAPI.Config
+ alias Pleroma.Web.AdminAPI.ConfigView
alias Pleroma.Web.AdminAPI.ReportView
alias Pleroma.Web.AdminAPI.Search
alias Pleroma.Web.CommonAPI
@@ -362,6 +364,41 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
end
end
+ def config_show(conn, _params) do
+ configs = Pleroma.Repo.all(Config)
+
+ conn
+ |> put_view(ConfigView)
+ |> render("index.json", %{configs: configs})
+ end
+
+ def config_update(conn, %{"configs" => configs}) do
+ updated =
+ if Pleroma.Config.get([:instance, :dynamic_configuration]) do
+ updated =
+ Enum.map(configs, fn
+ %{"key" => key, "value" => value} ->
+ {:ok, config} = Config.update_or_create(%{key: key, value: value})
+ config
+
+ %{"key" => key, "delete" => "true"} ->
+ {:ok, _} = Config.delete(key)
+ nil
+ end)
+ |> Enum.reject(&is_nil(&1))
+
+ Pleroma.Config.TransferTask.load_and_update_env()
+ Mix.Tasks.Pleroma.Config.run(["migrate_from_db", Pleroma.Config.get(:env)])
+ updated
+ else
+ []
+ end
+
+ conn
+ |> put_view(ConfigView)
+ |> render("index.json", %{configs: updated})
+ end
+
def errors(conn, {:error, :not_found}) do
conn
|> put_status(404)
diff --git a/lib/pleroma/web/admin_api/config.ex b/lib/pleroma/web/admin_api/config.ex
new file mode 100644
index 000000000..b7072f050
--- /dev/null
+++ b/lib/pleroma/web/admin_api/config.ex
@@ -0,0 +1,144 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.AdminAPI.Config do
+ use Ecto.Schema
+ import Ecto.Changeset
+ alias __MODULE__
+ alias Pleroma.Repo
+
+ @type t :: %__MODULE__{}
+
+ schema "config" do
+ field(:key, :string)
+ field(:value, :binary)
+
+ timestamps()
+ end
+
+ @spec get_by_key(String.t()) :: Config.t() | nil
+ def get_by_key(key), do: Repo.get_by(Config, key: key)
+
+ @spec changeset(Config.t(), map()) :: Changeset.t()
+ def changeset(config, params \\ %{}) do
+ config
+ |> cast(params, [:key, :value])
+ |> validate_required([:key, :value])
+ |> unique_constraint(:key)
+ end
+
+ @spec create(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
+ def create(%{key: key, value: value}) do
+ %Config{}
+ |> changeset(%{key: key, value: transform(value)})
+ |> Repo.insert()
+ end
+
+ @spec update(Config.t(), map()) :: {:ok, Config} | {:error, Changeset.t()}
+ def update(%Config{} = config, %{value: value}) do
+ config
+ |> change(value: transform(value))
+ |> Repo.update()
+ end
+
+ @spec update_or_create(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
+ def update_or_create(%{key: key} = params) do
+ with %Config{} = config <- Config.get_by_key(key) do
+ Config.update(config, params)
+ else
+ nil -> Config.create(params)
+ end
+ end
+
+ @spec delete(String.t()) :: {:ok, Config.t()} | {:error, Changeset.t()}
+ def delete(key) do
+ with %Config{} = config <- Config.get_by_key(key) do
+ Repo.delete(config)
+ else
+ nil -> {:error, "Config with key #{key} not found"}
+ end
+ end
+
+ @spec from_binary(binary()) :: term()
+ def from_binary(value), do: :erlang.binary_to_term(value)
+
+ @spec from_binary_to_map(binary()) :: any()
+ def from_binary_to_map(binary) do
+ from_binary(binary)
+ |> do_convert()
+ end
+
+ defp do_convert([{k, v}] = value) when is_list(value) and length(value) == 1,
+ do: %{k => do_convert(v)}
+
+ defp do_convert(values) when is_list(values), do: for(val <- values, do: do_convert(val))
+
+ defp do_convert({k, v} = value) when is_tuple(value),
+ do: %{k => do_convert(v)}
+
+ defp do_convert(value) when is_binary(value) or is_atom(value) or is_map(value),
+ do: value
+
+ @spec transform(any()) :: binary()
+ def transform(entity) when is_map(entity) do
+ tuples =
+ for {k, v} <- entity,
+ into: [],
+ do: {if(is_atom(k), do: k, else: String.to_atom(k)), do_transform(v)}
+
+ Enum.reject(tuples, fn {_k, v} -> is_nil(v) end)
+ |> Enum.sort()
+ |> :erlang.term_to_binary()
+ end
+
+ def transform(entity) when is_list(entity) do
+ list = Enum.map(entity, &do_transform(&1))
+ :erlang.term_to_binary(list)
+ end
+
+ def transform(entity), do: :erlang.term_to_binary(entity)
+
+ defp do_transform(%Regex{} = value) when is_map(value), do: value
+
+ defp do_transform(value) when is_map(value) do
+ values =
+ for {key, val} <- value,
+ into: [],
+ do: {String.to_atom(key), do_transform(val)}
+
+ Enum.sort(values)
+ end
+
+ defp do_transform(value) when is_list(value) do
+ Enum.map(value, &do_transform(&1))
+ end
+
+ defp do_transform(entity) when is_list(entity) and length(entity) == 1, do: hd(entity)
+
+ defp do_transform(value) when is_binary(value) do
+ value = String.trim(value)
+
+ case String.length(value) do
+ 0 ->
+ nil
+
+ _ ->
+ cond do
+ String.starts_with?(value, "Pleroma") ->
+ String.to_existing_atom("Elixir." <> value)
+
+ String.starts_with?(value, ":") ->
+ String.replace(value, ":", "") |> String.to_existing_atom()
+
+ String.starts_with?(value, "i:") ->
+ String.replace(value, "i:", "") |> String.to_integer()
+
+ true ->
+ value
+ end
+ end
+ end
+
+ defp do_transform(value), do: value
+end
diff --git a/lib/pleroma/web/admin_api/views/config_view.ex b/lib/pleroma/web/admin_api/views/config_view.ex
new file mode 100644
index 000000000..c8560033e
--- /dev/null
+++ b/lib/pleroma/web/admin_api/views/config_view.ex
@@ -0,0 +1,16 @@
+defmodule Pleroma.Web.AdminAPI.ConfigView do
+ use Pleroma.Web, :view
+
+ def render("index.json", %{configs: configs}) do
+ %{
+ configs: render_many(configs, __MODULE__, "show.json", as: :config)
+ }
+ end
+
+ def render("show.json", %{config: config}) do
+ %{
+ key: config.key,
+ value: Pleroma.Web.AdminAPI.Config.from_binary_to_map(config.value)
+ }
+ end
+end
diff --git a/lib/pleroma/web/endpoint.ex b/lib/pleroma/web/endpoint.ex
index bd76e4295..ddaf88f1d 100644
--- a/lib/pleroma/web/endpoint.ex
+++ b/lib/pleroma/web/endpoint.ex
@@ -91,7 +91,7 @@ defmodule Pleroma.Web.Endpoint do
Plug.Session,
store: :cookie,
key: cookie_name,
- signing_salt: {Pleroma.Config, :get, [[__MODULE__, :signing_salt], "CqaoopA2"]},
+ signing_salt: Pleroma.Config.get([__MODULE__, :signing_salt], "CqaoopA2"),
http_only: true,
secure: secure_cookies,
extra: extra
diff --git a/lib/pleroma/web/oauth/token.ex b/lib/pleroma/web/oauth/token.ex
index f412f7eb2..90c304487 100644
--- a/lib/pleroma/web/oauth/token.ex
+++ b/lib/pleroma/web/oauth/token.ex
@@ -14,7 +14,6 @@ defmodule Pleroma.Web.OAuth.Token do
alias Pleroma.Web.OAuth.Token
alias Pleroma.Web.OAuth.Token.Query
- @expires_in Pleroma.Config.get([:oauth2, :token_expires_in], 600)
@type t :: %__MODULE__{}
schema "oauth_tokens" do
@@ -78,7 +77,7 @@ defmodule Pleroma.Web.OAuth.Token do
defp put_valid_until(changeset, attrs) do
expires_in =
- Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), @expires_in))
+ Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), expires_in()))
changeset
|> change(%{valid_until: expires_in})
@@ -123,4 +122,6 @@ defmodule Pleroma.Web.OAuth.Token do
end
def is_expired?(_), do: false
+
+ defp expires_in, do: Pleroma.Config.get([:oauth2, :token_expires_in], 600)
end
diff --git a/lib/pleroma/web/oauth/token/response.ex b/lib/pleroma/web/oauth/token/response.ex
index 64e78b183..2648571ad 100644
--- a/lib/pleroma/web/oauth/token/response.ex
+++ b/lib/pleroma/web/oauth/token/response.ex
@@ -4,15 +4,13 @@ defmodule Pleroma.Web.OAuth.Token.Response do
alias Pleroma.User
alias Pleroma.Web.OAuth.Token.Utils
- @expires_in Pleroma.Config.get([:oauth2, :token_expires_in], 600)
-
@doc false
def build(%User{} = user, token, opts \\ %{}) do
%{
token_type: "Bearer",
access_token: token.token,
refresh_token: token.refresh_token,
- expires_in: @expires_in,
+ expires_in: expires_in(),
scope: Enum.join(token.scopes, " "),
me: user.ap_id
}
@@ -25,8 +23,10 @@ defmodule Pleroma.Web.OAuth.Token.Response do
access_token: token.token,
refresh_token: token.refresh_token,
created_at: Utils.format_created_at(token),
- expires_in: @expires_in,
+ expires_in: expires_in(),
scope: Enum.join(token.scopes, " ")
}
end
+
+ defp expires_in, do: Pleroma.Config.get([:oauth2, :token_expires_in], 600)
end
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index 17733a77b..0e3f73226 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -202,6 +202,9 @@ defmodule Pleroma.Web.Router do
put("/statuses/:id", AdminAPIController, :status_update)
delete("/statuses/:id", AdminAPIController, :status_delete)
+
+ get("/config", AdminAPIController, :config_show)
+ post("/config", AdminAPIController, :config_update)
end
scope "/", Pleroma.Web.TwitterAPI do