feat(instances): record the instance of new remote users

The instances table only got rows from fetches, reachability changes
and favicon lookups, so most hosts we know users from had none. Insert
a row when a remote user is created, keeping existing rows.
This commit is contained in:
Lain Soykaf 2026-10-27 20:48:02 +04:00
commit 8e3404a239
No known key found for this signature in database
3 changed files with 32 additions and 4 deletions

View file

@ -91,6 +91,13 @@ defmodule Pleroma.Instances.Instance do
def reachable?(url_or_host) when is_binary(url_or_host), do: true
@doc "Creates a record for the instance unless one exists. Existing records are kept."
def ensure_exists(url_or_host) when is_binary(url_or_host) do
%Instance{host: host(url_or_host)}
|> changeset(%{})
|> Repo.insert(on_conflict: :nothing, conflict_target: :host)
end
def set_reachable(url_or_host) when is_binary(url_or_host) do
host = host(url_or_host)

View file

@ -11,6 +11,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
alias Pleroma.Conversation.Participation
alias Pleroma.Filter
alias Pleroma.Hashtag
alias Pleroma.Instances.Instance
alias Pleroma.Maps
alias Pleroma.Notification
alias Pleroma.Object
@ -2068,10 +2069,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
else
maybe_handle_clashing_nickname(data)
data
|> User.remote_user_changeset()
|> Repo.insert()
|> User.set_cache()
with {:ok, user} <- data |> User.remote_user_changeset() |> Repo.insert() do
# Keeps the instances table complete, so it can list our peers.
Instance.ensure_exists(user.ap_id)
User.set_cache({:ok, user})
end
end
end
end

View file

@ -184,6 +184,25 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
end
test "it records the user's instance" do
refute Repo.get_by(Pleroma.Instances.Instance, host: "mastodon.example.org")
{:ok, _user} = ActivityPub.make_user_from_ap_id("http://mastodon.example.org/users/admin")
assert %{unreachable_since: nil} =
Repo.get_by(Pleroma.Instances.Instance, host: "mastodon.example.org")
end
test "it keeps an existing instance record" do
unreachable_since = ~N[2020-01-01 00:00:00.000000]
insert(:instance, host: "mastodon.example.org", unreachable_since: unreachable_since)
{:ok, _user} = ActivityPub.make_user_from_ap_id("http://mastodon.example.org/users/admin")
assert %{unreachable_since: ^unreachable_since} =
Repo.get_by(Pleroma.Instances.Instance, host: "mastodon.example.org")
end
test "it returns a user that is invisible" do
user_id = "http://mastodon.example.org/users/relay"
{:ok, user} = ActivityPub.make_user_from_ap_id(user_id)