Unilateral remove from followers (#232)
[akkoma] / lib / pleroma / instances / instance.ex
index 4d0e8034d61400f623169a0d47c3bc21f236e1a6..533dbbb825789ef72936b0131b821a54a4529a24 100644 (file)
@@ -8,6 +8,8 @@ defmodule Pleroma.Instances.Instance do
   alias Pleroma.Instances
   alias Pleroma.Instances.Instance
   alias Pleroma.Repo
+  alias Pleroma.User
+  alias Pleroma.Workers.BackgroundWorker
 
   use Ecto.Schema
 
@@ -168,7 +170,7 @@ defmodule Pleroma.Instances.Instance do
     try do
       with {_, true} <- {:reachable, reachable?(instance_uri.host)},
            {:ok, %Tesla.Env{body: html}} <-
-             Pleroma.HTTP.get(to_string(instance_uri), [{"accept", "text/html"}], pool: :media),
+             Pleroma.HTTP.get(to_string(instance_uri), [{"accept", "text/html"}], []),
            {_, [favicon_rel | _]} when is_binary(favicon_rel) <-
              {:parse,
               html |> Floki.parse_document!() |> Floki.attribute("link[rel=icon]", "href")},
@@ -195,4 +197,24 @@ defmodule Pleroma.Instances.Instance do
         nil
     end
   end
+
+  @doc """
+  Deletes all users from an instance in a background task, thus also deleting
+  all of those users' activities and notifications.
+  """
+  def delete_users_and_activities(host) when is_binary(host) do
+    BackgroundWorker.enqueue("delete_instance", %{"host" => host})
+  end
+
+  def perform(:delete_instance, host) when is_binary(host) do
+    User.Query.build(%{nickname: "@#{host}"})
+    |> Repo.chunk_stream(100, :batches)
+    |> Stream.each(fn users ->
+      users
+      |> Enum.each(fn user ->
+        User.perform(:delete, user)
+      end)
+    end)
+    |> Stream.run()
+  end
 end