Support for searching local posts/users only
This commit is contained in:
parent
a1be54d706
commit
0bbcd4065f
8 changed files with 55 additions and 11 deletions
|
@ -871,7 +871,8 @@
|
|||
# these features are placed in the ':extensions' section for clarity
|
||||
# as they are not in the upstream, at least not yet
|
||||
search_type_media: true,
|
||||
search_option_following: true
|
||||
search_option_following: true,
|
||||
search_option_local: true
|
||||
}
|
||||
|
||||
config :pleroma, Pleroma.Search.Meilisearch,
|
||||
|
|
|
@ -31,6 +31,7 @@ def search(user, search_query, options \\ []) do
|
|||
author = Keyword.get(options, :author)
|
||||
following = can_search_following && Keyword.get(options, :following, false)
|
||||
only_media = Keyword.get(options, :only_media, false)
|
||||
only_local = Keyword.get(options, :local, false)
|
||||
|
||||
search_function =
|
||||
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()
|
||||
|> restrict_public()
|
||||
|> query_with(index_type, search_query, search_function)
|
||||
|> maybe_restrict_local(user)
|
||||
|> maybe_restrict_local(user, only_local)
|
||||
|> maybe_restrict_author(author)
|
||||
|> maybe_restrict_blocked(user)
|
||||
|> maybe_restrict_following(user, following)
|
||||
|
@ -163,14 +164,15 @@ def query_with(q, :pgroonga, search_query, _) do
|
|||
)
|
||||
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)
|
||||
|
||||
case {limit, user} do
|
||||
{:all, _} -> restrict_local(q)
|
||||
{:unauthenticated, %User{}} -> q
|
||||
{:unauthenticated, _} -> restrict_local(q)
|
||||
{false, _} -> q
|
||||
case {only_local, limit, user} do
|
||||
{true, _, _} -> restrict_local(q)
|
||||
{_, :all, _} -> restrict_local(q)
|
||||
{_, :unauthenticated, %User{}} -> q
|
||||
{_, :unauthenticated, _} -> restrict_local(q)
|
||||
{_, false, _} -> q
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -55,6 +55,7 @@ def search(user, query, options) do
|
|||
offset = Keyword.get(options, :offset, 0)
|
||||
following = can_search_following && Keyword.get(options, :following, false)
|
||||
only_media = Keyword.get(options, :only_media, false)
|
||||
only_local = Keyword.get(options, :local, false)
|
||||
|
||||
parsed_query =
|
||||
query
|
||||
|
@ -86,6 +87,11 @@ def search(user, query, options) do
|
|||
Enum.member?(User.following_ap_ids(user), x.object.data["actor"])
|
||||
else
|
||||
true
|
||||
end,
|
||||
if only_local do
|
||||
x.local == true
|
||||
else
|
||||
true
|
||||
end
|
||||
])
|
||||
end)
|
||||
|
|
|
@ -84,6 +84,7 @@ def search(user, query, options \\ []) do
|
|||
author = Keyword.get(options, :author)
|
||||
following = can_search_following && Keyword.get(options, :following, false)
|
||||
only_media = Keyword.get(options, :only_media, false)
|
||||
only_local = Keyword.get(options, :local, false)
|
||||
|
||||
res =
|
||||
meili_post(
|
||||
|
@ -99,7 +100,7 @@ def search(user, query, options \\ []) do
|
|||
|> Activity.create_by_object_ap_id()
|
||||
|> Activity.with_preloaded_object()
|
||||
|> Activity.restrict_deactivated_users()
|
||||
|> maybe_restrict_local(user)
|
||||
|> maybe_restrict_local(user, only_local)
|
||||
|> maybe_restrict_author(author)
|
||||
|> maybe_restrict_blocked(user)
|
||||
|> maybe_restrict_following(user, following)
|
||||
|
|
|
@ -21,6 +21,7 @@ def search(query_string, opts \\ []) do
|
|||
|
||||
resolve = Keyword.get(opts, :resolve, false)
|
||||
following = Keyword.get(opts, :following, false)
|
||||
only_local = Keyword.get(opts, :local, false)
|
||||
result_limit = Keyword.get(opts, :limit, @limit)
|
||||
offset = Keyword.get(opts, :offset, 0)
|
||||
|
||||
|
@ -39,7 +40,7 @@ def search(query_string, opts \\ []) do
|
|||
|
||||
results =
|
||||
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)
|
||||
|
||||
results
|
||||
|
@ -92,13 +93,21 @@ defp format_query(query_string) do
|
|||
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
|
||||
|> base_query(following)
|
||||
|> filter_blocked_user(for_user)
|
||||
|> filter_invisible_users()
|
||||
|> filter_internal_users()
|
||||
|> filter_blocked_domains(for_user)
|
||||
|> filter_local_user(only_local)
|
||||
|> query_with(query_type, query_string)
|
||||
|> select_top_users(top_user_ids)
|
||||
|> 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_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
|
||||
case {limit(), user} do
|
||||
{:all, _} -> :noop
|
||||
|
|
|
@ -46,6 +46,12 @@ def account_search_operation do
|
|||
:query,
|
||||
%Schema{allOf: [BooleanLike], default: false},
|
||||
"Only include accounts that the user is following"
|
||||
),
|
||||
Operation.parameter(
|
||||
:local,
|
||||
:query,
|
||||
%Schema{allOf: [BooleanLike], default: false},
|
||||
"Only include local accounts"
|
||||
)
|
||||
],
|
||||
responses: %{
|
||||
|
@ -93,6 +99,12 @@ def search2_operation do
|
|||
%Schema{allOf: [BooleanLike], default: false},
|
||||
"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()
|
||||
],
|
||||
responses: %{
|
||||
|
|
|
@ -89,6 +89,7 @@ defp search_options(params, user) do
|
|||
[
|
||||
resolve: params[:resolve],
|
||||
following: params[:following],
|
||||
local: params[:local],
|
||||
limit: min(params[:limit], @search_limit),
|
||||
offset: params[:offset],
|
||||
type: params[:type],
|
||||
|
|
|
@ -95,6 +95,9 @@ def features do
|
|||
if Config.get([Pleroma.Search, :extensions, :search_option_following], true) do
|
||||
"bnakkoma:search_option_following"
|
||||
end,
|
||||
if Config.get([Pleroma.Search, :extensions, :search_option_local], true) do
|
||||
"bnakkoma:search_option_local"
|
||||
end,
|
||||
"bnakkoma:opensearch_protocol",
|
||||
"custom_emoji_reactions",
|
||||
"pleroma:get:main/ostatus"
|
||||
|
|
Loading…
Reference in a new issue