aboutsummaryrefslogtreecommitdiff
path: root/lib/mix
diff options
context:
space:
mode:
Diffstat (limited to 'lib/mix')
-rw-r--r--lib/mix/tasks/pleroma/config.ex11
-rw-r--r--lib/mix/tasks/pleroma/database.ex23
-rw-r--r--lib/mix/tasks/pleroma/emoji.ex44
-rw-r--r--lib/mix/tasks/pleroma/refresh_counter_cache.ex49
-rw-r--r--lib/mix/tasks/pleroma/user.ex12
5 files changed, 88 insertions, 51 deletions
diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex
index 5c9ef6904..d5129d410 100644
--- a/lib/mix/tasks/pleroma/config.ex
+++ b/lib/mix/tasks/pleroma/config.ex
@@ -52,6 +52,7 @@ defmodule Mix.Tasks.Pleroma.Config do
defp do_migrate_to_db(config_file) do
if File.exists?(config_file) do
+ shell_info("Migrating settings from file: #{Path.expand(config_file)}")
Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
@@ -72,8 +73,7 @@ defmodule Mix.Tasks.Pleroma.Config 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})
+ {:ok, _} = ConfigDB.update_or_create(%{group: group, key: key, value: value})
shell_info("Settings for key #{key} migrated.")
end)
@@ -131,12 +131,9 @@ defmodule Mix.Tasks.Pleroma.Config do
end
defp write(config, file) do
- value =
- config.value
- |> ConfigDB.from_binary()
- |> inspect(limit: :infinity)
+ value = inspect(config.value, limit: :infinity)
- IO.write(file, "config #{config.group}, #{config.key}, #{value}\r\n\r\n")
+ IO.write(file, "config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
config
end
diff --git a/lib/mix/tasks/pleroma/database.ex b/lib/mix/tasks/pleroma/database.ex
index 778de162f..82e2abdcb 100644
--- a/lib/mix/tasks/pleroma/database.ex
+++ b/lib/mix/tasks/pleroma/database.ex
@@ -4,6 +4,7 @@
defmodule Mix.Tasks.Pleroma.Database do
alias Pleroma.Conversation
+ alias Pleroma.Maintenance
alias Pleroma.Object
alias Pleroma.Repo
alias Pleroma.User
@@ -34,13 +35,7 @@ defmodule Mix.Tasks.Pleroma.Database do
)
if Keyword.get(options, :vacuum) do
- Logger.info("Runnning VACUUM FULL")
-
- Repo.query!(
- "vacuum full;",
- [],
- timeout: :infinity
- )
+ Maintenance.vacuum("full")
end
end
@@ -94,13 +89,7 @@ defmodule Mix.Tasks.Pleroma.Database do
|> Repo.delete_all(timeout: :infinity)
if Keyword.get(options, :vacuum) do
- Logger.info("Runnning VACUUM FULL")
-
- Repo.query!(
- "vacuum full;",
- [],
- timeout: :infinity
- )
+ Maintenance.vacuum("full")
end
end
@@ -135,4 +124,10 @@ defmodule Mix.Tasks.Pleroma.Database do
end)
|> Stream.run()
end
+
+ def run(["vacuum", args]) do
+ start_pleroma()
+
+ Maintenance.vacuum(args)
+ end
end
diff --git a/lib/mix/tasks/pleroma/emoji.ex b/lib/mix/tasks/pleroma/emoji.ex
index cdffa88b2..f4eaeac98 100644
--- a/lib/mix/tasks/pleroma/emoji.ex
+++ b/lib/mix/tasks/pleroma/emoji.ex
@@ -15,7 +15,7 @@ defmodule Mix.Tasks.Pleroma.Emoji do
{options, [], []} = parse_global_opts(args)
url_or_path = options[:manifest] || default_manifest()
- manifest = fetch_manifest(url_or_path)
+ manifest = fetch_and_decode(url_or_path)
Enum.each(manifest, fn {name, info} ->
to_print = [
@@ -42,12 +42,12 @@ defmodule Mix.Tasks.Pleroma.Emoji do
url_or_path = options[:manifest] || default_manifest()
- manifest = fetch_manifest(url_or_path)
+ manifest = fetch_and_decode(url_or_path)
for pack_name <- pack_names do
if Map.has_key?(manifest, pack_name) do
pack = manifest[pack_name]
- src_url = pack["src"]
+ src = pack["src"]
IO.puts(
IO.ANSI.format([
@@ -57,11 +57,11 @@ defmodule Mix.Tasks.Pleroma.Emoji do
:normal,
" from ",
:underline,
- src_url
+ src
])
)
- binary_archive = Tesla.get!(client(), src_url).body
+ {:ok, binary_archive} = fetch(src)
archive_sha = :crypto.hash(:sha256, binary_archive) |> Base.encode16()
sha_status_text = ["SHA256 of ", :bright, pack_name, :normal, " source file is ", :bright]
@@ -74,8 +74,8 @@ defmodule Mix.Tasks.Pleroma.Emoji do
raise "Bad SHA256 for #{pack_name}"
end
- # The url specified in files should be in the same directory
- files_url =
+ # The location specified in files should be in the same directory
+ files_loc =
url_or_path
|> Path.dirname()
|> Path.join(pack["files"])
@@ -88,11 +88,11 @@ defmodule Mix.Tasks.Pleroma.Emoji do
:normal,
" from ",
:underline,
- files_url
+ files_loc
])
)
- files = Tesla.get!(client(), files_url).body |> Jason.decode!()
+ files = fetch_and_decode(files_loc)
IO.puts(IO.ANSI.format(["Unpacking ", :bright, pack_name]))
@@ -237,16 +237,26 @@ defmodule Mix.Tasks.Pleroma.Emoji do
end
end
- defp fetch_manifest(from) do
- Jason.decode!(
- if String.starts_with?(from, "http") do
- Tesla.get!(client(), from).body
- else
- File.read!(from)
- end
- )
+ def run(["reload"]) do
+ start_pleroma()
+ Pleroma.Emoji.reload()
+ IO.puts("Emoji packs have been reloaded.")
end
+ defp fetch_and_decode(from) do
+ with {:ok, json} <- fetch(from) do
+ Jason.decode!(json)
+ end
+ end
+
+ defp fetch("http" <> _ = from) do
+ with {:ok, %{body: body}} <- Tesla.get(client(), from) do
+ {:ok, body}
+ end
+ end
+
+ defp fetch(path), do: File.read(path)
+
defp parse_global_opts(args) do
OptionParser.parse(
args,
diff --git a/lib/mix/tasks/pleroma/refresh_counter_cache.ex b/lib/mix/tasks/pleroma/refresh_counter_cache.ex
index 15b4dbfa6..efcbaa3b1 100644
--- a/lib/mix/tasks/pleroma/refresh_counter_cache.ex
+++ b/lib/mix/tasks/pleroma/refresh_counter_cache.ex
@@ -17,30 +17,53 @@ defmodule Mix.Tasks.Pleroma.RefreshCounterCache do
def run([]) do
Mix.Pleroma.start_pleroma()
- ["public", "unlisted", "private", "direct"]
- |> Enum.each(fn visibility ->
- count = status_visibility_count_query(visibility)
- name = "status_visibility_#{visibility}"
- CounterCache.set(name, count)
- Mix.Pleroma.shell_info("Set #{name} to #{count}")
+ instances =
+ Activity
+ |> distinct([a], true)
+ |> select([a], fragment("split_part(?, '/', 3)", a.actor))
+ |> Repo.all()
+
+ instances
+ |> Enum.with_index(1)
+ |> Enum.each(fn {instance, i} ->
+ counters = instance_counters(instance)
+ CounterCache.set(instance, counters)
+
+ Mix.Pleroma.shell_info(
+ "[#{i}/#{length(instances)}] Setting #{instance} counters: #{inspect(counters)}"
+ )
end)
Mix.Pleroma.shell_info("Done")
end
- defp status_visibility_count_query(visibility) do
+ defp instance_counters(instance) do
+ counters = %{"public" => 0, "unlisted" => 0, "private" => 0, "direct" => 0}
+
Activity
- |> where(
+ |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
+ |> where([a], fragment("split_part(?, '/', 3) = ?", a.actor, ^instance))
+ |> select(
+ [a],
+ {fragment(
+ "activity_visibility(?, ?, ?)",
+ a.actor,
+ a.recipients,
+ a.data
+ ), count(a.id)}
+ )
+ |> group_by(
[a],
fragment(
- "activity_visibility(?, ?, ?) = ?",
+ "activity_visibility(?, ?, ?)",
a.actor,
a.recipients,
- a.data,
- ^visibility
+ a.data
)
)
- |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
- |> Repo.aggregate(:count, :id, timeout: :timer.minutes(30))
+ |> Repo.all(timeout: :timer.minutes(30))
+ |> Enum.reduce(counters, fn {visibility, count}, acc ->
+ Map.put(acc, visibility, count)
+ end)
end
end
diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex
index 3635c02bc..bca7e87bf 100644
--- a/lib/mix/tasks/pleroma/user.ex
+++ b/lib/mix/tasks/pleroma/user.ex
@@ -144,6 +144,18 @@ defmodule Mix.Tasks.Pleroma.User do
end
end
+ def run(["reset_mfa", nickname]) do
+ start_pleroma()
+
+ with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
+ {:ok, _token} <- Pleroma.MFA.disable(user) do
+ shell_info("Multi-Factor Authentication disabled for #{user.nickname}")
+ else
+ _ ->
+ shell_error("No local user #{nickname}")
+ end
+ end
+
def run(["deactivate", nickname]) do
start_pleroma()