1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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?(_), 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 defp parse_datetime(datetime) when is_binary(datetime) do
123 NaiveDateTime.from_iso8601(datetime)
126 defp parse_datetime(datetime), do: datetime
128 def get_or_update_favicon(%URI{host: host} = instance_uri) do
129 existing_record = Repo.get_by(Instance, %{host: host})
130 now = NaiveDateTime.utc_now()
132 if existing_record && existing_record.favicon_updated_at &&
133 NaiveDateTime.diff(now, existing_record.favicon_updated_at) < 86_400 do
134 existing_record.favicon
136 favicon = scrape_favicon(instance_uri)
138 if existing_record do
140 |> changeset(%{favicon: favicon, favicon_updated_at: now})
144 |> changeset(%{host: host, favicon: favicon, favicon_updated_at: now})
152 Logger.warn("Instance.get_or_update_favicon(\"#{host}\") error: #{inspect(e)}")
156 defp scrape_favicon(%URI{} = instance_uri) do
158 with {:ok, %Tesla.Env{body: html}} <-
159 Pleroma.HTTP.get(to_string(instance_uri), [{"accept", "text/html"}],
160 adapter: [pool: :media]
162 {_, [favicon_rel | _]} when is_binary(favicon_rel) <-
164 html |> Floki.parse_document!() |> Floki.attribute("link[rel=icon]", "href")},
165 {_, favicon} when is_binary(favicon) <-
166 {:merge, URI.merge(instance_uri, favicon_rel) |> to_string()} do
174 "Instance.scrape_favicon(\"#{to_string(instance_uri)}\") error: #{inspect(e)}"