Merge branch 'feature/optimize_rich_media_parser' into 'develop'
[akkoma] / lib / pleroma / instances / instance.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
21 timestamps()
22 end
23
24 defdelegate host(url_or_host), to: Instances
25
26 def changeset(struct, params \\ %{}) do
27 struct
28 |> cast(params, [:host, :unreachable_since])
29 |> validate_required([:host])
30 |> unique_constraint(:host)
31 end
32
33 def filter_reachable([]), do: %{}
34
35 def filter_reachable(urls_or_hosts) when is_list(urls_or_hosts) do
36 hosts =
37 urls_or_hosts
38 |> Enum.map(&(&1 && host(&1)))
39 |> Enum.filter(&(to_string(&1) != ""))
40
41 unreachable_since_by_host =
42 Repo.all(
43 from(i in Instance,
44 where: i.host in ^hosts,
45 select: {i.host, i.unreachable_since}
46 )
47 )
48 |> Map.new(& &1)
49
50 reachability_datetime_threshold = Instances.reachability_datetime_threshold()
51
52 for entry <- Enum.filter(urls_or_hosts, &is_binary/1) do
53 host = host(entry)
54 unreachable_since = unreachable_since_by_host[host]
55
56 if !unreachable_since ||
57 NaiveDateTime.compare(unreachable_since, reachability_datetime_threshold) == :gt do
58 {entry, unreachable_since}
59 end
60 end
61 |> Enum.filter(& &1)
62 |> Map.new(& &1)
63 end
64
65 def reachable?(url_or_host) when is_binary(url_or_host) do
66 !Repo.one(
67 from(i in Instance,
68 where:
69 i.host == ^host(url_or_host) and
70 i.unreachable_since <= ^Instances.reachability_datetime_threshold(),
71 select: true
72 )
73 )
74 end
75
76 def reachable?(_), do: true
77
78 def set_reachable(url_or_host) when is_binary(url_or_host) do
79 with host <- host(url_or_host),
80 %Instance{} = existing_record <- Repo.get_by(Instance, %{host: host}) do
81 {:ok, _instance} =
82 existing_record
83 |> changeset(%{unreachable_since: nil})
84 |> Repo.update()
85 end
86 end
87
88 def set_reachable(_), do: {:error, nil}
89
90 def set_unreachable(url_or_host, unreachable_since \\ nil)
91
92 def set_unreachable(url_or_host, unreachable_since) when is_binary(url_or_host) do
93 unreachable_since = parse_datetime(unreachable_since) || NaiveDateTime.utc_now()
94 host = host(url_or_host)
95 existing_record = Repo.get_by(Instance, %{host: host})
96
97 changes = %{unreachable_since: unreachable_since}
98
99 cond do
100 is_nil(existing_record) ->
101 %Instance{}
102 |> changeset(Map.put(changes, :host, host))
103 |> Repo.insert()
104
105 existing_record.unreachable_since &&
106 NaiveDateTime.compare(existing_record.unreachable_since, unreachable_since) != :gt ->
107 {:ok, existing_record}
108
109 true ->
110 existing_record
111 |> changeset(changes)
112 |> Repo.update()
113 end
114 end
115
116 def set_unreachable(_, _), do: {:error, nil}
117
118 defp parse_datetime(datetime) when is_binary(datetime) do
119 NaiveDateTime.from_iso8601(datetime)
120 end
121
122 defp parse_datetime(datetime), do: datetime
123 end