summaryrefslogtreecommitdiff
path: root/lib/mix
diff options
context:
space:
mode:
Diffstat (limited to 'lib/mix')
-rw-r--r--lib/mix/tasks/pleroma/common.ex2
-rw-r--r--lib/mix/tasks/pleroma/instance.ex1
-rw-r--r--lib/mix/tasks/pleroma/relay.ex4
-rw-r--r--lib/mix/tasks/pleroma/uploads.ex96
-rw-r--r--lib/mix/tasks/pleroma/user.ex44
5 files changed, 121 insertions, 26 deletions
diff --git a/lib/mix/tasks/pleroma/common.ex b/lib/mix/tasks/pleroma/common.ex
index 06893af05..36432c291 100644
--- a/lib/mix/tasks/pleroma/common.ex
+++ b/lib/mix/tasks/pleroma/common.ex
@@ -1,5 +1,5 @@
defmodule Mix.Tasks.Pleroma.Common do
- @shortdoc "Common functions to be reused in mix tasks"
+ @doc "Common functions to be reused in mix tasks"
def start_pleroma do
Mix.Task.run("app.start")
end
diff --git a/lib/mix/tasks/pleroma/instance.ex b/lib/mix/tasks/pleroma/instance.ex
index c66322707..3be856115 100644
--- a/lib/mix/tasks/pleroma/instance.ex
+++ b/lib/mix/tasks/pleroma/instance.ex
@@ -1,6 +1,5 @@
defmodule Mix.Tasks.Pleroma.Instance do
use Mix.Task
- alias Pleroma.{Repo, User}
alias Mix.Tasks.Pleroma.Common
@shortdoc "Manages Pleroma instance"
diff --git a/lib/mix/tasks/pleroma/relay.ex b/lib/mix/tasks/pleroma/relay.ex
index 2502923af..03586d6c3 100644
--- a/lib/mix/tasks/pleroma/relay.ex
+++ b/lib/mix/tasks/pleroma/relay.ex
@@ -22,7 +22,7 @@ defmodule Mix.Tasks.Pleroma.Relay do
def run(["follow", target]) do
Common.start_pleroma()
- with {:ok, activity} <- Relay.follow(target) do
+ with {:ok, _activity} <- Relay.follow(target) do
# put this task to sleep to allow the genserver to push out the messages
:timer.sleep(500)
else
@@ -33,7 +33,7 @@ defmodule Mix.Tasks.Pleroma.Relay do
def run(["unfollow", target]) do
Common.start_pleroma()
- with {:ok, activity} <- Relay.follow(target) do
+ with {:ok, _activity} <- Relay.unfollow(target) do
# put this task to sleep to allow the genserver to push out the messages
:timer.sleep(500)
else
diff --git a/lib/mix/tasks/pleroma/uploads.ex b/lib/mix/tasks/pleroma/uploads.ex
new file mode 100644
index 000000000..c6b840130
--- /dev/null
+++ b/lib/mix/tasks/pleroma/uploads.ex
@@ -0,0 +1,96 @@
+defmodule Mix.Tasks.Pleroma.Uploads do
+ use Mix.Task
+ alias Pleroma.{Upload, Uploaders.Local}
+ alias Mix.Tasks.Pleroma.Common
+ require Logger
+
+ @log_every 50
+ @shortdoc "Migrate uploads from local to remote storage"
+ @doc """
+ Manages uploads
+ ## Migrate uploads from local to remote storage
+
+ """
+ def run(["migrate_local", target_uploader | args]) do
+ delete? = Enum.member?(args, "--delete")
+ Common.start_pleroma()
+ local_path = Pleroma.Config.get!([Local, :uploads])
+ uploader = Module.concat(Pleroma.Uploaders, target_uploader)
+
+ unless Code.ensure_loaded?(uploader) do
+ raise("The uploader #{inspect(uploader)} is not an existing/loaded module.")
+ end
+
+ target_enabled? = Pleroma.Config.get([Upload, :uploader]) == uploader
+
+ unless target_enabled? do
+ Pleroma.Config.put([Upload, :uploader], uploader)
+ end
+
+ Mix.shell().info("Migrating files from local #{local_path} to #{to_string(uploader)}")
+
+ if delete? do
+ Mix.shell().info(
+ "Attention: uploaded files will be deleted, hope you have backups! (--delete ; cancel with ^C)"
+ )
+
+ :timer.sleep(:timer.seconds(5))
+ end
+
+ uploads =
+ File.ls!(local_path)
+ |> Enum.map(fn id ->
+ root_path = Path.join(local_path, id)
+
+ cond do
+ File.dir?(root_path) ->
+ files = for file <- File.ls!(root_path), do: {id, file, Path.join([root_path, file])}
+
+ case List.first(files) do
+ {id, file, path} ->
+ {%Pleroma.Upload{id: id, name: file, path: id <> "/" <> file, tempfile: path},
+ root_path}
+
+ _ ->
+ nil
+ end
+
+ File.exists?(root_path) ->
+ file = Path.basename(id)
+ hash = Path.rootname(id)
+ {%Pleroma.Upload{id: hash, name: file, path: file, tempfile: root_path}, root_path}
+
+ true ->
+ nil
+ end
+ end)
+ |> Enum.filter(& &1)
+
+ total_count = length(uploads)
+ Mix.shell().info("Found #{total_count} uploads")
+
+ uploads
+ |> Task.async_stream(
+ fn {upload, root_path} ->
+ case Upload.store(upload, uploader: uploader, filters: [], size_limit: nil) do
+ {:ok, _} ->
+ if delete?, do: File.rm_rf!(root_path)
+ Logger.debug("uploaded: #{inspect(upload.path)} #{inspect(upload)}")
+ :ok
+
+ error ->
+ Mix.shell().error("failed to upload #{inspect(upload.path)}: #{inspect(error)}")
+ end
+ end,
+ timeout: 150_000
+ )
+ |> Stream.chunk_every(@log_every)
+ |> Enum.reduce(0, fn done, count ->
+ count = count + length(done)
+ Mix.shell().info("Uploaded #{count}/#{total_count} files")
+ count
+ end)
+
+ Mix.shell().info("Done!")
+ end
+end
diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex
index 590553443..2675b021d 100644
--- a/lib/mix/tasks/pleroma/user.ex
+++ b/lib/mix/tasks/pleroma/user.ex
@@ -20,7 +20,7 @@ defmodule Mix.Tasks.Pleroma.User do
- `--admin`/`--no-admin` - whether the user is an admin
## Generate an invite link.
-
+
mix pleroma.user invite
## Delete the user's account.
@@ -32,7 +32,7 @@ defmodule Mix.Tasks.Pleroma.User do
mix pleroma.user toggle_activated NICKNAME
## Unsubscribe local users from user's account and deactivate it
-
+
mix pleroma.user unsubscribe NICKNAME
## Create a password reset link.
@@ -235,6 +235,26 @@ defmodule Mix.Tasks.Pleroma.User do
end
end
+ def run(["invite"]) do
+ Common.start_pleroma()
+
+ with {:ok, token} <- Pleroma.UserInviteToken.create_token() do
+ Mix.shell().info("Generated user invite token")
+
+ url =
+ Pleroma.Web.Router.Helpers.redirect_url(
+ Pleroma.Web.Endpoint,
+ :registration_page,
+ token.token
+ )
+
+ IO.puts(url)
+ else
+ _ ->
+ Mix.shell().error("Could not create invite token.")
+ end
+ end
+
defp set_moderator(user, value) do
info_cng = User.Info.admin_api_update(user.info, %{is_moderator: value})
@@ -270,24 +290,4 @@ defmodule Mix.Tasks.Pleroma.User do
Mix.shell().info("Locked status of #{user.nickname}: #{user.info.locked}")
end
-
- def run(["invite"]) do
- Common.start_pleroma()
-
- with {:ok, token} <- Pleroma.UserInviteToken.create_token() do
- Mix.shell().info("Generated user invite token")
-
- url =
- Pleroma.Web.Router.Helpers.redirect_url(
- Pleroma.Web.Endpoint,
- :registration_page,
- token.token
- )
-
- IO.puts(url)
- else
- _ ->
- Mix.shell().error("Could not create invite token.")
- end
- end
end