Merge develop
[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 maybe_resolve(resolve, for_user, query_string)
23
24 results =
25 query_string
26 |> search_query(for_user, following)
27 |> Pagination.fetch_paginated(%{"offset" => offset, "limit" => result_limit}, :offset)
28
29 results
30 end
31
32 defp format_query(query_string) do
33 # Strip the beginning @ off if there is a query
34 query_string = String.trim_leading(query_string, "@")
35
36 with [name, domain] <- String.split(query_string, "@") do
37 encoded_domain =
38 domain
39 |> String.replace(~r/[!-\-|@|[-`|{-~|\/|:|\s]+/, "")
40 |> String.to_charlist()
41 |> :idna.encode()
42 |> to_string()
43
44 name <> "@" <> encoded_domain
45 else
46 _ -> query_string
47 end
48 end
49
50 defp search_query(query_string, for_user, following) do
51 for_user
52 |> base_query(following)
53 |> filter_blocked_user(for_user)
54 |> filter_invisible_users()
55 |> filter_internal_users()
56 |> filter_blocked_domains(for_user)
57 |> fts_search(query_string)
58 |> trigram_rank(query_string)
59 |> boost_search_rank(for_user)
60 |> subquery()
61 |> order_by(desc: :search_rank)
62 |> maybe_restrict_local(for_user)
63 end
64
65 defp fts_search(query, query_string) do
66 query_string = to_tsquery(query_string)
67
68 from(
69 u in query,
70 where:
71 fragment(
72 # The fragment must _exactly_ match `users_fts_index`, otherwise the index won't work
73 """
74 (
75 setweight(to_tsvector('simple', regexp_replace(?, '\\W', ' ', 'g')), 'A') ||
76 setweight(to_tsvector('simple', regexp_replace(coalesce(?, ''), '\\W', ' ', 'g')), 'B')
77 ) @@ to_tsquery('simple', ?)
78 """,
79 u.nickname,
80 u.name,
81 ^query_string
82 )
83 )
84 end
85
86 defp to_tsquery(query_string) do
87 String.trim_trailing(query_string, "@" <> local_domain())
88 |> String.replace(~r/[!-\/|@|[-`|{-~|:-?]+/, " ")
89 |> String.trim()
90 |> String.split()
91 |> Enum.map(&(&1 <> ":*"))
92 |> Enum.join(" | ")
93 end
94
95 # Considers nickname match, localized nickname match, name match; preferences nickname match
96 defp trigram_rank(query, query_string) do
97 from(
98 u in query,
99 select_merge: %{
100 search_rank:
101 fragment(
102 """
103 similarity(?, ?) +
104 similarity(?, regexp_replace(?, '@.+', '')) +
105 similarity(?, trim(coalesce(?, '')))
106 """,
107 ^query_string,
108 u.nickname,
109 ^query_string,
110 u.nickname,
111 ^query_string,
112 u.name
113 )
114 }
115 )
116 end
117
118 defp base_query(%User{} = user, true), do: User.get_friends_query(user)
119 defp base_query(_user, _following), do: User
120
121 defp filter_invisible_users(query) do
122 from(q in query, where: q.invisible == false)
123 end
124
125 defp filter_internal_users(query) do
126 from(q in query, where: q.actor_type != "Application")
127 end
128
129 defp filter_blocked_user(query, %User{} = blocker) do
130 query
131 |> join(:left, [u], b in Pleroma.UserRelationship,
132 as: :blocks,
133 on: b.relationship_type == ^:block and b.source_id == ^blocker.id and u.id == b.target_id
134 )
135 |> where([blocks: b], is_nil(b.target_id))
136 end
137
138 defp filter_blocked_user(query, _), do: query
139
140 defp filter_blocked_domains(query, %User{domain_blocks: domain_blocks})
141 when length(domain_blocks) > 0 do
142 domains = Enum.join(domain_blocks, ",")
143
144 from(
145 q in query,
146 where: fragment("substring(ap_id from '.*://([^/]*)') NOT IN (?)", ^domains)
147 )
148 end
149
150 defp filter_blocked_domains(query, _), do: query
151
152 defp maybe_resolve(true, user, query) do
153 case {limit(), user} do
154 {:all, _} -> :noop
155 {:unauthenticated, %User{}} -> User.get_or_fetch(query)
156 {:unauthenticated, _} -> :noop
157 {false, _} -> User.get_or_fetch(query)
158 end
159 end
160
161 defp maybe_resolve(_, _, _), do: :noop
162
163 defp maybe_restrict_local(q, user) do
164 case {limit(), user} do
165 {:all, _} -> restrict_local(q)
166 {:unauthenticated, %User{}} -> q
167 {:unauthenticated, _} -> restrict_local(q)
168 {false, _} -> q
169 end
170 end
171
172 defp limit, do: Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
173
174 defp restrict_local(q), do: where(q, [u], u.local == true)
175
176 defp local_domain, do: Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
177
178 defp boost_search_rank(query, %User{} = for_user) do
179 friends_ids = User.get_friends_ids(for_user)
180 followers_ids = User.get_followers_ids(for_user)
181
182 from(u in subquery(query),
183 select_merge: %{
184 search_rank:
185 fragment(
186 """
187 CASE WHEN (?) THEN (?) * 1.5
188 WHEN (?) THEN (?) * 1.3
189 WHEN (?) THEN (?) * 1.1
190 ELSE (?) END
191 """,
192 u.id in ^friends_ids and u.id in ^followers_ids,
193 u.search_rank,
194 u.id in ^friends_ids,
195 u.search_rank,
196 u.id in ^followers_ids,
197 u.search_rank,
198 u.search_rank
199 )
200 }
201 )
202 end
203
204 defp boost_search_rank(query, _for_user), do: query
205 end