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 @cachex Pleroma.Config.get([:cachex, :provider], Cachex)
10 alias Pleroma.Instances
11 alias Pleroma.Instances.Instance
14 alias Pleroma.Workers.BackgroundWorker
25 field(:unreachable_since, :naive_datetime_usec)
26 field(:favicon, :string)
27 field(:metadata_updated_at, :naive_datetime)
28 field(:nodeinfo, :map, default: %{})
33 defdelegate host(url_or_host), to: Instances
35 def changeset(struct, params \\ %{}) do
37 |> cast(params, [:host, :unreachable_since, :favicon, :nodeinfo, :metadata_updated_at])
38 |> validate_required([:host])
39 |> unique_constraint(:host)
42 def filter_reachable([]), do: %{}
44 def filter_reachable(urls_or_hosts) when is_list(urls_or_hosts) do
47 |> Enum.map(&(&1 && host(&1)))
48 |> Enum.filter(&(to_string(&1) != ""))
50 unreachable_since_by_host =
53 where: i.host in ^hosts,
54 select: {i.host, i.unreachable_since}
59 reachability_datetime_threshold = Instances.reachability_datetime_threshold()
61 for entry <- Enum.filter(urls_or_hosts, &is_binary/1) do
63 unreachable_since = unreachable_since_by_host[host]
65 if !unreachable_since ||
66 NaiveDateTime.compare(unreachable_since, reachability_datetime_threshold) == :gt do
67 {entry, unreachable_since}
74 def reachable?(url_or_host) when is_binary(url_or_host) do
78 i.host == ^host(url_or_host) and
79 i.unreachable_since <= ^Instances.reachability_datetime_threshold(),
85 def reachable?(url_or_host) when is_binary(url_or_host), do: true
87 def set_reachable(url_or_host) when is_binary(url_or_host) do
88 with host <- host(url_or_host),
89 %Instance{} = existing_record <- Repo.get_by(Instance, %{host: host}) do
92 |> changeset(%{unreachable_since: nil})
97 def set_reachable(_), do: {:error, nil}
99 def set_unreachable(url_or_host, unreachable_since \\ nil)
101 def set_unreachable(url_or_host, unreachable_since) when is_binary(url_or_host) do
102 unreachable_since = parse_datetime(unreachable_since) || NaiveDateTime.utc_now()
103 host = host(url_or_host)
104 existing_record = Repo.get_by(Instance, %{host: host})
106 changes = %{unreachable_since: unreachable_since}
109 is_nil(existing_record) ->
111 |> changeset(Map.put(changes, :host, host))
114 existing_record.unreachable_since &&
115 NaiveDateTime.compare(existing_record.unreachable_since, unreachable_since) != :gt ->
116 {:ok, existing_record}
120 |> changeset(changes)
125 def set_unreachable(_, _), do: {:error, nil}
127 def get_consistently_unreachable do
128 reachability_datetime_threshold = Instances.reachability_datetime_threshold()
131 where: ^reachability_datetime_threshold > i.unreachable_since,
132 order_by: i.unreachable_since,
133 select: {i.host, i.unreachable_since}
138 defp parse_datetime(datetime) when is_binary(datetime) do
139 NaiveDateTime.from_iso8601(datetime)
142 defp parse_datetime(datetime), do: datetime
144 def needs_update(nil), do: true
146 def needs_update(%Instance{metadata_updated_at: nil}), do: true
148 def needs_update(%Instance{metadata_updated_at: metadata_updated_at}) do
149 now = NaiveDateTime.utc_now()
150 NaiveDateTime.diff(now, metadata_updated_at) > 86_400
155 host: Pleroma.Web.Endpoint.host(),
156 favicon: Pleroma.Web.Endpoint.url() <> "/favicon.png",
157 nodeinfo: Pleroma.Web.Nodeinfo.NodeinfoController.raw_nodeinfo()
161 def update_metadata(%URI{host: host} = uri) do
162 Logger.info("Checking metadata for #{host}")
163 existing_record = Repo.get_by(Instance, %{host: host})
165 if reachable?(host) do
166 do_update_metadata(uri, existing_record)
168 {:discard, :unreachable}
172 defp do_update_metadata(%URI{host: host} = uri, existing_record) do
173 if existing_record do
174 if needs_update(existing_record) do
175 Logger.info("Updating metadata for #{host}")
176 favicon = scrape_favicon(uri)
177 nodeinfo = scrape_nodeinfo(uri)
184 metadata_updated_at: NaiveDateTime.utc_now()
188 {:discard, "Does not require update"}
191 favicon = scrape_favicon(uri)
192 nodeinfo = scrape_nodeinfo(uri)
194 Logger.info("Creating metadata for #{host}")
202 metadata_updated_at: NaiveDateTime.utc_now()
208 def get_favicon(%URI{host: host}) do
209 existing_record = Repo.get_by(Instance, %{host: host})
211 if existing_record do
212 existing_record.favicon
218 defp scrape_nodeinfo(%URI{} = instance_uri) do
219 with true <- Pleroma.Config.get([:instances_nodeinfo, :enabled]),
220 {_, true} <- {:reachable, reachable?(instance_uri.host)},
221 {:ok, %Tesla.Env{status: 200, body: body}} <-
223 "https://#{instance_uri.host}/.well-known/nodeinfo",
224 headers: [{"Accept", "application/json"}]
226 {:ok, json} <- Jason.decode(body),
227 {:ok, %{"links" => links}} <- {:ok, json},
228 {:ok, %{"href" => href}} <-
230 Enum.find(links, &(&1["rel"] == "http://nodeinfo.diaspora.software/ns/schema/2.0"))},
231 {:ok, %Tesla.Env{body: data}} <-
232 Pleroma.HTTP.get(href, [{"accept", "application/json"}], []),
233 {:length, true} <- {:length, String.length(data) < 50_000},
234 {:ok, nodeinfo} <- Jason.decode(data) do
237 {:reachable, false} ->
239 "Instance.scrape_nodeinfo(\"#{to_string(instance_uri)}\") ignored unreachable host"
246 "Instance.scrape_nodeinfo(\"#{to_string(instance_uri)}\") ignored too long body"
256 defp scrape_favicon(%URI{} = instance_uri) do
257 with true <- Pleroma.Config.get([:instances_favicons, :enabled]),
258 {_, true} <- {:reachable, reachable?(instance_uri.host)},
259 {:ok, %Tesla.Env{body: html}} <-
260 Pleroma.HTTP.get(to_string(instance_uri), [{"accept", "text/html"}], []),
261 {_, [favicon_rel | _]} when is_binary(favicon_rel) <-
262 {:parse, html |> Floki.parse_document!() |> Floki.attribute("link[rel=icon]", "href")},
263 {_, favicon} when is_binary(favicon) <-
264 {:merge, URI.merge(instance_uri, favicon_rel) |> to_string()},
265 {:length, true} <- {:length, String.length(favicon) < 255} do
268 {:reachable, false} ->
270 "Instance.scrape_favicon(\"#{to_string(instance_uri)}\") ignored unreachable host"
281 Deletes all users from an instance in a background task, thus also deleting
282 all of those users' activities and notifications.
284 def delete_users_and_activities(host) when is_binary(host) do
285 BackgroundWorker.enqueue("delete_instance", %{"host" => host})
288 def perform(:delete_instance, host) when is_binary(host) do
289 User.Query.build(%{nickname: "@#{host}"})
290 |> Repo.chunk_stream(100, :batches)
291 |> Stream.each(fn users ->
293 |> Enum.each(fn user ->
294 User.perform(:delete, user)
300 def get_by_url(url_or_host) do
301 url = host(url_or_host)
302 Repo.get_by(Instance, host: url)
305 def get_cached_by_url(url_or_host) do
306 url = host(url_or_host)
308 if url == Pleroma.Web.Endpoint.host() do
311 @cachex.fetch!(:instances_cache, "instances:#{url}", fn _ ->
312 with %Instance{} = instance <- get_by_url(url) do
313 {:commit, {:ok, instance}}