instance.ex 2.88 KB
Newer Older
1 2 3
defmodule Pleroma.Instances.Instance do
  @moduledoc "Instance."

Haelwenn's avatar
Haelwenn committed
4
  alias Pleroma.Instances
5
  alias Pleroma.Instances.Instance
6
  alias Pleroma.Repo
7 8 9

  use Ecto.Schema

Haelwenn's avatar
Haelwenn committed
10 11
  import Ecto.Query
  import Ecto.Changeset
12 13 14

  schema "instances" do
    field(:host, :string)
rinpatch's avatar
rinpatch committed
15
    field(:unreachable_since, :naive_datetime_usec)
16 17 18 19

    timestamps()
  end

20
  defdelegate host(url_or_host), to: Instances
21 22

  def changeset(struct, params \\ %{}) do
23
    struct
24
    |> cast(params, [:host, :unreachable_since])
25
    |> validate_required([:host])
26 27 28
    |> unique_constraint(:host)
  end

29
  def filter_reachable([]), do: %{}
30

31
  def filter_reachable(urls_or_hosts) when is_list(urls_or_hosts) do
32
    hosts =
33
      urls_or_hosts
34 35 36
      |> Enum.map(&(&1 && host(&1)))
      |> Enum.filter(&(to_string(&1) != ""))

37
    unreachable_since_by_host =
38 39
      Repo.all(
        from(i in Instance,
40 41
          where: i.host in ^hosts,
          select: {i.host, i.unreachable_since}
42 43
        )
      )
44
      |> Map.new(& &1)
45

46 47 48 49 50 51 52 53 54 55 56 57 58
    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)
59 60
  end

61
  def reachable?(url_or_host) when is_binary(url_or_host) do
62 63 64
    !Repo.one(
      from(i in Instance,
        where:
65
          i.host == ^host(url_or_host) and
66
            i.unreachable_since <= ^Instances.reachability_datetime_threshold(),
67 68 69 70 71
        select: true
      )
    )
  end

72 73
  def reachable?(_), do: true

74 75
  def set_reachable(url_or_host) when is_binary(url_or_host) do
    with host <- host(url_or_host),
76 77 78
         %Instance{} = existing_record <- Repo.get_by(Instance, %{host: host}) do
      {:ok, _instance} =
        existing_record
79
        |> changeset(%{unreachable_since: nil})
80 81
        |> Repo.update()
    end
82 83
  end

84
  def set_reachable(_), do: {:error, nil}
85

86
  def set_unreachable(url_or_host, unreachable_since \\ nil)
87

88
  def set_unreachable(url_or_host, unreachable_since) when is_binary(url_or_host) do
89
    unreachable_since = unreachable_since || DateTime.utc_now()
90
    host = host(url_or_host)
91 92
    existing_record = Repo.get_by(Instance, %{host: host})

93
    changes = %{unreachable_since: unreachable_since}
94

95 96
    cond do
      is_nil(existing_record) ->
97 98 99
        %Instance{}
        |> changeset(Map.put(changes, :host, host))
        |> Repo.insert()
100 101 102

      existing_record.unreachable_since &&
          NaiveDateTime.compare(existing_record.unreachable_since, unreachable_since) != :gt ->
103
        {:ok, existing_record}
104 105 106 107 108

      true ->
        existing_record
        |> changeset(changes)
        |> Repo.update()
109 110 111
    end
  end

112
  def set_unreachable(_, _), do: {:error, nil}
113
end