forked from AkkomaGang/akkoma
Compare commits
No commits in common. "instance-info" and "develop" have entirely different histories.
instance-i
...
develop
14 changed files with 111 additions and 502 deletions
|
@ -567,8 +567,7 @@
|
|||
attachments_cleanup: 1,
|
||||
new_users_digest: 1,
|
||||
mute_expire: 5,
|
||||
search_indexing: 10,
|
||||
nodeinfo_fetcher: 1
|
||||
search_indexing: 10
|
||||
],
|
||||
plugins: [
|
||||
Oban.Plugins.Pruner,
|
||||
|
@ -807,8 +806,7 @@
|
|||
|
||||
config :web_push_encryption, http_client: Pleroma.HTTP.WebPush
|
||||
|
||||
config :pleroma, :instances_favicons, enabled: true
|
||||
config :pleroma, :instances_nodeinfo, enabled: true
|
||||
config :pleroma, :instances_favicons, enabled: false
|
||||
|
||||
config :floki, :html_parser, Floki.HTMLParser.FastHtml
|
||||
|
||||
|
|
|
@ -3047,19 +3047,6 @@
|
|||
}
|
||||
]
|
||||
},
|
||||
%{
|
||||
group: :pleroma,
|
||||
key: :instances_nodeinfo,
|
||||
type: :group,
|
||||
description: "Control favicons for instances",
|
||||
children: [
|
||||
%{
|
||||
key: :enabled,
|
||||
type: :boolean,
|
||||
description: "Allow/disallow getting instance nodeinfo"
|
||||
}
|
||||
]
|
||||
},
|
||||
%{
|
||||
group: :ex_aws,
|
||||
key: :s3,
|
||||
|
|
|
@ -139,8 +139,6 @@
|
|||
# Reduce recompilation time
|
||||
# https://dashbit.co/blog/speeding-up-re-compilation-of-elixir-projects
|
||||
config :phoenix, :plug_init_mode, :runtime
|
||||
config :pleroma, :instances_favicons, enabled: false
|
||||
config :pleroma, :instances_nodeinfo, enabled: false
|
||||
|
||||
if File.exists?("./config/test.secret.exs") do
|
||||
import_config "test.secret.exs"
|
||||
|
|
|
@ -156,8 +156,7 @@ defp cachex_children do
|
|||
build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
|
||||
build_cachex("failed_proxy_url", limit: 2500),
|
||||
build_cachex("banned_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000),
|
||||
build_cachex("translations", default_ttl: :timer.hours(24 * 30), limit: 2500),
|
||||
build_cachex("instances", default_ttl: :timer.hours(24), limit: 2500)
|
||||
build_cachex("translations", default_ttl: :timer.hours(24 * 30), limit: 2500)
|
||||
]
|
||||
end
|
||||
|
||||
|
|
|
@ -5,8 +5,6 @@
|
|||
defmodule Pleroma.Instances.Instance do
|
||||
@moduledoc "Instance."
|
||||
|
||||
@cachex Pleroma.Config.get([:cachex, :provider], Cachex)
|
||||
|
||||
alias Pleroma.Instances
|
||||
alias Pleroma.Instances.Instance
|
||||
alias Pleroma.Repo
|
||||
|
@ -24,8 +22,7 @@ defmodule Pleroma.Instances.Instance do
|
|||
field(:host, :string)
|
||||
field(:unreachable_since, :naive_datetime_usec)
|
||||
field(:favicon, :string)
|
||||
field(:metadata_updated_at, :naive_datetime)
|
||||
field(:nodeinfo, :map, default: %{})
|
||||
field(:favicon_updated_at, :naive_datetime)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
@ -34,7 +31,7 @@ defmodule Pleroma.Instances.Instance do
|
|||
|
||||
def changeset(struct, params \\ %{}) do
|
||||
struct
|
||||
|> cast(params, [:host, :unreachable_since, :favicon, :nodeinfo, :metadata_updated_at])
|
||||
|> cast(params, [:host, :unreachable_since, :favicon, :favicon_updated_at])
|
||||
|> validate_required([:host])
|
||||
|> unique_constraint(:host)
|
||||
end
|
||||
|
@ -141,125 +138,44 @@ defp parse_datetime(datetime) when is_binary(datetime) do
|
|||
|
||||
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
|
||||
def get_or_update_favicon(%URI{host: host} = instance_uri) do
|
||||
existing_record = Repo.get_by(Instance, %{host: host})
|
||||
now = NaiveDateTime.utc_now()
|
||||
NaiveDateTime.diff(now, metadata_updated_at) > 86_400
|
||||
end
|
||||
|
||||
def update_metadata(%URI{host: host} = uri) do
|
||||
Logger.info("Checking metadata for #{host}")
|
||||
existing_record = Repo.get_by(Instance, %{host: host})
|
||||
|
||||
if reachable?(host) do
|
||||
do_update_metadata(uri, existing_record)
|
||||
else
|
||||
{:discard, :unreachable}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_update_metadata(%URI{host: host} = uri, existing_record) do
|
||||
if existing_record do
|
||||
if needs_update(existing_record) do
|
||||
Logger.info("Updating metadata for #{host}")
|
||||
favicon = scrape_favicon(uri)
|
||||
nodeinfo = scrape_nodeinfo(uri)
|
||||
|
||||
{:ok, instance} =
|
||||
existing_record
|
||||
|> changeset(%{
|
||||
host: host,
|
||||
favicon: favicon,
|
||||
nodeinfo: nodeinfo,
|
||||
metadata_updated_at: NaiveDateTime.utc_now()
|
||||
})
|
||||
|> Repo.update()
|
||||
|
||||
@cachex.put(:instances_cache, "instances:#{host}", instance)
|
||||
else
|
||||
{:discard, "Does not require update"}
|
||||
end
|
||||
else
|
||||
favicon = scrape_favicon(uri)
|
||||
nodeinfo = scrape_nodeinfo(uri)
|
||||
|
||||
Logger.info("Creating metadata for #{host}")
|
||||
|
||||
{:ok, instance} =
|
||||
%Instance{}
|
||||
|> changeset(%{
|
||||
host: host,
|
||||
favicon: favicon,
|
||||
nodeinfo: nodeinfo,
|
||||
metadata_updated_at: NaiveDateTime.utc_now()
|
||||
})
|
||||
|> Repo.insert()
|
||||
|
||||
@cachex.put(:instances_cache, "instances:#{host}", instance)
|
||||
end
|
||||
end
|
||||
|
||||
def get_favicon(%URI{host: host}) do
|
||||
existing_record = Repo.get_by(Instance, %{host: host})
|
||||
|
||||
if existing_record do
|
||||
if existing_record && existing_record.favicon_updated_at &&
|
||||
NaiveDateTime.diff(now, existing_record.favicon_updated_at) < 86_400 do
|
||||
existing_record.favicon
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
favicon = scrape_favicon(instance_uri)
|
||||
|
||||
defp scrape_nodeinfo(%URI{} = instance_uri) do
|
||||
with true <- Pleroma.Config.get([:instances_nodeinfo, :enabled]),
|
||||
{_, true} <- {:reachable, reachable?(instance_uri.host)},
|
||||
{:ok, %Tesla.Env{status: 200, body: body}} <-
|
||||
Tesla.get(
|
||||
"https://#{instance_uri.host}/.well-known/nodeinfo",
|
||||
headers: [{"Accept", "application/json"}]
|
||||
),
|
||||
{:ok, json} <- Jason.decode(body),
|
||||
{:ok, %{"links" => links}} <- {:ok, json},
|
||||
{:ok, %{"href" => href}} <-
|
||||
{:ok,
|
||||
Enum.find(links, &(&1["rel"] == "http://nodeinfo.diaspora.software/ns/schema/2.0"))},
|
||||
{:ok, %Tesla.Env{body: data}} <-
|
||||
Pleroma.HTTP.get(href, [{"accept", "application/json"}], []),
|
||||
{:length, true} <- {:length, String.length(data) < 50_000},
|
||||
{:ok, nodeinfo} <- Jason.decode(data) do
|
||||
nodeinfo
|
||||
if existing_record do
|
||||
existing_record
|
||||
|> changeset(%{favicon: favicon, favicon_updated_at: now})
|
||||
|> Repo.update()
|
||||
else
|
||||
{:reachable, false} ->
|
||||
Logger.debug(
|
||||
"Instance.scrape_nodeinfo(\"#{to_string(instance_uri)}\") ignored unreachable host"
|
||||
)
|
||||
|
||||
nil
|
||||
|
||||
{:length, false} ->
|
||||
Logger.debug(
|
||||
"Instance.scrape_nodeinfo(\"#{to_string(instance_uri)}\") ignored too long body"
|
||||
)
|
||||
|
||||
nil
|
||||
|
||||
_ ->
|
||||
nil
|
||||
%Instance{}
|
||||
|> changeset(%{host: host, favicon: favicon, favicon_updated_at: now})
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
favicon
|
||||
end
|
||||
rescue
|
||||
e ->
|
||||
Logger.warn("Instance.get_or_update_favicon(\"#{host}\") error: #{inspect(e)}")
|
||||
nil
|
||||
end
|
||||
|
||||
defp scrape_favicon(%URI{} = instance_uri) do
|
||||
with true <- Pleroma.Config.get([:instances_favicons, :enabled]),
|
||||
{_, true} <- {:reachable, reachable?(instance_uri.host)},
|
||||
try do
|
||||
with {_, true} <- {:reachable, reachable?(instance_uri.host)},
|
||||
{:ok, %Tesla.Env{body: html}} <-
|
||||
Pleroma.HTTP.get(to_string(instance_uri), [{"accept", "text/html"}], []),
|
||||
{_, [favicon_rel | _]} when is_binary(favicon_rel) <-
|
||||
{:parse, html |> Floki.parse_document!() |> Floki.attribute("link[rel=icon]", "href")},
|
||||
{:parse,
|
||||
html |> Floki.parse_document!() |> Floki.attribute("link[rel=icon]", "href")},
|
||||
{_, favicon} when is_binary(favicon) <-
|
||||
{:merge, URI.merge(instance_uri, favicon_rel) |> to_string()},
|
||||
{:length, true} <- {:length, String.length(favicon) < 255} do
|
||||
{:merge, URI.merge(instance_uri, favicon_rel) |> to_string()} do
|
||||
favicon
|
||||
else
|
||||
{:reachable, false} ->
|
||||
|
@ -270,6 +186,14 @@ defp scrape_favicon(%URI{} = instance_uri) do
|
|||
nil
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
rescue
|
||||
e ->
|
||||
Logger.warn(
|
||||
"Instance.scrape_favicon(\"#{to_string(instance_uri)}\") error: #{inspect(e)}"
|
||||
)
|
||||
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
@ -293,21 +217,4 @@ def perform(:delete_instance, host) when is_binary(host) do
|
|||
end)
|
||||
|> Stream.run()
|
||||
end
|
||||
|
||||
def get_by_url(url_or_host) do
|
||||
url = host(url_or_host)
|
||||
Repo.get_by(Instance, host: url)
|
||||
end
|
||||
|
||||
def get_cached_by_url(url_or_host) do
|
||||
url = host(url_or_host)
|
||||
|
||||
@cachex.fetch!(:instances_cache, "instances:#{url}", fn _ ->
|
||||
with %Instance{} = instance <- get_by_url(url) do
|
||||
{:commit, {:ok, instance}}
|
||||
else
|
||||
_ -> {:ignore, nil}
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -192,7 +192,6 @@ def handle(%{data: %{"type" => "Like"}} = object, meta) do
|
|||
# - Increase the user note count
|
||||
# - Increase the reply count
|
||||
# - Increase replies count
|
||||
# - Ask for scraping of nodeinfo
|
||||
# - Set up ActivityExpiration
|
||||
# - Set up notifications
|
||||
# - Index incoming posts for search (if needed)
|
||||
|
@ -210,10 +209,6 @@ def handle(%{data: %{"type" => "Create"}} = activity, meta) do
|
|||
|
||||
reply_depth = (meta[:depth] || 0) + 1
|
||||
|
||||
Pleroma.Workers.NodeInfoFetcherWorker.enqueue("process", %{
|
||||
"source_url" => activity.data["actor"]
|
||||
})
|
||||
|
||||
# FIXME: Force inReplyTo to replies
|
||||
if Pleroma.Web.Federator.allowed_thread_distance?(reply_depth) and
|
||||
object.data["replies"] != nil do
|
||||
|
@ -239,9 +234,7 @@ def handle(%{data: %{"type" => "Create"}} = activity, meta) do
|
|||
|
||||
{:ok, activity, meta}
|
||||
else
|
||||
e ->
|
||||
Logger.error(inspect(e))
|
||||
Repo.rollback(e)
|
||||
e -> Repo.rollback(e)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -186,16 +186,6 @@ def render("relationships.json", %{user: user, targets: targets} = opts) do
|
|||
render_many(targets, AccountView, "relationship.json", render_opts)
|
||||
end
|
||||
|
||||
def render("instance.json", %{instance: %Pleroma.Instances.Instance{} = instance}) do
|
||||
%{
|
||||
name: instance.host,
|
||||
favicon: instance.favicon |> MediaProxy.url(),
|
||||
nodeinfo: instance.nodeinfo
|
||||
}
|
||||
end
|
||||
|
||||
def render("instance.json", _), do: nil
|
||||
|
||||
defp do_render("show.json", %{user: user} = opts) do
|
||||
user = User.sanitize_html(user, User.html_filter_policy(opts[:for]))
|
||||
display_name = user.name || user.nickname
|
||||
|
@ -240,20 +230,16 @@ defp do_render("show.json", %{user: user} = opts) do
|
|||
%{}
|
||||
end
|
||||
|
||||
instance =
|
||||
with {:ok, instance} <- Pleroma.Instances.Instance.get_cached_by_url(user.ap_id) do
|
||||
instance
|
||||
else
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
|
||||
favicon =
|
||||
if is_nil(instance) do
|
||||
nil
|
||||
else
|
||||
instance.favicon
|
||||
if Pleroma.Config.get([:instances_favicons, :enabled]) do
|
||||
user
|
||||
|> Map.get(:ap_id, "")
|
||||
|> URI.parse()
|
||||
|> URI.merge("/")
|
||||
|> Pleroma.Instances.Instance.get_or_update_favicon()
|
||||
|> MediaProxy.url()
|
||||
else
|
||||
nil
|
||||
end
|
||||
|
||||
%{
|
||||
|
@ -285,9 +271,7 @@ defp do_render("show.json", %{user: user} = opts) do
|
|||
}
|
||||
},
|
||||
last_status_at: user.last_status_at,
|
||||
akkoma: %{
|
||||
instance: render("instance.json", %{instance: instance})
|
||||
},
|
||||
|
||||
# Pleroma extensions
|
||||
# Note: it's insecure to output :email but fully-qualified nickname may serve as safe stub
|
||||
fqn: User.full_nickname(user),
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
defmodule Pleroma.Workers.NodeInfoFetcherWorker do
|
||||
use Pleroma.Workers.WorkerHelper, queue: "nodeinfo_fetcher"
|
||||
|
||||
alias Oban.Job
|
||||
alias Pleroma.Instances.Instance
|
||||
|
||||
@impl Oban.Worker
|
||||
def perform(%Job{
|
||||
args: %{"op" => "process", "source_url" => domain}
|
||||
}) do
|
||||
uri =
|
||||
domain
|
||||
|> URI.parse()
|
||||
|> URI.merge("/")
|
||||
|
||||
Instance.update_metadata(uri)
|
||||
end
|
||||
end
|
|
@ -1,17 +0,0 @@
|
|||
defmodule Pleroma.Repo.Migrations.AddNodeinfo do
|
||||
use Ecto.Migration
|
||||
|
||||
def up do
|
||||
alter table(:instances) do
|
||||
add_if_not_exists(:nodeinfo, :map, default: %{})
|
||||
add_if_not_exists(:metadata_updated_at, :naive_datetime)
|
||||
end
|
||||
end
|
||||
|
||||
def down do
|
||||
alter table(:instances) do
|
||||
remove_if_exists(:nodeinfo, :map)
|
||||
remove_if_exists(:metadata_updated_at, :naive_datetime)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -9,16 +9,12 @@ defmodule Pleroma.Instances.InstanceTest do
|
|||
alias Pleroma.Tests.ObanHelpers
|
||||
alias Pleroma.Web.CommonAPI
|
||||
|
||||
use Pleroma.DataCase, async: true
|
||||
use Pleroma.DataCase
|
||||
|
||||
import ExUnit.CaptureLog
|
||||
import Pleroma.Factory
|
||||
|
||||
setup_all do
|
||||
clear_config([:instance, :federation_reachability_timeout_days], 1)
|
||||
clear_config([:instances_nodeinfo, :enabled], true)
|
||||
clear_config([:instances_favicons, :enabled], true)
|
||||
end
|
||||
setup_all do: clear_config([:instance, :federation_reachability_timeout_days], 1)
|
||||
|
||||
describe "set_reachable/1" do
|
||||
test "clears `unreachable_since` of existing matching Instance record having non-nil `unreachable_since`" do
|
||||
|
@ -106,220 +102,62 @@ test "does NOT modify `unreachable_since` value of existing record in case it's
|
|||
end
|
||||
end
|
||||
|
||||
describe "update_metadata/1" do
|
||||
test "Scrapes favicon URLs and nodeinfo" do
|
||||
Tesla.Mock.mock(fn
|
||||
%{url: "https://favicon.example.org/"} ->
|
||||
describe "get_or_update_favicon/1" do
|
||||
test "Scrapes favicon URLs" do
|
||||
Tesla.Mock.mock(fn %{url: "https://favicon.example.org/"} ->
|
||||
%Tesla.Env{
|
||||
status: 200,
|
||||
body: ~s[<html><head><link rel="icon" href="/favicon.png"></head></html>]
|
||||
}
|
||||
|
||||
%{url: "https://favicon.example.org/.well-known/nodeinfo"} ->
|
||||
%Tesla.Env{
|
||||
status: 200,
|
||||
body:
|
||||
Jason.encode!(%{
|
||||
links: [
|
||||
%{
|
||||
rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
|
||||
href: "https://favicon.example.org/nodeinfo/2.0"
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
%{url: "https://favicon.example.org/nodeinfo/2.0"} ->
|
||||
%Tesla.Env{
|
||||
status: 200,
|
||||
body: Jason.encode!(%{version: "2.0", software: %{name: "Akkoma"}})
|
||||
}
|
||||
end)
|
||||
|
||||
assert {:ok, true} ==
|
||||
Instance.update_metadata(URI.parse("https://favicon.example.org/"))
|
||||
|
||||
{:ok, instance} = Instance.get_cached_by_url("https://favicon.example.org/")
|
||||
assert instance.favicon == "https://favicon.example.org/favicon.png"
|
||||
assert instance.nodeinfo == %{"version" => "2.0", "software" => %{"name" => "Akkoma"}}
|
||||
assert "https://favicon.example.org/favicon.png" ==
|
||||
Instance.get_or_update_favicon(URI.parse("https://favicon.example.org/"))
|
||||
end
|
||||
|
||||
test "Does not retain favicons that are too long" do
|
||||
test "Returns nil on too long favicon URLs" do
|
||||
long_favicon_url =
|
||||
"https://Lorem.ipsum.dolor.sit.amet/consecteturadipiscingelit/Praesentpharetrapurusutaliquamtempus/Mauriseulaoreetarcu/atfacilisisorci/Nullamporttitor/nequesedfeugiatmollis/dolormagnaefficiturlorem/nonpretiumsapienorcieurisus/Nullamveleratsem/Maecenassedaccumsanexnam/favicon.png"
|
||||
|
||||
Tesla.Mock.mock(fn
|
||||
%{url: "https://long-favicon.example.org/"} ->
|
||||
Tesla.Mock.mock(fn %{url: "https://long-favicon.example.org/"} ->
|
||||
%Tesla.Env{
|
||||
status: 200,
|
||||
body:
|
||||
~s[<html><head><link rel="icon" href="] <> long_favicon_url <> ~s["></head></html>]
|
||||
}
|
||||
|
||||
%{url: "https://long-favicon.example.org/.well-known/nodeinfo"} ->
|
||||
%Tesla.Env{
|
||||
status: 200,
|
||||
body:
|
||||
Jason.encode!(%{
|
||||
links: [
|
||||
%{
|
||||
rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
|
||||
href: "https://long-favicon.example.org/nodeinfo/2.0"
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
%{url: "https://long-favicon.example.org/nodeinfo/2.0"} ->
|
||||
%Tesla.Env{
|
||||
status: 200,
|
||||
body: Jason.encode!(%{version: "2.0", software: %{name: "Akkoma"}})
|
||||
}
|
||||
end)
|
||||
|
||||
assert {:ok, true} ==
|
||||
Instance.update_metadata(URI.parse("https://long-favicon.example.org/"))
|
||||
|
||||
{:ok, instance} = Instance.get_cached_by_url("https://long-favicon.example.org/")
|
||||
assert instance.favicon == nil
|
||||
assert capture_log(fn ->
|
||||
assert nil ==
|
||||
Instance.get_or_update_favicon(
|
||||
URI.parse("https://long-favicon.example.org/")
|
||||
)
|
||||
end) =~
|
||||
"Instance.get_or_update_favicon(\"long-favicon.example.org\") error: %Postgrex.Error{"
|
||||
end
|
||||
|
||||
test "Handles not getting a favicon URL properly" do
|
||||
Tesla.Mock.mock(fn
|
||||
%{url: "https://no-favicon.example.org/"} ->
|
||||
Tesla.Mock.mock(fn %{url: "https://no-favicon.example.org/"} ->
|
||||
%Tesla.Env{
|
||||
status: 200,
|
||||
body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
|
||||
}
|
||||
|
||||
%{url: "https://no-favicon.example.org/.well-known/nodeinfo"} ->
|
||||
%Tesla.Env{
|
||||
status: 200,
|
||||
body:
|
||||
Jason.encode!(%{
|
||||
links: [
|
||||
%{
|
||||
rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
|
||||
href: "https://no-favicon.example.org/nodeinfo/2.0"
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
%{url: "https://no-favicon.example.org/nodeinfo/2.0"} ->
|
||||
%Tesla.Env{
|
||||
status: 200,
|
||||
body: Jason.encode!(%{version: "2.0", software: %{name: "Akkoma"}})
|
||||
}
|
||||
end)
|
||||
|
||||
refute capture_log(fn ->
|
||||
assert {:ok, true} =
|
||||
Instance.update_metadata(URI.parse("https://no-favicon.example.org/"))
|
||||
end) =~ "Instance.update_metadata(\"https://no-favicon.example.org/\") error: "
|
||||
assert nil ==
|
||||
Instance.get_or_update_favicon(
|
||||
URI.parse("https://no-favicon.example.org/")
|
||||
)
|
||||
end) =~ "Instance.scrape_favicon(\"https://no-favicon.example.org/\") error: "
|
||||
end
|
||||
|
||||
test "Doesn't scrape unreachable instances" do
|
||||
test "Doesn't scrapes unreachable instances" do
|
||||
instance = insert(:instance, unreachable_since: Instances.reachability_datetime_threshold())
|
||||
url = "https://" <> instance.host
|
||||
|
||||
assert {:discard, :unreachable} == Instance.update_metadata(URI.parse(url))
|
||||
end
|
||||
|
||||
test "doesn't continue scraping nodeinfo if we can't find a link" do
|
||||
Tesla.Mock.mock(fn
|
||||
%{url: "https://bad-nodeinfo.example.org/"} ->
|
||||
%Tesla.Env{
|
||||
status: 200,
|
||||
body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
|
||||
}
|
||||
|
||||
%{url: "https://bad-nodeinfo.example.org/.well-known/nodeinfo"} ->
|
||||
%Tesla.Env{
|
||||
status: 200,
|
||||
body: "oepsie woepsie de nodeinfo is kapotie uwu"
|
||||
}
|
||||
end)
|
||||
|
||||
assert {:ok, true} ==
|
||||
Instance.update_metadata(URI.parse("https://bad-nodeinfo.example.org/"))
|
||||
|
||||
{:ok, instance} = Instance.get_cached_by_url("https://bad-nodeinfo.example.org/")
|
||||
assert instance.nodeinfo == nil
|
||||
end
|
||||
|
||||
test "doesn't store bad json in the nodeinfo" do
|
||||
Tesla.Mock.mock(fn
|
||||
%{url: "https://bad-nodeinfo.example.org/"} ->
|
||||
%Tesla.Env{
|
||||
status: 200,
|
||||
body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
|
||||
}
|
||||
|
||||
%{url: "https://bad-nodeinfo.example.org/.well-known/nodeinfo"} ->
|
||||
%Tesla.Env{
|
||||
status: 200,
|
||||
body:
|
||||
Jason.encode!(%{
|
||||
links: [
|
||||
%{
|
||||
rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
|
||||
href: "https://bad-nodeinfo.example.org/nodeinfo/2.0"
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
%{url: "https://bad-nodeinfo.example.org/nodeinfo/2.0"} ->
|
||||
%Tesla.Env{
|
||||
status: 200,
|
||||
body: "oepsie woepsie de json might be bad uwu"
|
||||
}
|
||||
end)
|
||||
|
||||
assert {:ok, true} ==
|
||||
Instance.update_metadata(URI.parse("https://bad-nodeinfo.example.org/"))
|
||||
|
||||
{:ok, instance} = Instance.get_cached_by_url("https://bad-nodeinfo.example.org/")
|
||||
assert instance.nodeinfo == nil
|
||||
end
|
||||
|
||||
test "doesn't store incredibly long json nodeinfo" do
|
||||
too_long = String.duplicate("a", 50_000)
|
||||
|
||||
Tesla.Mock.mock(fn
|
||||
%{url: "https://bad-nodeinfo.example.org/"} ->
|
||||
%Tesla.Env{
|
||||
status: 200,
|
||||
body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
|
||||
}
|
||||
|
||||
%{url: "https://bad-nodeinfo.example.org/.well-known/nodeinfo"} ->
|
||||
%Tesla.Env{
|
||||
status: 200,
|
||||
body:
|
||||
Jason.encode!(%{
|
||||
links: [
|
||||
%{
|
||||
rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
|
||||
href: "https://bad-nodeinfo.example.org/nodeinfo/2.0"
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
%{url: "https://bad-nodeinfo.example.org/nodeinfo/2.0"} ->
|
||||
%Tesla.Env{
|
||||
status: 200,
|
||||
body: Jason.encode!(%{version: "2.0", software: %{name: too_long}})
|
||||
}
|
||||
end)
|
||||
|
||||
assert {:ok, true} ==
|
||||
Instance.update_metadata(URI.parse("https://bad-nodeinfo.example.org/"))
|
||||
|
||||
{:ok, instance} = Instance.get_cached_by_url("https://bad-nodeinfo.example.org/")
|
||||
assert instance.nodeinfo == nil
|
||||
assert capture_log(fn -> assert nil == Instance.get_or_update_favicon(URI.parse(url)) end) =~
|
||||
"Instance.scrape_favicon(\"#{url}\") ignored unreachable host"
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -21,35 +21,6 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
|
|||
import Mock
|
||||
import Pleroma.Factory
|
||||
|
||||
describe "handle" do
|
||||
test "it queues a fetch of instance information" do
|
||||
author = insert(:user, local: false, ap_id: "https://wowee.example.com/users/1")
|
||||
recipient = insert(:user, local: true)
|
||||
|
||||
{:ok, note_data, _meta} =
|
||||
Builder.note(%Pleroma.Web.CommonAPI.ActivityDraft{
|
||||
user: author,
|
||||
to: [recipient.ap_id],
|
||||
mentions: [recipient],
|
||||
content_html: "hey",
|
||||
extra: %{"id" => "https://wowee.example.com/notes/1"}
|
||||
})
|
||||
|
||||
{:ok, create_activity_data, _meta} =
|
||||
Builder.create(author, note_data["id"], [recipient.ap_id])
|
||||
|
||||
{:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
|
||||
|
||||
{:ok, _create_activity, _meta} =
|
||||
SideEffects.handle(create_activity, local: false, object_data: note_data)
|
||||
|
||||
assert_enqueued(
|
||||
worker: Pleroma.Workers.NodeInfoFetcherWorker,
|
||||
args: %{"op" => "process", "source_url" => "https://wowee.example.com/users/1"}
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe "handle_after_transaction" do
|
||||
test "it streams out notifications and streams" do
|
||||
author = insert(:user, local: true)
|
||||
|
|
|
@ -25,7 +25,6 @@ test "Represent a user account" do
|
|||
|
||||
user =
|
||||
insert(:user, %{
|
||||
ap_id: "https://example.com/users/chikichikibanban",
|
||||
follower_count: 3,
|
||||
note_count: 5,
|
||||
background: background_image,
|
||||
|
@ -39,8 +38,6 @@ test "Represent a user account" do
|
|||
also_known_as: ["https://shitposter.zone/users/shp"]
|
||||
})
|
||||
|
||||
insert(:instance, %{host: "example.com", nodeinfo: %{version: "2.1"}})
|
||||
|
||||
expected = %{
|
||||
id: to_string(user.id),
|
||||
username: "shp",
|
||||
|
@ -53,15 +50,6 @@ test "Represent a user account" do
|
|||
statuses_count: 5,
|
||||
note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f '&<>"",
|
||||
url: user.ap_id,
|
||||
akkoma: %{
|
||||
instance: %{
|
||||
name: "example.com",
|
||||
nodeinfo: %{
|
||||
"version" => "2.1"
|
||||
},
|
||||
favicon: nil
|
||||
}
|
||||
},
|
||||
avatar: "http://localhost:4001/images/avi.png",
|
||||
avatar_static: "http://localhost:4001/images/avi.png",
|
||||
header: "http://localhost:4001/images/banner.png",
|
||||
|
@ -112,11 +100,7 @@ test "Represent a user account" do
|
|||
|
||||
describe "favicon" do
|
||||
setup do
|
||||
[
|
||||
user: insert(:user),
|
||||
instance:
|
||||
insert(:instance, %{host: "localhost", favicon: "https://example.com/favicon.ico"})
|
||||
]
|
||||
[user: insert(:user)]
|
||||
end
|
||||
|
||||
test "is parsed when :instance_favicons is enabled", %{user: user} do
|
||||
|
@ -124,14 +108,13 @@ test "is parsed when :instance_favicons is enabled", %{user: user} do
|
|||
|
||||
assert %{
|
||||
pleroma: %{
|
||||
favicon: "https://example.com/favicon.ico"
|
||||
favicon:
|
||||
"https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png"
|
||||
}
|
||||
} = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
|
||||
end
|
||||
|
||||
test "is nil when we have no instance", %{user: user} do
|
||||
user = %{user | ap_id: "https://wowee.example.com/users/2"}
|
||||
|
||||
test "is nil when :instances_favicons is disabled", %{user: user} do
|
||||
assert %{pleroma: %{favicon: nil}} =
|
||||
AccountView.render("show.json", %{user: user, skip_visibility_check: true})
|
||||
end
|
||||
|
@ -193,7 +176,6 @@ test "Represent a Service(bot) account" do
|
|||
},
|
||||
fqn: "shp@shitposter.club",
|
||||
last_status_at: nil,
|
||||
akkoma: %{instance: nil},
|
||||
pleroma: %{
|
||||
ap_id: user.ap_id,
|
||||
also_known_as: [],
|
||||
|
@ -596,8 +578,6 @@ test "uses mediaproxy urls when it's enabled (regardless of media preview proxy
|
|||
emoji: %{"joker_smile" => "https://evil.website/society.png"}
|
||||
)
|
||||
|
||||
insert(:instance, %{host: "localhost", favicon: "https://evil.website/favicon.png"})
|
||||
|
||||
with media_preview_enabled <- [false, true] do
|
||||
clear_config([:media_preview_proxy, :enabled], media_preview_enabled)
|
||||
|
||||
|
@ -606,9 +586,6 @@ test "uses mediaproxy urls when it's enabled (regardless of media preview proxy
|
|||
{key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
|
||||
String.starts_with?(url, Pleroma.Web.Endpoint.url())
|
||||
|
||||
{:akkoma, %{instance: %{favicon: favicon_url}}} ->
|
||||
String.starts_with?(favicon_url, Pleroma.Web.Endpoint.url())
|
||||
|
||||
{:emojis, emojis} ->
|
||||
Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
|
||||
String.starts_with?(url, Pleroma.Web.Endpoint.url()) &&
|
||||
|
@ -621,10 +598,4 @@ test "uses mediaproxy urls when it's enabled (regardless of media preview proxy
|
|||
|> assert()
|
||||
end
|
||||
end
|
||||
|
||||
test "returns nil in the instance field when no instance is held locally" do
|
||||
user = insert(:user)
|
||||
view = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
|
||||
assert view[:akkoma][:instance] == nil
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.StreamerTest do
|
||||
use Pleroma.DataCase, async: false
|
||||
use Pleroma.DataCase
|
||||
|
||||
import Pleroma.Factory
|
||||
|
||||
|
|
|
@ -36,15 +36,6 @@ def conversation_factory do
|
|||
}
|
||||
end
|
||||
|
||||
def instance_factory(attrs \\ %{}) do
|
||||
%Pleroma.Instances.Instance{
|
||||
host: attrs[:domain] || "example.com",
|
||||
nodeinfo: %{version: "2.0", openRegistrations: true},
|
||||
unreachable_since: nil
|
||||
}
|
||||
|> Map.merge(attrs)
|
||||
end
|
||||
|
||||
def user_factory(attrs \\ %{}) do
|
||||
pem = Enum.random(@rsa_keys)
|
||||
|
||||
|
@ -531,6 +522,13 @@ def oauth_app_factory do
|
|||
}
|
||||
end
|
||||
|
||||
def instance_factory do
|
||||
%Pleroma.Instances.Instance{
|
||||
host: "domain.com",
|
||||
unreachable_since: nil
|
||||
}
|
||||
end
|
||||
|
||||
def oauth_token_factory(attrs \\ %{}) do
|
||||
scopes = Map.get(attrs, :scopes, ["read"])
|
||||
oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
|
||||
|
|
Loading…
Reference in a new issue