fix(mastodon-api): optimize account status restrictions

This commit is contained in:
Lain Soykaf 2026-07-28 16:01:48 +04:00
commit 4a47a756be
No known key found for this signature in database
2 changed files with 93 additions and 27 deletions

View file

@ -710,6 +710,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|> Map.put(:type, ["Create", "Announce"])
|> Map.put(:user, reading_user)
|> Map.put(:actor_id, user.ap_id)
|> Map.put(:actor_local, user.local)
|> Map.put(:pinned_object_ids, Map.keys(user.pinned_objects))
params =
@ -1357,41 +1358,48 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp restrict_unauthenticated(query, _), do: query
defp restrict_unauthenticated_object(query, nil) do
local = Config.restrict_unauthenticated_access?(:activities, :local)
remote = Config.restrict_unauthenticated_access?(:activities, :remote)
local_object_pattern = Pleroma.Web.Endpoint.url() <> "/%"
defp restrict_activity_and_object_origin(query, local?) do
local_object_prefix = Pleroma.Web.Endpoint.url() <> "/"
# Keeping the correlated origin checks inside CASE prevents PostgreSQL from choosing a
# global bitmap-and-sort plan before applying the account statuses limit.
from([activity, object: object] in query,
where:
fragment(
"CASE WHEN ? = ? AND starts_with((?->>'id'), ?) = ? THEN true ELSE false END",
activity.local,
^local?,
object.data,
^local_object_prefix,
^local?
)
)
end
defp maybe_restrict_unauthenticated(
query,
%{restrict_unauthenticated: true, user: nil, actor_local: actor_local}
)
when is_boolean(actor_local) do
local_restricted = Config.restrict_unauthenticated_access?(:activities, :local)
remote_restricted = Config.restrict_unauthenticated_access?(:activities, :remote)
actor_restricted = if actor_local, do: local_restricted, else: remote_restricted
cond do
local and remote ->
from([object: object] in query, where: false)
actor_restricted ->
from(activity in query, where: false)
local ->
from([object: object] in query,
where: not fragment("(?->>'id') LIKE ?", object.data, ^local_object_pattern)
)
local_restricted ->
restrict_activity_and_object_origin(query, false)
remote ->
from([object: object] in query,
where: fragment("(?->>'id') LIKE ?", object.data, ^local_object_pattern)
)
remote_restricted ->
restrict_activity_and_object_origin(query, true)
true ->
query
end
end
defp restrict_unauthenticated_object(query, _), do: query
defp maybe_restrict_unauthenticated(
query,
%{restrict_unauthenticated: true, user: user}
) do
query
|> restrict_unauthenticated(user)
|> restrict_unauthenticated_object(user)
end
defp maybe_restrict_unauthenticated(query, _), do: query
defp restrict_quote_url(query, %{quote_url: quote_url}) do