- try do
- with {:ok, %Tesla.Env{body: html}} <-
- Pleroma.HTTP.get(to_string(instance_uri), [{:Accept, "text/html"}]),
- favicon_rel <-
- html
- |> Floki.parse_document!()
- |> Floki.attribute("link[rel=icon]", "href")
- |> List.first(),
- favicon <- URI.merge(instance_uri, favicon_rel) |> to_string(),
- true <- is_binary(favicon) do
- favicon
- else
- _ -> nil
- end
- rescue
- _ -> nil
+ with true <- Pleroma.Config.get([:instances_favicons, :enabled]),
+ {_, true} <- {:reachable, reachable?(instance_uri.host)},
+ {:ok, %Tesla.Env{body: html}} <-
+ 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")},
+ {_, favicon} when is_binary(favicon) <-
+ {:merge, URI.merge(instance_uri, favicon_rel) |> to_string()},
+ {:length, true} <- {:length, String.length(favicon) < 255} do
+ favicon
+ else
+ {:reachable, false} ->
+ Logger.debug(
+ "Instance.scrape_favicon(\"#{to_string(instance_uri)}\") ignored unreachable host"
+ )
+
+ nil
+
+ _ ->
+ 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
+
+ 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)