408295e0ca75a49617903650a4d4e8f6975cb13e
[akkoma] / lib / pleroma / user / search.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.User.Search do
6 alias Pleroma.Pagination
7 alias Pleroma.User
8 import Ecto.Query
9
10 @limit 20
11
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)
17
18 for_user = Keyword.get(opts, :for_user)
19
20 query_string = format_query(query_string)
21
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)
25
26 top_user_ids =
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]
31 _ -> []
32 end
33
34 results =
35 query_string
36 |> search_query(for_user, following, top_user_ids)
37 |> Pagination.fetch_paginated(%{"offset" => offset, "limit" => result_limit}, :offset)
38
39 results
40 end
41
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, "@")
45
46 with [name, domain] <- String.split(query_string, "@") do
47 encoded_domain =
48 domain
49 |> String.replace(~r/[!-\-|@|[-`|{-~|\/|:|\s]+/, "")
50 |> String.to_charlist()
51 |> :idna.encode()
52 |> to_string()
53
54 name <> "@" <> encoded_domain
55 else
56 _ -> query_string
57 end
58 end
59
60 defp search_query(query_string, for_user, following, top_user_ids) do
61 for_user
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)
72 |> subquery()
73 |> order_by(desc: :search_rank)
74 |> maybe_restrict_local(for_user)
75 end
76
77 defp select_top_users(query, top_user_ids) do
78 from(u in query,
79 or_where: u.id in ^top_user_ids
80 )
81 end
82
83 defp fts_search(query, query_string) do
84 query_string = to_tsquery(query_string)
85
86 from(
87 u in query,
88 where:
89 fragment(
90 # The fragment must _exactly_ match `users_fts_index`, otherwise the index won't work
91 """
92 (
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', ?)
96 """,
97 u.nickname,
98 u.name,
99 ^query_string
100 )
101 )
102 end
103
104 defp to_tsquery(query_string) do
105 String.trim_trailing(query_string, "@" <> local_domain())
106 |> String.replace(~r/[!-\/|@|[-`|{-~|:-?]+/, " ")
107 |> String.trim()
108 |> String.split()
109 |> Enum.map(&(&1 <> ":*"))
110 |> Enum.join(" | ")
111 end
112
113 # Considers nickname match, localized nickname match, name match; preferences nickname match
114 defp trigram_rank(query, query_string) do
115 from(
116 u in query,
117 select_merge: %{
118 search_rank:
119 fragment(
120 """
121 similarity(?, ?) +
122 similarity(?, regexp_replace(?, '@.+', '')) +
123 similarity(?, trim(coalesce(?, '')))
124 """,
125 ^query_string,
126 u.nickname,
127 ^query_string,
128 u.nickname,
129 ^query_string,
130 u.name
131 )
132 }
133 )
134 end
135
136 defp base_query(%User{} = user, true), do: User.get_friends_query(user)
137 defp base_query(_user, _following), do: User
138
139 defp filter_invisible_users(query) do
140 from(q in query, where: q.invisible == false)
141 end
142
143 defp filter_discoverable_users(query) do
144 from(q in query, where: q.discoverable == true)
145 end
146
147 defp filter_internal_users(query) do
148 from(q in query, where: q.actor_type != "Application")
149 end
150
151 defp filter_blocked_user(query, %User{} = blocker) do
152 query
153 |> join(:left, [u], b in Pleroma.UserRelationship,
154 as: :blocks,
155 on: b.relationship_type == ^:block and b.source_id == ^blocker.id and u.id == b.target_id
156 )
157 |> where([blocks: b], is_nil(b.target_id))
158 end
159
160 defp filter_blocked_user(query, _), do: query
161
162 defp filter_blocked_domains(query, %User{domain_blocks: domain_blocks})
163 when length(domain_blocks) > 0 do
164 domains = Enum.join(domain_blocks, ",")
165
166 from(
167 q in query,
168 where: fragment("substring(ap_id from '.*://([^/]*)') NOT IN (?)", ^domains)
169 )
170 end
171
172 defp filter_blocked_domains(query, _), do: query
173
174 defp maybe_resolve(true, user, query) do
175 case {limit(), user} do
176 {:all, _} -> :noop
177 {:unauthenticated, %User{}} -> User.get_or_fetch(query)
178 {:unauthenticated, _} -> :noop
179 {false, _} -> User.get_or_fetch(query)
180 end
181 end
182
183 defp maybe_resolve(_, _, _), do: :noop
184
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)
190 {false, _} -> q
191 end
192 end
193
194 defp limit, do: Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
195
196 defp restrict_local(q), do: where(q, [u], u.local == true)
197
198 defp local_domain, do: Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
199
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)
203
204 from(u in subquery(query),
205 select_merge: %{
206 search_rank:
207 fragment(
208 """
209 CASE WHEN (?) THEN (?) * 1.5
210 WHEN (?) THEN (?) * 1.3
211 WHEN (?) THEN (?) * 1.1
212 WHEN (?) THEN 9001
213 ELSE (?) END
214 """,
215 u.id in ^friends_ids and u.id in ^followers_ids,
216 u.search_rank,
217 u.id in ^friends_ids,
218 u.search_rank,
219 u.id in ^followers_ids,
220 u.search_rank,
221 u.id in ^top_user_ids,
222 u.search_rank
223 )
224 }
225 )
226 end
227
228 defp boost_search_rank(query, _for_user, top_user_ids) do
229 from(u in subquery(query),
230 select_merge: %{
231 search_rank:
232 fragment(
233 """
234 CASE WHEN (?) THEN 9001
235 ELSE (?) END
236 """,
237 u.id in ^top_user_ids,
238 u.search_rank
239 )
240 }
241 )
242 end
243 end