1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.User.Search do
6 alias Pleroma.Pagination
12 def search(query_string, opts \\ []) do
13 resolve = Keyword.get(opts, :resolve, false)
14 following = Keyword.get(opts, :following, false)
15 result_limit = Keyword.get(opts, :limit, @limit)
16 offset = Keyword.get(opts, :offset, 0)
18 for_user = Keyword.get(opts, :for_user)
20 query_string = format_query(query_string)
22 # If this returns anything, it should bounce to the top
23 maybe_resolved = maybe_resolve(resolve, for_user, query_string)
24 maybe_ap_id_match = User.get_cached_by_ap_id(query_string)
27 case {maybe_resolved, maybe_ap_id_match} do
28 {{:ok, %User{} = user}, %User{} = other_user} -> [user.id, other_user.id]
29 {{:ok, %User{} = user}, _} -> [user.id]
30 {_, %User{} = user} -> [user.id]
36 |> search_query(for_user, following, top_user_ids)
37 |> Pagination.fetch_paginated(%{"offset" => offset, "limit" => result_limit}, :offset)
42 defp format_query(query_string) do
43 # Strip the beginning @ off if there is a query
44 query_string = String.trim_leading(query_string, "@")
46 with [name, domain] <- String.split(query_string, "@") do
49 |> String.replace(~r/[!-\-|@|[-`|{-~|\/|:|\s]+/, "")
50 |> String.to_charlist()
54 name <> "@" <> encoded_domain
60 defp search_query(query_string, for_user, following, top_user_ids) do
62 |> base_query(following)
63 |> filter_blocked_user(for_user)
64 |> filter_invisible_users()
65 |> filter_discoverable_users()
66 |> filter_internal_users()
67 |> filter_blocked_domains(for_user)
68 |> fts_search(query_string)
69 |> select_top_users(top_user_ids)
70 |> trigram_rank(query_string)
71 |> boost_search_rank(for_user, top_user_ids)
73 |> order_by(desc: :search_rank)
74 |> maybe_restrict_local(for_user)
77 defp select_top_users(query, top_user_ids) do
79 or_where: u.id in ^top_user_ids
83 defp fts_search(query, query_string) do
84 query_string = to_tsquery(query_string)
90 # The fragment must _exactly_ match `users_fts_index`, otherwise the index won't work
93 setweight(to_tsvector('simple', regexp_replace(?, '\\W', ' ', 'g')), 'A') ||
94 setweight(to_tsvector('simple', regexp_replace(coalesce(?, ''), '\\W', ' ', 'g')), 'B')
95 ) @@ to_tsquery('simple', ?)
104 defp to_tsquery(query_string) do
105 String.trim_trailing(query_string, "@" <> local_domain())
106 |> String.replace(~r/[!-\/|@|[-`|{-~|:-?]+/, " ")
109 |> Enum.map(&(&1 <> ":*"))
113 # Considers nickname match, localized nickname match, name match; preferences nickname match
114 defp trigram_rank(query, query_string) do
122 similarity(?, regexp_replace(?, '@.+', '')) +
123 similarity(?, trim(coalesce(?, '')))
136 defp base_query(%User{} = user, true), do: User.get_friends_query(user)
137 defp base_query(_user, _following), do: User
139 defp filter_invisible_users(query) do
140 from(q in query, where: q.invisible == false)
143 defp filter_discoverable_users(query) do
144 from(q in query, where: q.discoverable == true)
147 defp filter_internal_users(query) do
148 from(q in query, where: q.actor_type != "Application")
151 defp filter_blocked_user(query, %User{} = blocker) do
153 |> join(:left, [u], b in Pleroma.UserRelationship,
155 on: b.relationship_type == ^:block and b.source_id == ^blocker.id and u.id == b.target_id
157 |> where([blocks: b], is_nil(b.target_id))
160 defp filter_blocked_user(query, _), do: query
162 defp filter_blocked_domains(query, %User{domain_blocks: domain_blocks})
163 when length(domain_blocks) > 0 do
164 domains = Enum.join(domain_blocks, ",")
168 where: fragment("substring(ap_id from '.*://([^/]*)') NOT IN (?)", ^domains)
172 defp filter_blocked_domains(query, _), do: query
174 defp maybe_resolve(true, user, query) do
175 case {limit(), user} do
177 {:unauthenticated, %User{}} -> User.get_or_fetch(query)
178 {:unauthenticated, _} -> :noop
179 {false, _} -> User.get_or_fetch(query)
183 defp maybe_resolve(_, _, _), do: :noop
185 defp maybe_restrict_local(q, user) do
186 case {limit(), user} do
187 {:all, _} -> restrict_local(q)
188 {:unauthenticated, %User{}} -> q
189 {:unauthenticated, _} -> restrict_local(q)
194 defp limit, do: Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
196 defp restrict_local(q), do: where(q, [u], u.local == true)
198 defp local_domain, do: Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
200 defp boost_search_rank(query, %User{} = for_user, top_user_ids) do
201 friends_ids = User.get_friends_ids(for_user)
202 followers_ids = User.get_followers_ids(for_user)
204 from(u in subquery(query),
209 CASE WHEN (?) THEN (?) * 1.5
210 WHEN (?) THEN (?) * 1.3
211 WHEN (?) THEN (?) * 1.1
215 u.id in ^friends_ids and u.id in ^followers_ids,
217 u.id in ^friends_ids,
219 u.id in ^followers_ids,
221 u.id in ^top_user_ids,
228 defp boost_search_rank(query, _for_user, top_user_ids) do
229 from(u in subquery(query),
234 CASE WHEN (?) THEN 9001
237 u.id in ^top_user_ids,