Merge pull request 'Search: fetch remote statuses only when resolving is allowed' (#8031) from lambadalambda/pleroma:fix/search-remote-fetch into develop

Reviewed-on: https://git.pleroma.social/pleroma/pleroma/pulls/8031
This commit is contained in:
lain 2026-10-31 16:20:39 +00:00
commit 240bc1be1f
7 changed files with 62 additions and 15 deletions

View file

@ -0,0 +1 @@
Status search only fetches remote URLs with resolve=true, on the first page, and for anonymous users only if limit_to_local_content allows it

View file

@ -45,7 +45,7 @@ defmodule Pleroma.Search.DatabaseSearch do
end end
# Outside the query transaction, as it may fetch over the network. # Outside the query transaction, as it may fetch over the network.
maybe_fetch(activities, user, search_query) maybe_fetch(activities, user, search_query, options)
end end
@doc """ @doc """
@ -160,7 +160,27 @@ defmodule Pleroma.Search.DatabaseSearch do
defp restrict_local(q), do: where(q, local: true) defp restrict_local(q), do: where(q, local: true)
def maybe_fetch(activities, user, search_query) do @doc """
Adds the status a URL query points to, fetching it if needed. As in Mastodon,
this only happens with `resolve: true` and on the first page, and for
anonymous users only if `[:instance, :limit_to_local_content]` is `false`.
"""
def maybe_fetch(activities, user, search_query, options) do
if fetch_allowed?(user, options),
do: fetch(activities, user, search_query),
else: activities
end
defp fetch_allowed?(user, options) do
options[:resolve] == true and (options[:offset] || 0) == 0 and
case Config.get([:instance, :limit_to_local_content], :unauthenticated) do
:all -> false
:unauthenticated -> match?(%User{}, user)
false -> true
end
end
defp fetch(activities, user, search_query) do
with true <- Regex.match?(~r/https?:/, search_query), with true <- Regex.match?(~r/https?:/, search_query),
{:ok, object} <- Fetcher.fetch_object_from_id(search_query), {:ok, object} <- Fetcher.fetch_object_from_id(search_query),
%Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]), %Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),

View file

@ -111,11 +111,11 @@ defmodule Pleroma.Search.Meilisearch do
|> maybe_restrict_local(user) |> maybe_restrict_local(user)
|> maybe_restrict_author(author) |> maybe_restrict_author(author)
|> maybe_restrict_blocked(user) |> maybe_restrict_blocked(user)
|> maybe_fetch(user, query) |> maybe_fetch(user, query, options)
|> order_by([object: obj], desc: obj.data["published"]) |> order_by([object: obj], desc: obj.data["published"])
|> Pleroma.Repo.all() |> Pleroma.Repo.all()
rescue rescue
_ -> maybe_fetch([], user, query) _ -> maybe_fetch([], user, query, options)
end end
end end
end end

View file

@ -12,7 +12,7 @@ defmodule Pleroma.Search.ParadeDB do
import Pleroma.Search.DatabaseSearch, import Pleroma.Search.DatabaseSearch,
only: [ only: [
maybe_fetch: 3, maybe_fetch: 4,
maybe_restrict_author: 2, maybe_restrict_author: 2,
maybe_restrict_blocked: 2, maybe_restrict_blocked: 2,
maybe_restrict_local: 2 maybe_restrict_local: 2
@ -149,19 +149,19 @@ defmodule Pleroma.Search.ParadeDB do
case search_ids(query, limit, offset, author) do case search_ids(query, limit, offset, author) do
{:ok, []} -> {:ok, []} ->
maybe_fetch([], user, query) maybe_fetch([], user, query, options)
{:ok, ids} -> {:ok, ids} ->
ids ids
|> fetch_activities(user, query, author) |> fetch_activities(user, query, author, options)
{:error, error} -> {:error, error} ->
Logger.error("ParadeDB search failed: #{inspect(error)}") Logger.error("ParadeDB search failed: #{inspect(error)}")
maybe_fetch([], user, query) maybe_fetch([], user, query, options)
end end
end end
defp fetch_activities(ids, user, query, author) do defp fetch_activities(ids, user, query, author, options) do
dumped_ids = dumped_ids =
ids ids
|> Enum.map(&dump_activity_id/1) |> Enum.map(&dump_activity_id/1)
@ -183,7 +183,7 @@ defmodule Pleroma.Search.ParadeDB do
) )
) )
|> Pleroma.Repo.all() |> Pleroma.Repo.all()
|> maybe_fetch(user, query) |> maybe_fetch(user, query, options)
end end
defp maybe_restrict_public(query, %Pleroma.User{}) do defp maybe_restrict_public(query, %Pleroma.User{}) do

View file

@ -10,7 +10,7 @@ defmodule Pleroma.Search.QdrantSearch do
alias __MODULE__.OpenAIClient alias __MODULE__.OpenAIClient
alias __MODULE__.QdrantClient alias __MODULE__.QdrantClient
import Pleroma.Search.DatabaseSearch, only: [maybe_fetch: 3] import Pleroma.Search.DatabaseSearch, only: [maybe_fetch: 4]
@impl true @impl true
def create_index do def create_index do
@ -131,7 +131,7 @@ defmodule Pleroma.Search.QdrantSearch do
|> Activity.restrict_deactivated_users() |> Activity.restrict_deactivated_users()
|> Ecto.Query.order_by([a], fragment("array_position(?, ?)", ^ids, a.id)) |> Ecto.Query.order_by([a], fragment("array_position(?, ?)", ^ids, a.id))
|> Pleroma.Repo.all() |> Pleroma.Repo.all()
|> maybe_fetch(user, original_query) |> maybe_fetch(user, original_query, options)
else else
_ -> _ ->
[] []

View file

@ -328,7 +328,9 @@ defmodule Pleroma.Web.MastodonAPI.SearchControllerTest do
assert status["id"] == to_string(activity.id) assert status["id"] == to_string(activity.id)
end end
test "search fetches remote statuses and prefers them over other results", %{conn: conn} do test "search fetches remote statuses and prefers them over other results" do
%{conn: conn} = oauth_access(["read:search"])
{:ok, %{id: activity_id}} = {:ok, %{id: activity_id}} =
CommonAPI.post(insert(:user), %{ CommonAPI.post(insert(:user), %{
status: "check out http://mastodon.example.org/@admin/99541947525187367" status: "check out http://mastodon.example.org/@admin/99541947525187367"
@ -336,7 +338,9 @@ defmodule Pleroma.Web.MastodonAPI.SearchControllerTest do
%{"url" => result_url, "id" => result_id} = %{"url" => result_url, "id" => result_id} =
conn conn
|> get("/api/v1/search?q=http://mastodon.example.org/@admin/99541947525187367") |> get(
"/api/v1/search?resolve=true&q=http://mastodon.example.org/@admin/99541947525187367"
)
|> json_response_and_validate_schema(200) |> json_response_and_validate_schema(200)
|> Map.get("statuses") |> Map.get("statuses")
|> List.first() |> List.first()
@ -345,6 +349,28 @@ defmodule Pleroma.Web.MastodonAPI.SearchControllerTest do
assert match?(^result_url, "http://mastodon.example.org/@admin/99541947525187367") assert match?(^result_url, "http://mastodon.example.org/@admin/99541947525187367")
end end
test "search fetches remote statuses only with resolve, when logged in, on the first page",
%{conn: anonymous} do
%{conn: conn} = oauth_access(["read:search"])
url = "http://mastodon.example.org/@admin/99541947525187367"
for {conn, params} <- [
{conn, ""},
{conn, "&resolve=false"},
{conn, "&resolve=true&offset=5"},
{anonymous, "&resolve=true"}
] do
assert %{"statuses" => []} =
conn
|> get("/api/v1/search?q=#{url}#{params}")
|> json_response_and_validate_schema(200)
end
refute Pleroma.Object.get_by_ap_id(
"http://mastodon.example.org/users/admin/statuses/99541947525187367"
)
end
test "search doesn't show statuses that it shouldn't", %{conn: conn} do test "search doesn't show statuses that it shouldn't", %{conn: conn} do
{:ok, activity} = {:ok, activity} =
CommonAPI.post(insert(:user), %{ CommonAPI.post(insert(:user), %{

View file

@ -788,7 +788,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
test "put the url advertised in the Activity in to the url attribute" do test "put the url advertised in the Activity in to the url attribute" do
id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810" id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810"
[activity] = Activity.search(nil, id) [activity] = Activity.search(insert(:user), id, resolve: true)
status = StatusView.render("show.json", %{activity: activity}) status = StatusView.render("show.json", %{activity: activity})