2019-01-23 15:37:25 +00:00
|
|
|
defmodule Pleroma.Instances.Instance do
|
|
|
|
@moduledoc "Instance."
|
|
|
|
|
|
|
|
alias Pleroma.Instances
|
|
|
|
alias Pleroma.Instances.Instance
|
|
|
|
|
|
|
|
use Ecto.Schema
|
|
|
|
|
|
|
|
import Ecto.{Query, Changeset}
|
|
|
|
|
|
|
|
alias Pleroma.Repo
|
|
|
|
|
|
|
|
schema "instances" do
|
|
|
|
field(:host, :string)
|
|
|
|
field(:unreachable_since, :naive_datetime)
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2019-01-24 16:15:23 +00:00
|
|
|
defdelegate host(url), to: Instances
|
|
|
|
|
|
|
|
def changeset(struct, params \\ %{}) do
|
2019-01-23 15:37:25 +00:00
|
|
|
struct
|
2019-01-28 08:03:52 +00:00
|
|
|
|> cast(params, [:host, :unreachable_since])
|
2019-01-24 16:15:23 +00:00
|
|
|
|> validate_required([:host])
|
2019-01-23 15:37:25 +00:00
|
|
|
|> unique_constraint(:host)
|
|
|
|
end
|
|
|
|
|
2019-01-24 16:15:23 +00:00
|
|
|
def filter_reachable([]), do: []
|
|
|
|
|
|
|
|
def filter_reachable(urls) when is_list(urls) do
|
|
|
|
hosts =
|
|
|
|
urls
|
|
|
|
|> Enum.map(&(&1 && host(&1)))
|
|
|
|
|> Enum.filter(&(to_string(&1) != ""))
|
|
|
|
|
|
|
|
unreachable_hosts =
|
|
|
|
Repo.all(
|
|
|
|
from(i in Instance,
|
|
|
|
where:
|
2019-01-25 12:10:21 +00:00
|
|
|
i.host in ^hosts and
|
|
|
|
i.unreachable_since <= ^Instances.reachability_datetime_threshold(),
|
2019-01-24 16:15:23 +00:00
|
|
|
select: i.host
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
Enum.filter(urls, &(&1 && host(&1) not in unreachable_hosts))
|
|
|
|
end
|
|
|
|
|
2019-01-24 14:37:23 +00:00
|
|
|
def reachable?(url) when is_binary(url) do
|
2019-01-23 15:37:25 +00:00
|
|
|
!Repo.one(
|
|
|
|
from(i in Instance,
|
|
|
|
where:
|
2019-01-25 12:10:21 +00:00
|
|
|
i.host == ^host(url) and
|
|
|
|
i.unreachable_since <= ^Instances.reachability_datetime_threshold(),
|
2019-01-23 15:37:25 +00:00
|
|
|
select: true
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-01-24 14:37:23 +00:00
|
|
|
def reachable?(_), do: true
|
|
|
|
|
|
|
|
def set_reachable(url) when is_binary(url) do
|
2019-01-24 16:15:23 +00:00
|
|
|
with host <- host(url),
|
|
|
|
%Instance{} = existing_record <- Repo.get_by(Instance, %{host: host}) do
|
|
|
|
{:ok, _instance} =
|
|
|
|
existing_record
|
2019-01-28 08:03:52 +00:00
|
|
|
|> changeset(%{unreachable_since: nil})
|
2019-01-24 16:15:23 +00:00
|
|
|
|> Repo.update()
|
|
|
|
end
|
2019-01-23 15:37:25 +00:00
|
|
|
end
|
|
|
|
|
2019-01-24 14:37:23 +00:00
|
|
|
def set_reachable(_), do: {0, :noop}
|
|
|
|
|
|
|
|
def set_unreachable(url, unreachable_since \\ nil)
|
|
|
|
|
|
|
|
def set_unreachable(url, unreachable_since) when is_binary(url) do
|
2019-01-23 15:37:25 +00:00
|
|
|
unreachable_since = unreachable_since || DateTime.utc_now()
|
|
|
|
host = host(url)
|
|
|
|
existing_record = Repo.get_by(Instance, %{host: host})
|
|
|
|
|
2019-01-28 08:03:52 +00:00
|
|
|
changes = %{unreachable_since: unreachable_since}
|
2019-01-23 15:37:25 +00:00
|
|
|
|
2019-01-28 08:03:52 +00:00
|
|
|
cond do
|
|
|
|
is_nil(existing_record) ->
|
2019-01-24 16:15:23 +00:00
|
|
|
%Instance{}
|
|
|
|
|> changeset(Map.put(changes, :host, host))
|
|
|
|
|> Repo.insert()
|
2019-01-28 08:03:52 +00:00
|
|
|
|
|
|
|
existing_record.unreachable_since &&
|
|
|
|
NaiveDateTime.compare(existing_record.unreachable_since, unreachable_since) != :gt ->
|
|
|
|
{:noop, existing_record}
|
|
|
|
|
|
|
|
true ->
|
|
|
|
existing_record
|
|
|
|
|> changeset(changes)
|
|
|
|
|> Repo.update()
|
2019-01-23 15:37:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-24 14:37:23 +00:00
|
|
|
def set_unreachable(_, _), do: {0, :noop}
|
2019-01-23 15:37:25 +00:00
|
|
|
end
|