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
21 field(:unreachable_since, :naive_datetime_usec)
22 field(:favicon, :string)
23 field(:favicon_updated_at, :naive_datetime)
28 defdelegate host(url_or_host), to: Instances
30 def changeset(struct, params \\ %{}) do
32 |> cast(params, [:host, :unreachable_since, :favicon, :favicon_updated_at])
33 |> validate_required([:host])
34 |> unique_constraint(:host)
37 def filter_reachable([]), do: %{}
39 def filter_reachable(urls_or_hosts) when is_list(urls_or_hosts) do
42 |> Enum.map(&(&1 && host(&1)))
43 |> Enum.filter(&(to_string(&1) != ""))
45 unreachable_since_by_host =
48 where: i.host in ^hosts,
49 select: {i.host, i.unreachable_since}
54 reachability_datetime_threshold = Instances.reachability_datetime_threshold()
56 for entry <- Enum.filter(urls_or_hosts, &is_binary/1) do
58 unreachable_since = unreachable_since_by_host[host]
60 if !unreachable_since ||
61 NaiveDateTime.compare(unreachable_since, reachability_datetime_threshold) == :gt do
62 {entry, unreachable_since}
69 def reachable?(url_or_host) when is_binary(url_or_host) do
73 i.host == ^host(url_or_host) and
74 i.unreachable_since <= ^Instances.reachability_datetime_threshold(),
80 def reachable?(url_or_host) when is_binary(url_or_host), do: true
82 def set_reachable(url_or_host) when is_binary(url_or_host) do
83 with host <- host(url_or_host),
84 %Instance{} = existing_record <- Repo.get_by(Instance, %{host: host}) do
87 |> changeset(%{unreachable_since: nil})
92 def set_reachable(_), do: {:error, nil}
94 def set_unreachable(url_or_host, unreachable_since \\ nil)
96 def set_unreachable(url_or_host, unreachable_since) when is_binary(url_or_host) do
97 unreachable_since = parse_datetime(unreachable_since) || NaiveDateTime.utc_now()
98 host = host(url_or_host)
99 existing_record = Repo.get_by(Instance, %{host: host})
101 changes = %{unreachable_since: unreachable_since}
104 is_nil(existing_record) ->
106 |> changeset(Map.put(changes, :host, host))
109 existing_record.unreachable_since &&
110 NaiveDateTime.compare(existing_record.unreachable_since, unreachable_since) != :gt ->
111 {:ok, existing_record}
115 |> changeset(changes)
120 def set_unreachable(_, _), do: {:error, nil}
122 def get_consistently_unreachable do
123 reachability_datetime_threshold = Instances.reachability_datetime_threshold()
126 where: ^reachability_datetime_threshold > i.unreachable_since,
127 order_by: i.unreachable_since,
128 select: {i.host, i.unreachable_since}
133 defp parse_datetime(datetime) when is_binary(datetime) do
134 NaiveDateTime.from_iso8601(datetime)
137 defp parse_datetime(datetime), do: datetime
139 def get_or_update_favicon(%URI{host: host} = instance_uri) do
140 existing_record = Repo.get_by(Instance, %{host: host})
141 now = NaiveDateTime.utc_now()
143 if existing_record && existing_record.favicon_updated_at &&
144 NaiveDateTime.diff(now, existing_record.favicon_updated_at) < 86_400 do
145 existing_record.favicon
147 favicon = scrape_favicon(instance_uri)
149 if existing_record do
151 |> changeset(%{favicon: favicon, favicon_updated_at: now})
155 |> changeset(%{host: host, favicon: favicon, favicon_updated_at: now})
163 Logger.warn("Instance.get_or_update_favicon(\"#{host}\") error: #{inspect(e)}")
167 defp scrape_favicon(%URI{} = instance_uri) do
169 with {_, true} <- {:reachable, reachable?(instance_uri.host)},
170 {:ok, %Tesla.Env{body: html}} <-
171 Pleroma.HTTP.get(to_string(instance_uri), [{"accept", "text/html"}], pool: :media),
172 {_, [favicon_rel | _]} when is_binary(favicon_rel) <-
174 html |> Floki.parse_document!() |> Floki.attribute("link[rel=icon]", "href")},
175 {_, favicon} when is_binary(favicon) <-
176 {:merge, URI.merge(instance_uri, favicon_rel) |> to_string()} do
179 {:reachable, false} ->
181 "Instance.scrape_favicon(\"#{to_string(instance_uri)}\") ignored unreachable host"
192 "Instance.scrape_favicon(\"#{to_string(instance_uri)}\") error: #{inspect(e)}"