Support for searching local posts/users only

This commit is contained in:
itepechi 2023-11-15 08:45:53 +09:00
parent a1be54d706
commit 0bbcd4065f
8 changed files with 55 additions and 11 deletions

View file

@ -871,7 +871,8 @@
# these features are placed in the ':extensions' section for clarity # these features are placed in the ':extensions' section for clarity
# as they are not in the upstream, at least not yet # as they are not in the upstream, at least not yet
search_type_media: true, search_type_media: true,
search_option_following: true search_option_following: true,
search_option_local: true
} }
config :pleroma, Pleroma.Search.Meilisearch, config :pleroma, Pleroma.Search.Meilisearch,

View file

@ -31,6 +31,7 @@ def search(user, search_query, options \\ []) do
author = Keyword.get(options, :author) author = Keyword.get(options, :author)
following = can_search_following && Keyword.get(options, :following, false) following = can_search_following && Keyword.get(options, :following, false)
only_media = Keyword.get(options, :only_media, false) only_media = Keyword.get(options, :only_media, false)
only_local = Keyword.get(options, :local, false)
search_function = search_function =
if :persistent_term.get({Pleroma.Repo, :postgres_version}) >= 11 do if :persistent_term.get({Pleroma.Repo, :postgres_version}) >= 11 do
@ -45,7 +46,7 @@ def search(user, search_query, options \\ []) do
|> Activity.restrict_deactivated_users() |> Activity.restrict_deactivated_users()
|> restrict_public() |> restrict_public()
|> query_with(index_type, search_query, search_function) |> query_with(index_type, search_query, search_function)
|> maybe_restrict_local(user) |> maybe_restrict_local(user, only_local)
|> maybe_restrict_author(author) |> maybe_restrict_author(author)
|> maybe_restrict_blocked(user) |> maybe_restrict_blocked(user)
|> maybe_restrict_following(user, following) |> maybe_restrict_following(user, following)
@ -163,14 +164,15 @@ def query_with(q, :pgroonga, search_query, _) do
) )
end end
def maybe_restrict_local(q, user) do def maybe_restrict_local(q, user, only_local) do
limit = Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated) limit = Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
case {limit, user} do case {only_local, limit, user} do
{:all, _} -> restrict_local(q) {true, _, _} -> restrict_local(q)
{:unauthenticated, %User{}} -> q {_, :all, _} -> restrict_local(q)
{:unauthenticated, _} -> restrict_local(q) {_, :unauthenticated, %User{}} -> q
{false, _} -> q {_, :unauthenticated, _} -> restrict_local(q)
{_, false, _} -> q
end end
end end

View file

@ -55,6 +55,7 @@ def search(user, query, options) do
offset = Keyword.get(options, :offset, 0) offset = Keyword.get(options, :offset, 0)
following = can_search_following && Keyword.get(options, :following, false) following = can_search_following && Keyword.get(options, :following, false)
only_media = Keyword.get(options, :only_media, false) only_media = Keyword.get(options, :only_media, false)
only_local = Keyword.get(options, :local, false)
parsed_query = parsed_query =
query query
@ -86,6 +87,11 @@ def search(user, query, options) do
Enum.member?(User.following_ap_ids(user), x.object.data["actor"]) Enum.member?(User.following_ap_ids(user), x.object.data["actor"])
else else
true true
end,
if only_local do
x.local == true
else
true
end end
]) ])
end) end)

View file

@ -84,6 +84,7 @@ def search(user, query, options \\ []) do
author = Keyword.get(options, :author) author = Keyword.get(options, :author)
following = can_search_following && Keyword.get(options, :following, false) following = can_search_following && Keyword.get(options, :following, false)
only_media = Keyword.get(options, :only_media, false) only_media = Keyword.get(options, :only_media, false)
only_local = Keyword.get(options, :local, false)
res = res =
meili_post( meili_post(
@ -99,7 +100,7 @@ def search(user, query, options \\ []) do
|> Activity.create_by_object_ap_id() |> Activity.create_by_object_ap_id()
|> Activity.with_preloaded_object() |> Activity.with_preloaded_object()
|> Activity.restrict_deactivated_users() |> Activity.restrict_deactivated_users()
|> maybe_restrict_local(user) |> maybe_restrict_local(user, only_local)
|> maybe_restrict_author(author) |> maybe_restrict_author(author)
|> maybe_restrict_blocked(user) |> maybe_restrict_blocked(user)
|> maybe_restrict_following(user, following) |> maybe_restrict_following(user, following)

View file

@ -21,6 +21,7 @@ def search(query_string, opts \\ []) do
resolve = Keyword.get(opts, :resolve, false) resolve = Keyword.get(opts, :resolve, false)
following = Keyword.get(opts, :following, false) following = Keyword.get(opts, :following, false)
only_local = Keyword.get(opts, :local, false)
result_limit = Keyword.get(opts, :limit, @limit) result_limit = Keyword.get(opts, :limit, @limit)
offset = Keyword.get(opts, :offset, 0) offset = Keyword.get(opts, :offset, 0)
@ -39,7 +40,7 @@ def search(query_string, opts \\ []) do
results = results =
query_string query_string
|> search_query(query_type, for_user, following, top_user_ids) |> search_query(query_type, for_user, following, only_local, top_user_ids)
|> Pagination.fetch_paginated(%{"offset" => offset, "limit" => result_limit}, :offset) |> Pagination.fetch_paginated(%{"offset" => offset, "limit" => result_limit}, :offset)
results results
@ -92,13 +93,21 @@ defp format_query(query_string) do
end end
end end
defp search_query(query_string, query_type, for_user, following, top_user_ids) do defp search_query(
query_string,
query_type,
for_user,
following,
only_local,
top_user_ids
) do
for_user for_user
|> base_query(following) |> base_query(following)
|> filter_blocked_user(for_user) |> filter_blocked_user(for_user)
|> filter_invisible_users() |> filter_invisible_users()
|> filter_internal_users() |> filter_internal_users()
|> filter_blocked_domains(for_user) |> filter_blocked_domains(for_user)
|> filter_local_user(only_local)
|> query_with(query_type, query_string) |> query_with(query_type, query_string)
|> select_top_users(top_user_ids) |> select_top_users(top_user_ids)
|> trigram_rank(query_string) |> trigram_rank(query_string)
@ -233,6 +242,15 @@ defp filter_blocked_domains(query, %User{domain_blocks: domain_blocks})
defp filter_blocked_domains(query, _), do: query defp filter_blocked_domains(query, _), do: query
defp filter_local_user(query, true) do
from(
q in query,
where: q.local == true
)
end
defp filter_local_user(query, _), do: query
defp maybe_resolve(true, user, query) do defp maybe_resolve(true, user, query) do
case {limit(), user} do case {limit(), user} do
{:all, _} -> :noop {:all, _} -> :noop

View file

@ -46,6 +46,12 @@ def account_search_operation do
:query, :query,
%Schema{allOf: [BooleanLike], default: false}, %Schema{allOf: [BooleanLike], default: false},
"Only include accounts that the user is following" "Only include accounts that the user is following"
),
Operation.parameter(
:local,
:query,
%Schema{allOf: [BooleanLike], default: false},
"Only include local accounts"
) )
], ],
responses: %{ responses: %{
@ -93,6 +99,12 @@ def search2_operation do
%Schema{allOf: [BooleanLike], default: false}, %Schema{allOf: [BooleanLike], default: false},
"Only include accounts that the user is following" "Only include accounts that the user is following"
), ),
Operation.parameter(
:local,
:query,
%Schema{allOf: [BooleanLike], default: false},
"Only include local accounts"
),
with_relationships_param() | pagination_params() with_relationships_param() | pagination_params()
], ],
responses: %{ responses: %{

View file

@ -89,6 +89,7 @@ defp search_options(params, user) do
[ [
resolve: params[:resolve], resolve: params[:resolve],
following: params[:following], following: params[:following],
local: params[:local],
limit: min(params[:limit], @search_limit), limit: min(params[:limit], @search_limit),
offset: params[:offset], offset: params[:offset],
type: params[:type], type: params[:type],

View file

@ -95,6 +95,9 @@ def features do
if Config.get([Pleroma.Search, :extensions, :search_option_following], true) do if Config.get([Pleroma.Search, :extensions, :search_option_following], true) do
"bnakkoma:search_option_following" "bnakkoma:search_option_following"
end, end,
if Config.get([Pleroma.Search, :extensions, :search_option_local], true) do
"bnakkoma:search_option_local"
end,
"bnakkoma:opensearch_protocol", "bnakkoma:opensearch_protocol",
"custom_emoji_reactions", "custom_emoji_reactions",
"pleroma:get:main/ostatus" "pleroma:get:main/ostatus"