diff options
| author | Egor Kislitsyn <egor@kislitsyn.com> | 2020-06-29 16:09:49 +0400 | 
|---|---|---|
| committer | Egor Kislitsyn <egor@kislitsyn.com> | 2020-06-29 16:09:49 +0400 | 
| commit | 4a306720e8be8633e192f7b2c98f129a315939b9 (patch) | |
| tree | ed5ec059c532777a28eade72f384c8a800ead496 /lib/mix/tasks | |
| parent | d15aa9d9503e59b3cd0731394855781f435ec63c (diff) | |
| parent | 651f269568cb7e54cd96d8a0d8ea3533f77e9a3a (diff) | |
| download | pleroma-4a306720e8be8633e192f7b2c98f129a315939b9.tar.gz pleroma-4a306720e8be8633e192f7b2c98f129a315939b9.zip | |
Merge branch 'develop' into remove-twitter-api
Diffstat (limited to 'lib/mix/tasks')
| -rw-r--r-- | lib/mix/tasks/pleroma/benchmark.ex | 3 | ||||
| -rw-r--r-- | lib/mix/tasks/pleroma/config.ex | 11 | ||||
| -rw-r--r-- | lib/mix/tasks/pleroma/database.ex | 23 | ||||
| -rw-r--r-- | lib/mix/tasks/pleroma/emoji.ex | 44 | ||||
| -rw-r--r-- | lib/mix/tasks/pleroma/instance.ex | 8 | ||||
| -rw-r--r-- | lib/mix/tasks/pleroma/refresh_counter_cache.ex | 49 | ||||
| -rw-r--r-- | lib/mix/tasks/pleroma/user.ex | 32 | 
7 files changed, 98 insertions, 72 deletions
| diff --git a/lib/mix/tasks/pleroma/benchmark.ex b/lib/mix/tasks/pleroma/benchmark.ex index 6ab7fe8ef..dd2b9c8f2 100644 --- a/lib/mix/tasks/pleroma/benchmark.ex +++ b/lib/mix/tasks/pleroma/benchmark.ex @@ -67,8 +67,7 @@ defmodule Mix.Tasks.Pleroma.Benchmark do            Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{              activities: activities,              for: user, -            as: :activity, -            skip_relationships: true +            as: :activity            })          end        }, 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/instance.ex b/lib/mix/tasks/pleroma/instance.ex index bc842a59f..86409738a 100644 --- a/lib/mix/tasks/pleroma/instance.ex +++ b/lib/mix/tasks/pleroma/instance.ex @@ -147,6 +147,7 @@ defmodule Mix.Tasks.Pleroma.Instance do            "What directory should media uploads go in (when using the local uploader)?",            Pleroma.Config.get([Pleroma.Uploaders.Local, :uploads])          ) +        |> Path.expand()        static_dir =          get_option( @@ -155,6 +156,7 @@ defmodule Mix.Tasks.Pleroma.Instance do            "What directory should custom public files be read from (custom emojis, frontend bundle overrides, robots.txt, etc.)?",            Pleroma.Config.get([:instance, :static_dir])          ) +        |> Path.expand()        Config.put([:instance, :static_dir], static_dir) @@ -204,7 +206,7 @@ defmodule Mix.Tasks.Pleroma.Instance do        shell_info("Writing the postgres script to #{psql_path}.")        File.write(psql_path, result_psql) -      write_robots_txt(indexable, template_dir) +      write_robots_txt(static_dir, indexable, template_dir)        shell_info(          "\n All files successfully written! Refer to the installation instructions for your platform for next steps." @@ -224,15 +226,13 @@ defmodule Mix.Tasks.Pleroma.Instance do      end    end -  defp write_robots_txt(indexable, template_dir) do +  defp write_robots_txt(static_dir, indexable, template_dir) do      robots_txt =        EEx.eval_file(          template_dir <> "/robots_txt.eex",          indexable: indexable        ) -    static_dir = Pleroma.Config.get([:instance, :static_dir], "instance/static/") -      unless File.exists?(static_dir) do        File.mkdir_p!(static_dir)      end 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 da140ac86..bca7e87bf 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -144,28 +144,30 @@ defmodule Mix.Tasks.Pleroma.User do      end    end -  def run(["unsubscribe", nickname]) do +  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()      with %User{} = user <- User.get_cached_by_nickname(nickname) do        shell_info("Deactivating #{user.nickname}")        User.deactivate(user) - -      user -      |> User.get_friends() -      |> Enum.each(fn friend -> -        user = User.get_cached_by_id(user.id) - -        shell_info("Unsubscribing #{friend.nickname} from #{user.nickname}") -        User.unfollow(user, friend) -      end) -        :timer.sleep(500)        user = User.get_cached_by_id(user.id) -      if Enum.empty?(User.get_friends(user)) do -        shell_info("Successfully unsubscribed all followers from #{user.nickname}") +      if Enum.empty?(Enum.filter(User.get_friends(user), & &1.local)) do +        shell_info("Successfully unsubscribed all local followers from #{user.nickname}")        end      else        _ -> @@ -173,7 +175,7 @@ defmodule Mix.Tasks.Pleroma.User do      end    end -  def run(["unsubscribe_all_from_instance", instance]) do +  def run(["deactivate_all_from_instance", instance]) do      start_pleroma()      Pleroma.User.Query.build(%{nickname: "@#{instance}"}) @@ -181,7 +183,7 @@ defmodule Mix.Tasks.Pleroma.User do      |> Stream.each(fn users ->        users        |> Enum.each(fn user -> -        run(["unsubscribe", user.nickname]) +        run(["deactivate", user.nickname])        end)      end)      |> Stream.run() | 
