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
19 field(:unreachable_since, :naive_datetime_usec)
20 field(:favicon, :string)
21 field(:favicon_updated_at, :naive_datetime)
26 defdelegate host(url_or_host), to: Instances
28 def changeset(struct, params \\ %{}) do
30 |> cast(params, [:host, :unreachable_since, :favicon, :favicon_updated_at])
31 |> validate_required([:host])
32 |> unique_constraint(:host)
35 def filter_reachable([]), do: %{}
37 def filter_reachable(urls_or_hosts) when is_list(urls_or_hosts) do
40 |> Enum.map(&(&1 && host(&1)))
41 |> Enum.filter(&(to_string(&1) != ""))
43 unreachable_since_by_host =
46 where: i.host in ^hosts,
47 select: {i.host, i.unreachable_since}
52 reachability_datetime_threshold = Instances.reachability_datetime_threshold()
54 for entry <- Enum.filter(urls_or_hosts, &is_binary/1) do
56 unreachable_since = unreachable_since_by_host[host]
58 if !unreachable_since ||
59 NaiveDateTime.compare(unreachable_since, reachability_datetime_threshold) == :gt do
60 {entry, unreachable_since}
67 def reachable?(url_or_host) when is_binary(url_or_host) do
71 i.host == ^host(url_or_host) and
72 i.unreachable_since <= ^Instances.reachability_datetime_threshold(),
78 def reachable?(_), do: true
80 def set_reachable(url_or_host) when is_binary(url_or_host) do
81 with host <- host(url_or_host),
82 %Instance{} = existing_record <- Repo.get_by(Instance, %{host: host}) do
85 |> changeset(%{unreachable_since: nil})
90 def set_reachable(_), do: {:error, nil}
92 def set_unreachable(url_or_host, unreachable_since \\ nil)
94 def set_unreachable(url_or_host, unreachable_since) when is_binary(url_or_host) do
95 unreachable_since = parse_datetime(unreachable_since) || NaiveDateTime.utc_now()
96 host = host(url_or_host)
97 existing_record = Repo.get_by(Instance, %{host: host})
99 changes = %{unreachable_since: unreachable_since}
102 is_nil(existing_record) ->
104 |> changeset(Map.put(changes, :host, host))
107 existing_record.unreachable_since &&
108 NaiveDateTime.compare(existing_record.unreachable_since, unreachable_since) != :gt ->
109 {:ok, existing_record}
113 |> changeset(changes)
118 def set_unreachable(_, _), do: {:error, nil}
120 defp parse_datetime(datetime) when is_binary(datetime) do
121 NaiveDateTime.from_iso8601(datetime)
124 defp parse_datetime(datetime), do: datetime
126 def get_or_update_favicon(%URI{host: host} = instance_uri) do
127 existing_record = Repo.get_by(Instance, %{host: host})
128 now = NaiveDateTime.utc_now()
130 if existing_record && existing_record.favicon_updated_at &&
131 NaiveDateTime.diff(now, existing_record.favicon_updated_at) < 86_400 do
132 existing_record.favicon
134 favicon = scrape_favicon(instance_uri)
136 if existing_record do
138 |> changeset(%{favicon: favicon, favicon_updated_at: now})
142 |> changeset(%{host: host, favicon: favicon, favicon_updated_at: now})
150 defp scrape_favicon(%URI{} = instance_uri) do
152 with {:ok, %Tesla.Env{body: html}} <-
153 Pleroma.HTTP.get(to_string(instance_uri), [{"accept", "text/html"}],
154 adapter: [pool: :media]
158 |> Floki.parse_document!()
159 |> Floki.attribute("link[rel=icon]", "href")
161 favicon <- URI.merge(instance_uri, favicon_rel) |> to_string(),
162 true <- is_binary(favicon) do