+
+ @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
+
+ def get_by_url(url_or_host) do
+ url = host(url_or_host)
+ Repo.get_by(Instance, host: url)
+ end
+
+ def get_cached_by_url(url_or_host) do
+ url = host(url_or_host)
+
+ if url == Pleroma.Web.Endpoint.host() do
+ {:ok, local()}
+ else
+ @cachex.fetch!(:instances_cache, "instances:#{url}", fn _ ->
+ with %Instance{} = instance <- get_by_url(url) do
+ {:commit, {:ok, instance}}
+ else
+ _ -> {:ignore, nil}
+ end
+ end)
+ end
+ end
+
+ def set_request_signatures(url_or_host) when is_binary(url_or_host) do
+ host = host(url_or_host)
+ existing_record = Repo.get_by(Instance, %{host: host})
+ changes = %{has_request_signatures: true}
+
+ cond do
+ is_nil(existing_record) ->
+ %Instance{}
+ |> changeset(Map.put(changes, :host, host))
+ |> Repo.insert()
+
+ true ->
+ existing_record
+ |> changeset(changes)
+ |> Repo.update()
+ end
+ end
+
+ def set_request_signatures(_), do: {:error, :invalid_input}