2019-06-05 03:34:14 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-01 04:11:47 -07:00
|
|
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
2019-06-05 03:34:14 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.User.Search do
|
2020-09-25 01:12:48 -06:00
|
|
|
alias Pleroma.EctoType.ActivityPub.ObjectValidators.Uri, as: UriType
|
2019-07-11 07:55:31 -06:00
|
|
|
alias Pleroma.Pagination
|
2019-06-05 03:34:14 -06:00
|
|
|
alias Pleroma.User
|
2020-09-25 01:12:48 -06:00
|
|
|
|
2019-06-05 03:34:14 -06:00
|
|
|
import Ecto.Query
|
|
|
|
|
2019-06-14 05:39:57 -06:00
|
|
|
@limit 20
|
|
|
|
|
|
|
|
def search(query_string, opts \\ []) do
|
2019-06-05 03:34:14 -06:00
|
|
|
resolve = Keyword.get(opts, :resolve, false)
|
2019-06-14 05:39:57 -06:00
|
|
|
following = Keyword.get(opts, :following, false)
|
|
|
|
result_limit = Keyword.get(opts, :limit, @limit)
|
|
|
|
offset = Keyword.get(opts, :offset, 0)
|
|
|
|
|
2019-06-05 03:34:14 -06:00
|
|
|
for_user = Keyword.get(opts, :for_user)
|
|
|
|
|
2019-07-10 09:23:25 -06:00
|
|
|
query_string = format_query(query_string)
|
2019-06-05 03:34:14 -06:00
|
|
|
|
2020-09-24 07:54:55 -06:00
|
|
|
# If this returns anything, it should bounce to the top
|
|
|
|
maybe_resolved = maybe_resolve(resolve, for_user, query_string)
|
|
|
|
|
|
|
|
top_user_ids =
|
2020-09-25 00:46:14 -06:00
|
|
|
[]
|
|
|
|
|> maybe_add_resolved(maybe_resolved)
|
|
|
|
|> maybe_add_ap_id_match(query_string)
|
|
|
|
|> maybe_add_uri_match(query_string)
|
2019-06-05 03:34:14 -06:00
|
|
|
|
2019-10-16 03:52:47 -06:00
|
|
|
results =
|
|
|
|
query_string
|
2020-09-24 07:54:55 -06:00
|
|
|
|> search_query(for_user, following, top_user_ids)
|
2019-10-16 03:52:47 -06:00
|
|
|
|> Pagination.fetch_paginated(%{"offset" => offset, "limit" => result_limit}, :offset)
|
2019-06-05 03:34:14 -06:00
|
|
|
|
|
|
|
results
|
|
|
|
end
|
|
|
|
|
2020-09-25 00:46:14 -06:00
|
|
|
defp maybe_add_resolved(list, {:ok, %User{} = user}) do
|
|
|
|
[user.id | list]
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_add_resolved(list, _), do: list
|
|
|
|
|
|
|
|
defp maybe_add_ap_id_match(list, query) do
|
|
|
|
if user = User.get_cached_by_ap_id(query) do
|
|
|
|
[user.id | list]
|
|
|
|
else
|
|
|
|
list
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_add_uri_match(list, query) do
|
|
|
|
with {:ok, query} <- UriType.cast(query),
|
2020-09-30 06:14:41 -06:00
|
|
|
q = from(u in User, where: u.uri == ^query, select: u.id),
|
|
|
|
users = Pleroma.Repo.all(q) do
|
|
|
|
users ++ list
|
2020-09-25 00:46:14 -06:00
|
|
|
else
|
|
|
|
_ -> list
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-10 09:23:25 -06:00
|
|
|
defp format_query(query_string) do
|
|
|
|
# Strip the beginning @ off if there is a query
|
|
|
|
query_string = String.trim_leading(query_string, "@")
|
|
|
|
|
2020-02-25 07:34:56 -07:00
|
|
|
with [name, domain] <- String.split(query_string, "@") do
|
|
|
|
encoded_domain =
|
|
|
|
domain
|
|
|
|
|> String.replace(~r/[!-\-|@|[-`|{-~|\/|:|\s]+/, "")
|
|
|
|
|> String.to_charlist()
|
|
|
|
|> :idna.encode()
|
|
|
|
|> to_string()
|
|
|
|
|
|
|
|
name <> "@" <> encoded_domain
|
2019-07-10 09:23:25 -06:00
|
|
|
else
|
|
|
|
_ -> query_string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-24 07:54:55 -06:00
|
|
|
defp search_query(query_string, for_user, following, top_user_ids) do
|
2019-06-14 05:39:57 -06:00
|
|
|
for_user
|
|
|
|
|> base_query(following)
|
2019-07-04 22:22:08 -06:00
|
|
|
|> filter_blocked_user(for_user)
|
2019-11-04 22:55:41 -07:00
|
|
|
|> filter_invisible_users()
|
2020-07-03 02:02:15 -06:00
|
|
|
|> filter_internal_users()
|
2019-07-04 22:22:08 -06:00
|
|
|
|> filter_blocked_domains(for_user)
|
2019-10-16 04:49:33 -06:00
|
|
|
|> fts_search(query_string)
|
2020-09-24 07:54:55 -06:00
|
|
|
|> select_top_users(top_user_ids)
|
2019-10-16 04:49:33 -06:00
|
|
|
|> trigram_rank(query_string)
|
2020-09-24 07:54:55 -06:00
|
|
|
|> boost_search_rank(for_user, top_user_ids)
|
2019-06-05 03:34:14 -06:00
|
|
|
|> subquery()
|
|
|
|
|> order_by(desc: :search_rank)
|
|
|
|
|> maybe_restrict_local(for_user)
|
2022-09-15 14:38:35 -06:00
|
|
|
|> filter_deactivated_users()
|
2019-06-05 03:34:14 -06:00
|
|
|
end
|
|
|
|
|
2020-09-24 07:54:55 -06:00
|
|
|
defp select_top_users(query, top_user_ids) do
|
|
|
|
from(u in query,
|
|
|
|
or_where: u.id in ^top_user_ids
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-10-16 04:49:33 -06:00
|
|
|
defp fts_search(query, query_string) do
|
2019-10-16 03:52:47 -06:00
|
|
|
query_string = to_tsquery(query_string)
|
|
|
|
|
|
|
|
from(
|
|
|
|
u in query,
|
2019-10-16 04:49:33 -06:00
|
|
|
where:
|
|
|
|
fragment(
|
2020-07-08 13:50:15 -06:00
|
|
|
# The fragment must _exactly_ match `users_fts_index`, otherwise the index won't work
|
2019-10-16 04:49:33 -06:00
|
|
|
"""
|
2020-07-08 13:50:15 -06:00
|
|
|
(
|
|
|
|
setweight(to_tsvector('simple', regexp_replace(?, '\\W', ' ', 'g')), 'A') ||
|
|
|
|
setweight(to_tsvector('simple', regexp_replace(coalesce(?, ''), '\\W', ' ', 'g')), 'B')
|
|
|
|
) @@ to_tsquery('simple', ?)
|
2019-10-16 04:49:33 -06:00
|
|
|
""",
|
|
|
|
u.nickname,
|
2020-07-08 13:50:15 -06:00
|
|
|
u.name,
|
2019-10-16 04:49:33 -06:00
|
|
|
^query_string
|
|
|
|
)
|
2019-10-16 03:52:47 -06:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp to_tsquery(query_string) do
|
|
|
|
String.trim_trailing(query_string, "@" <> local_domain())
|
|
|
|
|> String.replace(~r/[!-\/|@|[-`|{-~|:-?]+/, " ")
|
|
|
|
|> String.trim()
|
|
|
|
|> String.split()
|
|
|
|
|> Enum.map(&(&1 <> ":*"))
|
|
|
|
|> Enum.join(" | ")
|
|
|
|
end
|
|
|
|
|
2020-07-08 04:14:18 -06:00
|
|
|
# Considers nickname match, localized nickname match, name match; preferences nickname match
|
2019-10-16 04:49:33 -06:00
|
|
|
defp trigram_rank(query, query_string) do
|
|
|
|
from(
|
|
|
|
u in query,
|
|
|
|
select_merge: %{
|
|
|
|
search_rank:
|
|
|
|
fragment(
|
2020-07-08 13:50:15 -06:00
|
|
|
"""
|
|
|
|
similarity(?, ?) +
|
|
|
|
similarity(?, regexp_replace(?, '@.+', '')) +
|
|
|
|
similarity(?, trim(coalesce(?, '')))
|
|
|
|
""",
|
2019-10-16 04:49:33 -06:00
|
|
|
^query_string,
|
|
|
|
u.nickname,
|
2020-07-08 04:14:18 -06:00
|
|
|
^query_string,
|
|
|
|
u.nickname,
|
|
|
|
^query_string,
|
2019-10-16 04:49:33 -06:00
|
|
|
u.name
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-09-02 12:11:24 -06:00
|
|
|
defp base_query(%User{} = user, true), do: User.get_friends_query(user)
|
|
|
|
defp base_query(_user, _following), do: User
|
2019-06-14 05:39:57 -06:00
|
|
|
|
2019-11-04 22:55:41 -07:00
|
|
|
defp filter_invisible_users(query) do
|
|
|
|
from(q in query, where: q.invisible == false)
|
|
|
|
end
|
|
|
|
|
2020-07-03 02:02:15 -06:00
|
|
|
defp filter_internal_users(query) do
|
|
|
|
from(q in query, where: q.actor_type != "Application")
|
2020-06-30 12:26:39 -06:00
|
|
|
end
|
|
|
|
|
2022-09-15 14:38:35 -06:00
|
|
|
defp filter_deactivated_users(query) do
|
|
|
|
from(q in query, where: q.is_active == true)
|
|
|
|
end
|
|
|
|
|
2019-11-10 06:30:21 -07:00
|
|
|
defp filter_blocked_user(query, %User{} = blocker) do
|
2019-11-11 04:16:57 -07:00
|
|
|
query
|
2019-11-18 10:38:56 -07:00
|
|
|
|> join(:left, [u], b in Pleroma.UserRelationship,
|
2019-11-11 04:16:57 -07:00
|
|
|
as: :blocks,
|
2019-11-18 10:38:56 -07:00
|
|
|
on: b.relationship_type == ^:block and b.source_id == ^blocker.id and u.id == b.target_id
|
2019-11-10 06:30:21 -07:00
|
|
|
)
|
2019-11-18 10:38:56 -07:00
|
|
|
|> where([blocks: b], is_nil(b.target_id))
|
2019-07-04 22:22:08 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
defp filter_blocked_user(query, _), do: query
|
|
|
|
|
2019-10-20 04:42:42 -06:00
|
|
|
defp filter_blocked_domains(query, %User{domain_blocks: domain_blocks})
|
2019-07-04 22:22:08 -06:00
|
|
|
when length(domain_blocks) > 0 do
|
|
|
|
domains = Enum.join(domain_blocks, ",")
|
|
|
|
|
|
|
|
from(
|
|
|
|
q in query,
|
|
|
|
where: fragment("substring(ap_id from '.*://([^/]*)') NOT IN (?)", ^domains)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp filter_blocked_domains(query, _), do: query
|
|
|
|
|
2019-06-11 08:25:53 -06:00
|
|
|
defp maybe_resolve(true, user, query) do
|
|
|
|
case {limit(), user} do
|
|
|
|
{:all, _} -> :noop
|
|
|
|
{:unauthenticated, %User{}} -> User.get_or_fetch(query)
|
|
|
|
{:unauthenticated, _} -> :noop
|
|
|
|
{false, _} -> User.get_or_fetch(query)
|
|
|
|
end
|
|
|
|
end
|
2019-06-06 06:17:49 -06:00
|
|
|
|
2019-06-11 08:25:53 -06:00
|
|
|
defp maybe_resolve(_, _, _), do: :noop
|
|
|
|
|
|
|
|
defp maybe_restrict_local(q, user) do
|
|
|
|
case {limit(), user} do
|
|
|
|
{:all, _} -> restrict_local(q)
|
|
|
|
{:unauthenticated, %User{}} -> q
|
|
|
|
{:unauthenticated, _} -> restrict_local(q)
|
|
|
|
{false, _} -> q
|
2019-06-06 06:17:49 -06:00
|
|
|
end
|
|
|
|
end
|
2019-06-05 03:34:14 -06:00
|
|
|
|
2019-06-11 08:25:53 -06:00
|
|
|
defp limit, do: Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
|
|
|
|
|
|
|
|
defp restrict_local(q), do: where(q, [u], u.local == true)
|
|
|
|
|
2019-10-16 03:52:47 -06:00
|
|
|
defp local_domain, do: Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
|
2019-06-05 03:34:14 -06:00
|
|
|
|
2020-09-24 07:54:55 -06:00
|
|
|
defp boost_search_rank(query, %User{} = for_user, top_user_ids) do
|
2019-06-05 03:34:14 -06:00
|
|
|
friends_ids = User.get_friends_ids(for_user)
|
|
|
|
followers_ids = User.get_followers_ids(for_user)
|
|
|
|
|
|
|
|
from(u in subquery(query),
|
|
|
|
select_merge: %{
|
|
|
|
search_rank:
|
|
|
|
fragment(
|
|
|
|
"""
|
2019-10-16 03:52:47 -06:00
|
|
|
CASE WHEN (?) THEN (?) * 1.5
|
|
|
|
WHEN (?) THEN (?) * 1.3
|
2019-06-05 03:34:14 -06:00
|
|
|
WHEN (?) THEN (?) * 1.1
|
2020-09-24 07:54:55 -06:00
|
|
|
WHEN (?) THEN 9001
|
2019-06-05 03:34:14 -06:00
|
|
|
ELSE (?) END
|
|
|
|
""",
|
|
|
|
u.id in ^friends_ids and u.id in ^followers_ids,
|
|
|
|
u.search_rank,
|
|
|
|
u.id in ^friends_ids,
|
|
|
|
u.search_rank,
|
|
|
|
u.id in ^followers_ids,
|
|
|
|
u.search_rank,
|
2020-09-24 07:54:55 -06:00
|
|
|
u.id in ^top_user_ids,
|
2019-06-05 03:34:14 -06:00
|
|
|
u.search_rank
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-09-24 07:54:55 -06:00
|
|
|
defp boost_search_rank(query, _for_user, top_user_ids) do
|
|
|
|
from(u in subquery(query),
|
|
|
|
select_merge: %{
|
|
|
|
search_rank:
|
|
|
|
fragment(
|
|
|
|
"""
|
|
|
|
CASE WHEN (?) THEN 9001
|
|
|
|
ELSE (?) END
|
|
|
|
""",
|
|
|
|
u.id in ^top_user_ids,
|
|
|
|
u.search_rank
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2019-06-05 03:34:14 -06:00
|
|
|
end
|