+ changes = %{unreachable_since: unreachable_since}
+
+ cond do
+ is_nil(existing_record) ->
+ %Instance{}
+ |> changeset(Map.put(changes, :host, host))
+ |> Repo.insert()
+
+ existing_record.unreachable_since &&
+ NaiveDateTime.compare(existing_record.unreachable_since, unreachable_since) != :gt ->
+ {:ok, existing_record}
+
+ true ->
+ existing_record
+ |> changeset(changes)
+ |> Repo.update()
+ end
+ end
+
+ def set_unreachable(_, _), do: {:error, nil}
+
+ def get_consistently_unreachable do
+ reachability_datetime_threshold = Instances.reachability_datetime_threshold()
+
+ from(i in Instance,
+ where: ^reachability_datetime_threshold > i.unreachable_since,
+ order_by: i.unreachable_since,
+ select: {i.host, i.unreachable_since}
+ )
+ |> Repo.all()
+ end
+
+ defp parse_datetime(datetime) when is_binary(datetime) do
+ NaiveDateTime.from_iso8601(datetime)
+ end
+
+ defp parse_datetime(datetime), do: datetime
+
+ def needs_update(nil), do: true
+
+ def needs_update(%Instance{metadata_updated_at: nil}), do: true
+
+ def needs_update(%Instance{metadata_updated_at: metadata_updated_at}) do
+ now = NaiveDateTime.utc_now()
+ NaiveDateTime.diff(now, metadata_updated_at) > 86_400
+ end
+
+ def local do
+ %Instance{
+ host: Pleroma.Web.Endpoint.host(),
+ favicon: Pleroma.Web.Endpoint.url() <> "/favicon.png",
+ nodeinfo: Pleroma.Web.Nodeinfo.NodeinfoController.raw_nodeinfo()