1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Instances.Instance do
8 alias Pleroma.Instances
9 alias Pleroma.Instances.Instance
12 alias Pleroma.Workers.BackgroundWorker
23 field(:unreachable_since, :naive_datetime_usec)
24 field(:favicon, :string)
25 field(:favicon_updated_at, :naive_datetime)
30 defdelegate host(url_or_host), to: Instances
32 def changeset(struct, params \\ %{}) do
34 |> cast(params, [:host, :unreachable_since, :favicon, :favicon_updated_at])
35 |> validate_required([:host])
36 |> unique_constraint(:host)
39 def filter_reachable([]), do: %{}
41 def filter_reachable(urls_or_hosts) when is_list(urls_or_hosts) do
44 |> Enum.map(&(&1 && host(&1)))
45 |> Enum.filter(&(to_string(&1) != ""))
47 unreachable_since_by_host =
50 where: i.host in ^hosts,
51 select: {i.host, i.unreachable_since}
56 reachability_datetime_threshold = Instances.reachability_datetime_threshold()
58 for entry <- Enum.filter(urls_or_hosts, &is_binary/1) do
60 unreachable_since = unreachable_since_by_host[host]
62 if !unreachable_since ||
63 NaiveDateTime.compare(unreachable_since, reachability_datetime_threshold) == :gt do
64 {entry, unreachable_since}
71 def reachable?(url_or_host) when is_binary(url_or_host) do
75 i.host == ^host(url_or_host) and
76 i.unreachable_since <= ^Instances.reachability_datetime_threshold(),
82 def reachable?(url_or_host) when is_binary(url_or_host), do: true
84 def set_reachable(url_or_host) when is_binary(url_or_host) do
85 with host <- host(url_or_host),
86 %Instance{} = existing_record <- Repo.get_by(Instance, %{host: host}) do
89 |> changeset(%{unreachable_since: nil})
94 def set_reachable(_), do: {:error, nil}
96 def set_unreachable(url_or_host, unreachable_since \\ nil)
98 def set_unreachable(url_or_host, unreachable_since) when is_binary(url_or_host) do
99 unreachable_since = parse_datetime(unreachable_since) || NaiveDateTime.utc_now()
100 host = host(url_or_host)
101 existing_record = Repo.get_by(Instance, %{host: host})
103 changes = %{unreachable_since: unreachable_since}
106 is_nil(existing_record) ->
108 |> changeset(Map.put(changes, :host, host))
111 existing_record.unreachable_since &&
112 NaiveDateTime.compare(existing_record.unreachable_since, unreachable_since) != :gt ->
113 {:ok, existing_record}
117 |> changeset(changes)
122 def set_unreachable(_, _), do: {:error, nil}
124 def get_consistently_unreachable do
125 reachability_datetime_threshold = Instances.reachability_datetime_threshold()
128 where: ^reachability_datetime_threshold > i.unreachable_since,
129 order_by: i.unreachable_since,
130 select: {i.host, i.unreachable_since}
135 defp parse_datetime(datetime) when is_binary(datetime) do
136 NaiveDateTime.from_iso8601(datetime)
139 defp parse_datetime(datetime), do: datetime
141 def get_or_update_favicon(%URI{host: host} = instance_uri) do
142 existing_record = Repo.get_by(Instance, %{host: host})
143 now = NaiveDateTime.utc_now()
145 if existing_record && existing_record.favicon_updated_at &&
146 NaiveDateTime.diff(now, existing_record.favicon_updated_at) < 86_400 do
147 existing_record.favicon
149 favicon = scrape_favicon(instance_uri)
151 if existing_record do
153 |> changeset(%{favicon: favicon, favicon_updated_at: now})
157 |> changeset(%{host: host, favicon: favicon, favicon_updated_at: now})
165 Logger.warn("Instance.get_or_update_favicon(\"#{host}\") error: #{inspect(e)}")
169 defp scrape_favicon(%URI{} = instance_uri) do
171 with {_, true} <- {:reachable, reachable?(instance_uri.host)},
172 {:ok, %Tesla.Env{body: html}} <-
173 Pleroma.HTTP.get(to_string(instance_uri), [{"accept", "text/html"}], pool: :media),
174 {_, [favicon_rel | _]} when is_binary(favicon_rel) <-
176 html |> Floki.parse_document!() |> Floki.attribute("link[rel=icon]", "href")},
177 {_, favicon} when is_binary(favicon) <-
178 {:merge, URI.merge(instance_uri, favicon_rel) |> to_string()} do
181 {:reachable, false} ->
183 "Instance.scrape_favicon(\"#{to_string(instance_uri)}\") ignored unreachable host"
194 "Instance.scrape_favicon(\"#{to_string(instance_uri)}\") error: #{inspect(e)}"
202 Deletes all users from an instance in a background task, thus also deleting
203 all of those users' activities and notifications.
205 def delete_users_and_activities(host) when is_binary(host) do
206 BackgroundWorker.enqueue("delete_instance", %{"host" => host})
209 def perform(:delete_instance, host) when is_binary(host) do
210 User.Query.build(%{nickname: "@#{host}"})
211 |> Repo.chunk_stream(100, :batches)
212 |> Stream.each(fn users ->
214 |> Enum.each(fn user ->
215 User.perform(:delete, user)