2019-06-05 11:34:14 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.User.Search do
|
2019-07-11 15:55:31 +02:00
|
|
|
alias Pleroma.Pagination
|
2019-06-05 11:34:14 +02:00
|
|
|
alias Pleroma.User
|
|
|
|
import Ecto.Query
|
|
|
|
|
2019-06-14 13:39:57 +02:00
|
|
|
@limit 20
|
|
|
|
|
|
|
|
def search(query_string, opts \\ []) do
|
2019-06-05 11:34:14 +02:00
|
|
|
resolve = Keyword.get(opts, :resolve, false)
|
2019-06-14 13:39:57 +02:00
|
|
|
following = Keyword.get(opts, :following, false)
|
|
|
|
result_limit = Keyword.get(opts, :limit, @limit)
|
|
|
|
offset = Keyword.get(opts, :offset, 0)
|
|
|
|
|
2019-06-05 11:34:14 +02:00
|
|
|
for_user = Keyword.get(opts, :for_user)
|
|
|
|
|
2019-07-10 17:23:25 +02:00
|
|
|
query_string = format_query(query_string)
|
2019-06-05 11:34:14 +02:00
|
|
|
|
2019-06-14 13:39:57 +02:00
|
|
|
maybe_resolve(resolve, for_user, query_string)
|
2019-06-05 11:34:14 +02:00
|
|
|
|
2019-10-16 11:52:47 +02:00
|
|
|
results =
|
|
|
|
query_string
|
|
|
|
|> search_query(for_user, following)
|
|
|
|
|> Pagination.fetch_paginated(%{"offset" => offset, "limit" => result_limit}, :offset)
|
2019-06-05 11:34:14 +02:00
|
|
|
|
|
|
|
results
|
|
|
|
end
|
|
|
|
|
2019-07-10 17:23:25 +02:00
|
|
|
defp format_query(query_string) do
|
|
|
|
# Strip the beginning @ off if there is a query
|
|
|
|
query_string = String.trim_leading(query_string, "@")
|
|
|
|
|
|
|
|
with [name, domain] <- String.split(query_string, "@"),
|
2019-08-03 20:13:20 +02:00
|
|
|
formatted_domain <- String.replace(domain, ~r/[!-\-|@|[-`|{-~|\/|:|\s]+/, "") do
|
2019-07-10 17:23:25 +02:00
|
|
|
name <> "@" <> to_string(:idna.encode(formatted_domain))
|
|
|
|
else
|
|
|
|
_ -> query_string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-14 13:39:57 +02:00
|
|
|
defp search_query(query_string, for_user, following) do
|
|
|
|
for_user
|
|
|
|
|> base_query(following)
|
2019-07-05 06:22:08 +02:00
|
|
|
|> filter_blocked_user(for_user)
|
|
|
|
|> filter_blocked_domains(for_user)
|
2019-10-16 12:49:33 +02:00
|
|
|
|> fts_search(query_string)
|
|
|
|
|> trigram_rank(query_string)
|
2019-10-16 11:52:47 +02:00
|
|
|
|> boost_search_rank(for_user)
|
2019-06-05 11:34:14 +02:00
|
|
|
|> subquery()
|
|
|
|
|> order_by(desc: :search_rank)
|
|
|
|
|> maybe_restrict_local(for_user)
|
|
|
|
end
|
|
|
|
|
2019-10-16 11:52:47 +02:00
|
|
|
@nickname_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~\-@]+$/
|
2019-10-16 12:49:33 +02:00
|
|
|
defp fts_search(query, query_string) do
|
2019-10-16 11:52:47 +02:00
|
|
|
{nickname_weight, name_weight} =
|
|
|
|
if String.match?(query_string, @nickname_regex) do
|
|
|
|
{"A", "B"}
|
|
|
|
else
|
|
|
|
{"B", "A"}
|
|
|
|
end
|
|
|
|
|
|
|
|
query_string = to_tsquery(query_string)
|
|
|
|
|
|
|
|
from(
|
|
|
|
u in query,
|
2019-10-16 12:49:33 +02:00
|
|
|
where:
|
|
|
|
fragment(
|
|
|
|
"""
|
|
|
|
(setweight(to_tsvector('simple', ?), ?) || setweight(to_tsvector('simple', ?), ?)) @@ to_tsquery('simple', ?)
|
|
|
|
""",
|
|
|
|
u.name,
|
|
|
|
^name_weight,
|
|
|
|
u.nickname,
|
|
|
|
^nickname_weight,
|
|
|
|
^query_string
|
|
|
|
)
|
2019-10-16 11:52:47 +02: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
|
|
|
|
|
2019-10-16 12:49:33 +02:00
|
|
|
defp trigram_rank(query, query_string) do
|
|
|
|
from(
|
|
|
|
u in query,
|
|
|
|
select_merge: %{
|
|
|
|
search_rank:
|
|
|
|
fragment(
|
|
|
|
"similarity(?, trim(? || ' ' || coalesce(?, '')))",
|
|
|
|
^query_string,
|
|
|
|
u.nickname,
|
|
|
|
u.name
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-06-14 13:39:57 +02:00
|
|
|
defp base_query(_user, false), do: User
|
|
|
|
defp base_query(user, true), do: User.get_followers_query(user)
|
|
|
|
|
2019-10-20 12:42:42 +02:00
|
|
|
defp filter_blocked_user(query, %User{blocks: blocks})
|
2019-07-05 06:22:08 +02:00
|
|
|
when length(blocks) > 0 do
|
|
|
|
from(q in query, where: not (q.ap_id in ^blocks))
|
|
|
|
end
|
|
|
|
|
|
|
|
defp filter_blocked_user(query, _), do: query
|
|
|
|
|
2019-10-20 12:42:42 +02:00
|
|
|
defp filter_blocked_domains(query, %User{domain_blocks: domain_blocks})
|
2019-07-05 06:22:08 +02: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 16:25:53 +02: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 14:17:49 +02:00
|
|
|
|
2019-06-11 16:25:53 +02: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 14:17:49 +02:00
|
|
|
end
|
|
|
|
end
|
2019-06-05 11:34:14 +02:00
|
|
|
|
2019-06-11 16:25:53 +02: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 11:52:47 +02:00
|
|
|
defp local_domain, do: Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
|
2019-06-05 11:34:14 +02:00
|
|
|
|
2019-10-16 11:52:47 +02:00
|
|
|
defp boost_search_rank(query, %User{} = for_user) do
|
2019-06-05 11:34:14 +02: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 11:52:47 +02:00
|
|
|
CASE WHEN (?) THEN (?) * 1.5
|
|
|
|
WHEN (?) THEN (?) * 1.3
|
2019-06-05 11:34:14 +02:00
|
|
|
WHEN (?) THEN (?) * 1.1
|
|
|
|
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,
|
|
|
|
u.search_rank
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-10-16 11:52:47 +02:00
|
|
|
defp boost_search_rank(query, _for_user), do: query
|
2019-06-05 11:34:14 +02:00
|
|
|
end
|