Merge branch 'develop' into fix/csp-for-captcha
[akkoma] / lib / pleroma / instances / instance.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Instances.Instance do
6 @moduledoc "Instance."
7
8 alias Pleroma.Instances
9 alias Pleroma.Instances.Instance
10 alias Pleroma.Repo
11
12 use Ecto.Schema
13
14 import Ecto.Query
15 import Ecto.Changeset
16
17 schema "instances" do
18 field(:host, :string)
19 field(:unreachable_since, :naive_datetime_usec)
20 field(:favicon, :string)
21 field(:favicon_updated_at, :naive_datetime)
22
23 timestamps()
24 end
25
26 defdelegate host(url_or_host), to: Instances
27
28 def changeset(struct, params \\ %{}) do
29 struct
30 |> cast(params, [:host, :unreachable_since, :favicon, :favicon_updated_at])
31 |> validate_required([:host])
32 |> unique_constraint(:host)
33 end
34
35 def filter_reachable([]), do: %{}
36
37 def filter_reachable(urls_or_hosts) when is_list(urls_or_hosts) do
38 hosts =
39 urls_or_hosts
40 |> Enum.map(&(&1 && host(&1)))
41 |> Enum.filter(&(to_string(&1) != ""))
42
43 unreachable_since_by_host =
44 Repo.all(
45 from(i in Instance,
46 where: i.host in ^hosts,
47 select: {i.host, i.unreachable_since}
48 )
49 )
50 |> Map.new(& &1)
51
52 reachability_datetime_threshold = Instances.reachability_datetime_threshold()
53
54 for entry <- Enum.filter(urls_or_hosts, &is_binary/1) do
55 host = host(entry)
56 unreachable_since = unreachable_since_by_host[host]
57
58 if !unreachable_since ||
59 NaiveDateTime.compare(unreachable_since, reachability_datetime_threshold) == :gt do
60 {entry, unreachable_since}
61 end
62 end
63 |> Enum.filter(& &1)
64 |> Map.new(& &1)
65 end
66
67 def reachable?(url_or_host) when is_binary(url_or_host) do
68 !Repo.one(
69 from(i in Instance,
70 where:
71 i.host == ^host(url_or_host) and
72 i.unreachable_since <= ^Instances.reachability_datetime_threshold(),
73 select: true
74 )
75 )
76 end
77
78 def reachable?(_), do: true
79
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
83 {:ok, _instance} =
84 existing_record
85 |> changeset(%{unreachable_since: nil})
86 |> Repo.update()
87 end
88 end
89
90 def set_reachable(_), do: {:error, nil}
91
92 def set_unreachable(url_or_host, unreachable_since \\ nil)
93
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})
98
99 changes = %{unreachable_since: unreachable_since}
100
101 cond do
102 is_nil(existing_record) ->
103 %Instance{}
104 |> changeset(Map.put(changes, :host, host))
105 |> Repo.insert()
106
107 existing_record.unreachable_since &&
108 NaiveDateTime.compare(existing_record.unreachable_since, unreachable_since) != :gt ->
109 {:ok, existing_record}
110
111 true ->
112 existing_record
113 |> changeset(changes)
114 |> Repo.update()
115 end
116 end
117
118 def set_unreachable(_, _), do: {:error, nil}
119
120 defp parse_datetime(datetime) when is_binary(datetime) do
121 NaiveDateTime.from_iso8601(datetime)
122 end
123
124 defp parse_datetime(datetime), do: datetime
125
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()
129
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
133 else
134 favicon = scrape_favicon(instance_uri)
135
136 if existing_record do
137 existing_record
138 |> changeset(%{favicon: favicon, favicon_updated_at: now})
139 |> Repo.update()
140 else
141 %Instance{}
142 |> changeset(%{host: host, favicon: favicon, favicon_updated_at: now})
143 |> Repo.insert()
144 end
145
146 favicon
147 end
148 end
149
150 defp scrape_favicon(%URI{} = instance_uri) do
151 try do
152 with {:ok, %Tesla.Env{body: html}} <-
153 Pleroma.HTTP.get(to_string(instance_uri), [{:Accept, "text/html"}]),
154 favicon_rel <-
155 html
156 |> Floki.parse_document!()
157 |> Floki.attribute("link[rel=icon]", "href")
158 |> List.first(),
159 favicon <- URI.merge(instance_uri, favicon_rel) |> to_string(),
160 true <- is_binary(favicon) do
161 favicon
162 else
163 _ -> nil
164 end
165 rescue
166 _ -> nil
167 end
168 end
169 end