2019-01-23 08:37:25 -07:00
|
|
|
defmodule Pleroma.Instances.Instance do
|
|
|
|
@moduledoc "Instance."
|
|
|
|
|
2019-02-09 08:16:26 -07:00
|
|
|
alias Pleroma.Instances
|
|
|
|
alias Pleroma.Repo
|
2019-01-23 08:37:25 -07:00
|
|
|
alias Pleroma.Instances.Instance
|
|
|
|
|
|
|
|
use Ecto.Schema
|
|
|
|
|
2019-02-09 08:20:18 -07:00
|
|
|
import Ecto.Query
|
|
|
|
import Ecto.Changeset
|
2019-01-23 08:37:25 -07:00
|
|
|
|
|
|
|
schema "instances" do
|
|
|
|
field(:host, :string)
|
|
|
|
field(:unreachable_since, :naive_datetime)
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2019-01-28 05:25:06 -07:00
|
|
|
defdelegate host(url_or_host), to: Instances
|
2019-01-24 09:15:23 -07:00
|
|
|
|
|
|
|
def changeset(struct, params \\ %{}) do
|
2019-01-23 08:37:25 -07:00
|
|
|
struct
|
2019-01-28 01:03:52 -07:00
|
|
|
|> cast(params, [:host, :unreachable_since])
|
2019-01-24 09:15:23 -07:00
|
|
|
|> validate_required([:host])
|
2019-01-23 08:37:25 -07:00
|
|
|
|> unique_constraint(:host)
|
|
|
|
end
|
|
|
|
|
2019-02-03 02:41:27 -07:00
|
|
|
def filter_reachable([]), do: %{}
|
2019-01-24 09:15:23 -07:00
|
|
|
|
2019-01-28 05:25:06 -07:00
|
|
|
def filter_reachable(urls_or_hosts) when is_list(urls_or_hosts) do
|
2019-01-24 09:15:23 -07:00
|
|
|
hosts =
|
2019-01-28 05:25:06 -07:00
|
|
|
urls_or_hosts
|
2019-01-24 09:15:23 -07:00
|
|
|
|> Enum.map(&(&1 && host(&1)))
|
|
|
|
|> Enum.filter(&(to_string(&1) != ""))
|
|
|
|
|
2019-02-03 02:41:27 -07:00
|
|
|
unreachable_since_by_host =
|
2019-01-24 09:15:23 -07:00
|
|
|
Repo.all(
|
|
|
|
from(i in Instance,
|
2019-02-03 02:41:27 -07:00
|
|
|
where: i.host in ^hosts,
|
|
|
|
select: {i.host, i.unreachable_since}
|
2019-01-24 09:15:23 -07:00
|
|
|
)
|
|
|
|
)
|
2019-02-03 02:41:27 -07:00
|
|
|
|> Map.new(& &1)
|
2019-01-24 09:15:23 -07:00
|
|
|
|
2019-02-03 02:41:27 -07:00
|
|
|
reachability_datetime_threshold = Instances.reachability_datetime_threshold()
|
|
|
|
|
|
|
|
for entry <- Enum.filter(urls_or_hosts, &is_binary/1) do
|
|
|
|
host = host(entry)
|
|
|
|
unreachable_since = unreachable_since_by_host[host]
|
|
|
|
|
|
|
|
if !unreachable_since ||
|
|
|
|
NaiveDateTime.compare(unreachable_since, reachability_datetime_threshold) == :gt do
|
|
|
|
{entry, unreachable_since}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|> Enum.filter(& &1)
|
|
|
|
|> Map.new(& &1)
|
2019-01-24 09:15:23 -07:00
|
|
|
end
|
|
|
|
|
2019-01-28 05:25:06 -07:00
|
|
|
def reachable?(url_or_host) when is_binary(url_or_host) do
|
2019-01-23 08:37:25 -07:00
|
|
|
!Repo.one(
|
|
|
|
from(i in Instance,
|
|
|
|
where:
|
2019-01-28 05:25:06 -07:00
|
|
|
i.host == ^host(url_or_host) and
|
2019-01-25 05:10:21 -07:00
|
|
|
i.unreachable_since <= ^Instances.reachability_datetime_threshold(),
|
2019-01-23 08:37:25 -07:00
|
|
|
select: true
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-01-24 07:37:23 -07:00
|
|
|
def reachable?(_), do: true
|
|
|
|
|
2019-01-28 05:25:06 -07:00
|
|
|
def set_reachable(url_or_host) when is_binary(url_or_host) do
|
|
|
|
with host <- host(url_or_host),
|
2019-01-24 09:15:23 -07:00
|
|
|
%Instance{} = existing_record <- Repo.get_by(Instance, %{host: host}) do
|
|
|
|
{:ok, _instance} =
|
|
|
|
existing_record
|
2019-01-28 01:03:52 -07:00
|
|
|
|> changeset(%{unreachable_since: nil})
|
2019-01-24 09:15:23 -07:00
|
|
|
|> Repo.update()
|
|
|
|
end
|
2019-01-23 08:37:25 -07:00
|
|
|
end
|
|
|
|
|
2019-01-28 05:25:06 -07:00
|
|
|
def set_reachable(_), do: {:error, nil}
|
2019-01-24 07:37:23 -07:00
|
|
|
|
2019-01-28 05:25:06 -07:00
|
|
|
def set_unreachable(url_or_host, unreachable_since \\ nil)
|
2019-01-24 07:37:23 -07:00
|
|
|
|
2019-01-28 05:25:06 -07:00
|
|
|
def set_unreachable(url_or_host, unreachable_since) when is_binary(url_or_host) do
|
2019-01-23 08:37:25 -07:00
|
|
|
unreachable_since = unreachable_since || DateTime.utc_now()
|
2019-01-28 05:25:06 -07:00
|
|
|
host = host(url_or_host)
|
2019-01-23 08:37:25 -07:00
|
|
|
existing_record = Repo.get_by(Instance, %{host: host})
|
|
|
|
|
2019-01-28 01:03:52 -07:00
|
|
|
changes = %{unreachable_since: unreachable_since}
|
2019-01-23 08:37:25 -07:00
|
|
|
|
2019-01-28 01:03:52 -07:00
|
|
|
cond do
|
|
|
|
is_nil(existing_record) ->
|
2019-01-24 09:15:23 -07:00
|
|
|
%Instance{}
|
|
|
|
|> changeset(Map.put(changes, :host, host))
|
|
|
|
|> Repo.insert()
|
2019-01-28 01:03:52 -07:00
|
|
|
|
|
|
|
existing_record.unreachable_since &&
|
|
|
|
NaiveDateTime.compare(existing_record.unreachable_since, unreachable_since) != :gt ->
|
2019-01-28 05:25:06 -07:00
|
|
|
{:ok, existing_record}
|
2019-01-28 01:03:52 -07:00
|
|
|
|
|
|
|
true ->
|
|
|
|
existing_record
|
|
|
|
|> changeset(changes)
|
|
|
|
|> Repo.update()
|
2019-01-23 08:37:25 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-28 05:25:06 -07:00
|
|
|
def set_unreachable(_, _), do: {:error, nil}
|
2019-01-23 08:37:25 -07:00
|
|
|
end
|