2019-07-10 07:13:23 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 23:44:49 +01:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-07-10 07:13:23 +02:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-01-23 16:37:25 +01:00
|
|
|
defmodule Pleroma.Instances.Instance do
|
|
|
|
@moduledoc "Instance."
|
|
|
|
|
2019-02-09 16:16:26 +01:00
|
|
|
alias Pleroma.Instances
|
2019-01-23 16:37:25 +01:00
|
|
|
alias Pleroma.Instances.Instance
|
2019-03-05 03:52:23 +01:00
|
|
|
alias Pleroma.Repo
|
2019-01-23 16:37:25 +01:00
|
|
|
|
|
|
|
use Ecto.Schema
|
|
|
|
|
2019-02-09 16:20:18 +01:00
|
|
|
import Ecto.Query
|
|
|
|
import Ecto.Changeset
|
2019-01-23 16:37:25 +01:00
|
|
|
|
|
|
|
schema "instances" do
|
|
|
|
field(:host, :string)
|
2019-03-20 13:59:27 +01:00
|
|
|
field(:unreachable_since, :naive_datetime_usec)
|
2020-07-07 11:13:38 +02:00
|
|
|
field(:favicon, :string)
|
|
|
|
field(:favicon_updated_at, :naive_datetime)
|
2019-01-23 16:37:25 +01:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2019-01-28 13:25:06 +01:00
|
|
|
defdelegate host(url_or_host), to: Instances
|
2019-01-24 17:15:23 +01:00
|
|
|
|
|
|
|
def changeset(struct, params \\ %{}) do
|
2019-01-23 16:37:25 +01:00
|
|
|
struct
|
2020-07-07 11:13:38 +02:00
|
|
|
|> cast(params, [:host, :unreachable_since, :favicon, :favicon_updated_at])
|
2019-01-24 17:15:23 +01:00
|
|
|
|> validate_required([:host])
|
2019-01-23 16:37:25 +01:00
|
|
|
|> unique_constraint(:host)
|
|
|
|
end
|
|
|
|
|
2019-02-03 10:41:27 +01:00
|
|
|
def filter_reachable([]), do: %{}
|
2019-01-24 17:15:23 +01:00
|
|
|
|
2019-01-28 13:25:06 +01:00
|
|
|
def filter_reachable(urls_or_hosts) when is_list(urls_or_hosts) do
|
2019-01-24 17:15:23 +01:00
|
|
|
hosts =
|
2019-01-28 13:25:06 +01:00
|
|
|
urls_or_hosts
|
2019-01-24 17:15:23 +01:00
|
|
|
|> Enum.map(&(&1 && host(&1)))
|
|
|
|
|> Enum.filter(&(to_string(&1) != ""))
|
|
|
|
|
2019-02-03 10:41:27 +01:00
|
|
|
unreachable_since_by_host =
|
2019-01-24 17:15:23 +01:00
|
|
|
Repo.all(
|
|
|
|
from(i in Instance,
|
2019-02-03 10:41:27 +01:00
|
|
|
where: i.host in ^hosts,
|
|
|
|
select: {i.host, i.unreachable_since}
|
2019-01-24 17:15:23 +01:00
|
|
|
)
|
|
|
|
)
|
2019-02-03 10:41:27 +01:00
|
|
|
|> Map.new(& &1)
|
2019-01-24 17:15:23 +01:00
|
|
|
|
2019-02-03 10:41:27 +01: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 17:15:23 +01:00
|
|
|
end
|
|
|
|
|
2019-01-28 13:25:06 +01:00
|
|
|
def reachable?(url_or_host) when is_binary(url_or_host) do
|
2019-01-23 16:37:25 +01:00
|
|
|
!Repo.one(
|
|
|
|
from(i in Instance,
|
|
|
|
where:
|
2019-01-28 13:25:06 +01:00
|
|
|
i.host == ^host(url_or_host) and
|
2019-01-25 13:10:21 +01:00
|
|
|
i.unreachable_since <= ^Instances.reachability_datetime_threshold(),
|
2019-01-23 16:37:25 +01:00
|
|
|
select: true
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-01-24 15:37:23 +01:00
|
|
|
def reachable?(_), do: true
|
|
|
|
|
2019-01-28 13:25:06 +01:00
|
|
|
def set_reachable(url_or_host) when is_binary(url_or_host) do
|
|
|
|
with host <- host(url_or_host),
|
2019-01-24 17:15:23 +01:00
|
|
|
%Instance{} = existing_record <- Repo.get_by(Instance, %{host: host}) do
|
|
|
|
{:ok, _instance} =
|
|
|
|
existing_record
|
2019-01-28 09:03:52 +01:00
|
|
|
|> changeset(%{unreachable_since: nil})
|
2019-01-24 17:15:23 +01:00
|
|
|
|> Repo.update()
|
|
|
|
end
|
2019-01-23 16:37:25 +01:00
|
|
|
end
|
|
|
|
|
2019-01-28 13:25:06 +01:00
|
|
|
def set_reachable(_), do: {:error, nil}
|
2019-01-24 15:37:23 +01:00
|
|
|
|
2019-01-28 13:25:06 +01:00
|
|
|
def set_unreachable(url_or_host, unreachable_since \\ nil)
|
2019-01-24 15:37:23 +01:00
|
|
|
|
2019-01-28 13:25:06 +01:00
|
|
|
def set_unreachable(url_or_host, unreachable_since) when is_binary(url_or_host) do
|
2019-08-10 19:38:31 +02:00
|
|
|
unreachable_since = parse_datetime(unreachable_since) || NaiveDateTime.utc_now()
|
2019-01-28 13:25:06 +01:00
|
|
|
host = host(url_or_host)
|
2019-01-23 16:37:25 +01:00
|
|
|
existing_record = Repo.get_by(Instance, %{host: host})
|
|
|
|
|
2019-01-28 09:03:52 +01:00
|
|
|
changes = %{unreachable_since: unreachable_since}
|
2019-01-23 16:37:25 +01:00
|
|
|
|
2019-01-28 09:03:52 +01:00
|
|
|
cond do
|
|
|
|
is_nil(existing_record) ->
|
2019-01-24 17:15:23 +01:00
|
|
|
%Instance{}
|
|
|
|
|> changeset(Map.put(changes, :host, host))
|
|
|
|
|> Repo.insert()
|
2019-01-28 09:03:52 +01:00
|
|
|
|
|
|
|
existing_record.unreachable_since &&
|
|
|
|
NaiveDateTime.compare(existing_record.unreachable_since, unreachable_since) != :gt ->
|
2019-01-28 13:25:06 +01:00
|
|
|
{:ok, existing_record}
|
2019-01-28 09:03:52 +01:00
|
|
|
|
|
|
|
true ->
|
|
|
|
existing_record
|
|
|
|
|> changeset(changes)
|
|
|
|
|> Repo.update()
|
2019-01-23 16:37:25 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-28 13:25:06 +01:00
|
|
|
def set_unreachable(_, _), do: {:error, nil}
|
2019-08-10 19:38:31 +02:00
|
|
|
|
|
|
|
defp parse_datetime(datetime) when is_binary(datetime) do
|
|
|
|
NaiveDateTime.from_iso8601(datetime)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp parse_datetime(datetime), do: datetime
|
2020-07-07 11:13:38 +02:00
|
|
|
|
|
|
|
def get_or_update_favicon(%URI{host: host} = instance_uri) do
|
|
|
|
existing_record = Repo.get_by(Instance, %{host: host})
|
|
|
|
now = NaiveDateTime.utc_now()
|
|
|
|
|
2020-07-07 12:07:30 +02:00
|
|
|
if existing_record && existing_record.favicon_updated_at &&
|
2020-07-07 11:13:38 +02:00
|
|
|
NaiveDateTime.diff(now, existing_record.favicon_updated_at) < 86_400 do
|
|
|
|
existing_record.favicon
|
|
|
|
else
|
|
|
|
favicon = scrape_favicon(instance_uri)
|
|
|
|
|
2020-07-07 12:07:30 +02:00
|
|
|
if existing_record do
|
|
|
|
existing_record
|
|
|
|
|> changeset(%{favicon: favicon, favicon_updated_at: now})
|
|
|
|
|> Repo.update()
|
|
|
|
else
|
|
|
|
%Instance{}
|
|
|
|
|> changeset(%{host: host, favicon: favicon, favicon_updated_at: now})
|
|
|
|
|> Repo.insert()
|
2020-07-07 11:13:38 +02:00
|
|
|
end
|
2020-07-07 12:07:30 +02:00
|
|
|
|
|
|
|
favicon
|
2020-07-07 11:13:38 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp scrape_favicon(%URI{} = instance_uri) do
|
|
|
|
try do
|
|
|
|
with {:ok, %Tesla.Env{body: html}} <-
|
2020-09-02 08:16:51 +02:00
|
|
|
Pleroma.HTTP.get(to_string(instance_uri), [{"accept", "text/html"}],
|
|
|
|
adapter: [pool: :media]
|
|
|
|
),
|
2020-07-07 11:13:38 +02:00
|
|
|
favicon_rel <-
|
|
|
|
html
|
|
|
|
|> Floki.parse_document!()
|
|
|
|
|> Floki.attribute("link[rel=icon]", "href")
|
|
|
|
|> List.first(),
|
|
|
|
favicon <- URI.merge(instance_uri, favicon_rel) |> to_string(),
|
|
|
|
true <- is_binary(favicon) do
|
|
|
|
favicon
|
|
|
|
else
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
end
|
2019-01-23 16:37:25 +01:00
|
|
|
end
|