aboutsummaryrefslogtreecommitdiff
path: root/lib/mix/tasks
diff options
context:
space:
mode:
authorMaksim Pechnikov <parallel588@gmail.com>2020-01-27 15:20:47 +0300
committerMaksim Pechnikov <parallel588@gmail.com>2020-01-27 15:20:47 +0300
commite442ea5722404ca551135b6f4767c016952bbda3 (patch)
tree91bb371d96fbc8b46d59c378f6dd690ba30f3da3 /lib/mix/tasks
parent6fbafb1cdcba3dc2a7e8b9718e295c9811a726d9 (diff)
parent0b582d415ba5215495047ddae1cb4e4fc1e016e6 (diff)
downloadpleroma-e442ea5722404ca551135b6f4767c016952bbda3.tar.gz
Merge branch 'develop' into issue/1276
Diffstat (limited to 'lib/mix/tasks')
-rw-r--r--lib/mix/tasks/pleroma/config.ex156
-rw-r--r--lib/mix/tasks/pleroma/docs.ex2
-rw-r--r--lib/mix/tasks/pleroma/emoji.ex2
-rw-r--r--lib/mix/tasks/pleroma/notification_settings.ex83
-rw-r--r--lib/mix/tasks/pleroma/robotstxt.ex1
-rw-r--r--lib/mix/tasks/pleroma/user.ex15
6 files changed, 209 insertions, 50 deletions
diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex
index 0e21408b2..3e76d2c97 100644
--- a/lib/mix/tasks/pleroma/config.ex
+++ b/lib/mix/tasks/pleroma/config.ex
@@ -4,69 +4,147 @@
defmodule Mix.Tasks.Pleroma.Config do
use Mix.Task
+
import Mix.Pleroma
+
+ alias Pleroma.ConfigDB
alias Pleroma.Repo
- alias Pleroma.Web.AdminAPI.Config
+
@shortdoc "Manages the location of the config"
@moduledoc File.read!("docs/administration/CLI_tasks/config.md")
+
def run(["migrate_to_db"]) do
start_pleroma()
+ migrate_to_db()
+ end
+
+ def run(["migrate_from_db" | options]) do
+ 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.", "")
+ {opts, _} =
+ OptionParser.parse!(options,
+ strict: [env: :string, delete: :boolean],
+ aliases: [d: :delete]
+ )
+
+ migrate_from_db(opts)
+ end
- key =
- if String.starts_with?(key, "Pleroma.") do
- key
+ @spec migrate_to_db(Path.t() | nil) :: any()
+ def migrate_to_db(file_path \\ nil) do
+ if Pleroma.Config.get([:configurable_from_database]) do
+ config_file =
+ if file_path do
+ file_path
+ else
+ if Pleroma.Config.get(:release) do
+ Pleroma.Config.get(:config_path)
else
- ":" <> key
+ "config/#{Pleroma.Config.get(:env)}.secret.exs"
end
+ end
- {:ok, _} = Config.update_or_create(%{group: "pleroma", key: key, value: v})
- Mix.shell().info("#{key} is migrated.")
- end)
-
- Mix.shell().info("Settings migrated.")
+ do_migrate_to_db(config_file)
else
- Mix.shell().info(
- "Migration is not allowed by config. You can change this behavior in instance settings."
- )
+ migration_error()
end
end
- def run(["migrate_from_db", env, delete?]) do
- start_pleroma()
+ defp do_migrate_to_db(config_file) do
+ if File.exists?(config_file) do
+ Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
+ Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
- delete? = if delete? == "true", do: true, else: false
+ custom_config =
+ config_file
+ |> read_file()
+ |> elem(0)
- if Pleroma.Config.get([:instance, :dynamic_configuration]) do
- config_path = "config/#{env}.exported_from_db.secret.exs"
+ custom_config
+ |> Keyword.keys()
+ |> Enum.each(&create(&1, custom_config))
+ else
+ shell_info("To migrate settings, you must define custom settings in #{config_file}.")
+ end
+ end
- {:ok, file} = File.open(config_path, [:write, :utf8])
- IO.write(file, "use Mix.Config\r\n")
+ defp create(group, settings) do
+ group
+ |> Pleroma.Config.Loader.filter_group(settings)
+ |> Enum.each(fn {key, value} ->
+ key = inspect(key)
+ {:ok, _} = ConfigDB.update_or_create(%{group: inspect(group), key: key, value: value})
- Repo.all(Config)
- |> Enum.each(fn config ->
- IO.write(
- file,
- "config :#{config.group}, #{config.key}, #{inspect(Config.from_binary(config.value))}\r\n\r\n"
- )
+ shell_info("Settings for key #{key} migrated.")
+ end)
+
+ shell_info("Settings for group :#{group} migrated.")
+ end
- if delete? do
- {:ok, _} = Repo.delete(config)
- Mix.shell().info("#{config.key} deleted from DB.")
+ defp migrate_from_db(opts) do
+ if Pleroma.Config.get([:configurable_from_database]) do
+ env = opts[:env] || "prod"
+
+ config_path =
+ if Pleroma.Config.get(:release) do
+ :config_path
+ |> Pleroma.Config.get()
+ |> Path.dirname()
+ else
+ "config"
end
- end)
+ |> Path.join("#{env}.exported_from_db.secret.exs")
+
+ file = File.open!(config_path, [:write, :utf8])
+
+ IO.write(file, config_header())
- File.close(file)
+ ConfigDB
+ |> Repo.all()
+ |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
+
+ :ok = 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."
- )
+ migration_error()
end
end
+
+ defp migration_error do
+ shell_error(
+ "Migration is not allowed in config. You can change this behavior by setting `configurable_from_database` to true."
+ )
+ end
+
+ if Code.ensure_loaded?(Config.Reader) do
+ defp config_header, do: "import Config\r\n\r\n"
+ defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
+ else
+ defp config_header, do: "use Mix.Config\r\n\r\n"
+ defp read_file(config_file), do: Mix.Config.eval!(config_file)
+ end
+
+ defp write_and_delete(config, file, delete?) do
+ config
+ |> write(file)
+ |> delete(delete?)
+ end
+
+ defp write(config, file) do
+ value =
+ config.value
+ |> ConfigDB.from_binary()
+ |> inspect(limit: :infinity)
+
+ IO.write(file, "config #{config.group}, #{config.key}, #{value}\r\n\r\n")
+
+ config
+ end
+
+ defp delete(config, true) do
+ {:ok, _} = Repo.delete(config)
+ shell_info("#{config.key} deleted from DB.")
+ end
+
+ defp delete(_config, _), do: :ok
end
diff --git a/lib/mix/tasks/pleroma/docs.ex b/lib/mix/tasks/pleroma/docs.ex
index 0d2663648..3c870f876 100644
--- a/lib/mix/tasks/pleroma/docs.ex
+++ b/lib/mix/tasks/pleroma/docs.ex
@@ -28,7 +28,7 @@ defmodule Mix.Tasks.Pleroma.Docs do
defp do_run(implementation) do
start_pleroma()
- with {descriptions, _paths} <- Mix.Config.eval!("config/description.exs"),
+ with descriptions <- Pleroma.Config.Loader.load("config/description.exs"),
{:ok, file_path} <-
Pleroma.Docs.Generator.process(
implementation,
diff --git a/lib/mix/tasks/pleroma/emoji.ex b/lib/mix/tasks/pleroma/emoji.ex
index 35669af27..24d999707 100644
--- a/lib/mix/tasks/pleroma/emoji.ex
+++ b/lib/mix/tasks/pleroma/emoji.ex
@@ -9,6 +9,7 @@ defmodule Mix.Tasks.Pleroma.Emoji do
@moduledoc File.read!("docs/administration/CLI_tasks/emoji.md")
def run(["ls-packs" | args]) do
+ Mix.Pleroma.start_pleroma()
Application.ensure_all_started(:hackney)
{options, [], []} = parse_global_opts(args)
@@ -35,6 +36,7 @@ defmodule Mix.Tasks.Pleroma.Emoji do
end
def run(["get-packs" | args]) do
+ Mix.Pleroma.start_pleroma()
Application.ensure_all_started(:hackney)
{options, pack_names, []} = parse_global_opts(args)
diff --git a/lib/mix/tasks/pleroma/notification_settings.ex b/lib/mix/tasks/pleroma/notification_settings.ex
new file mode 100644
index 000000000..7d65f0587
--- /dev/null
+++ b/lib/mix/tasks/pleroma/notification_settings.ex
@@ -0,0 +1,83 @@
+defmodule Mix.Tasks.Pleroma.NotificationSettings do
+ @shortdoc "Enable&Disable privacy option for push notifications"
+ @moduledoc """
+ Example:
+
+ > mix pleroma.notification_settings --privacy-option=false --nickname-users="parallel588" # set false only for parallel588 user
+ > mix pleroma.notification_settings --privacy-option=true # set true for all users
+
+ """
+
+ use Mix.Task
+ import Mix.Pleroma
+ import Ecto.Query
+
+ def run(args) do
+ start_pleroma()
+
+ {options, _, _} =
+ OptionParser.parse(
+ args,
+ strict: [
+ privacy_option: :boolean,
+ email_users: :string,
+ nickname_users: :string
+ ]
+ )
+
+ privacy_option = Keyword.get(options, :privacy_option)
+
+ if not is_nil(privacy_option) do
+ privacy_option
+ |> build_query(options)
+ |> Pleroma.Repo.update_all([])
+ end
+
+ shell_info("Done")
+ end
+
+ defp build_query(privacy_option, options) do
+ query =
+ from(u in Pleroma.User,
+ update: [
+ set: [
+ notification_settings:
+ fragment(
+ "jsonb_set(notification_settings, '{privacy_option}', ?)",
+ ^privacy_option
+ )
+ ]
+ ]
+ )
+
+ user_emails =
+ options
+ |> Keyword.get(:email_users, "")
+ |> String.split(",")
+ |> Enum.map(&String.trim(&1))
+ |> Enum.reject(&(&1 == ""))
+
+ query =
+ if length(user_emails) > 0 do
+ where(query, [u], u.email in ^user_emails)
+ else
+ query
+ end
+
+ user_nicknames =
+ options
+ |> Keyword.get(:nickname_users, "")
+ |> String.split(",")
+ |> Enum.map(&String.trim(&1))
+ |> Enum.reject(&(&1 == ""))
+
+ query =
+ if length(user_nicknames) > 0 do
+ where(query, [u], u.nickname in ^user_nicknames)
+ else
+ query
+ end
+
+ query
+ end
+end
diff --git a/lib/mix/tasks/pleroma/robotstxt.ex b/lib/mix/tasks/pleroma/robotstxt.ex
index 2128e1cd6..e99dd8502 100644
--- a/lib/mix/tasks/pleroma/robotstxt.ex
+++ b/lib/mix/tasks/pleroma/robotstxt.ex
@@ -18,6 +18,7 @@ defmodule Mix.Tasks.Pleroma.RobotsTxt do
"""
def run(["disallow_all"]) do
+ Mix.Pleroma.start_pleroma()
static_dir = Pleroma.Config.get([:instance, :static_dir], "instance/static/")
if !File.exists?(static_dir) do
diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex
index bc8eacda8..85c9e4954 100644
--- a/lib/mix/tasks/pleroma/user.ex
+++ b/lib/mix/tasks/pleroma/user.ex
@@ -8,7 +8,6 @@ defmodule Mix.Tasks.Pleroma.User do
alias Ecto.Changeset
alias Pleroma.User
alias Pleroma.UserInviteToken
- alias Pleroma.Web.OAuth
@shortdoc "Manages Pleroma users"
@moduledoc File.read!("docs/administration/CLI_tasks/user.md")
@@ -354,8 +353,7 @@ defmodule Mix.Tasks.Pleroma.User do
start_pleroma()
with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
- OAuth.Token.delete_user_tokens(user)
- OAuth.Authorization.delete_user_authorizations(user)
+ User.global_sign_out(user)
shell_info("#{nickname} signed out from all apps.")
else
@@ -373,9 +371,9 @@ defmodule Mix.Tasks.Pleroma.User do
users
|> Enum.each(fn user ->
shell_info(
- "#{user.nickname} moderator: #{user.info.is_moderator}, admin: #{user.info.is_admin}, locked: #{
- user.info.locked
- }, deactivated: #{user.info.deactivated}"
+ "#{user.nickname} moderator: #{user.is_moderator}, admin: #{user.is_admin}, locked: #{
+ user.locked
+ }, deactivated: #{user.deactivated}"
)
end)
end)
@@ -393,10 +391,7 @@ defmodule Mix.Tasks.Pleroma.User do
end
defp set_admin(user, value) do
- {:ok, user} =
- user
- |> Changeset.change(%{is_admin: value})
- |> User.update_and_set_cache()
+ {:ok, user} = User.admin_api_update(user, %{is_admin: value})
shell_info("Admin status of #{user.nickname}: #{user.is_admin}")
user