Merge branch 'fix/no-email-no-fail' into '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_blocked_domains(for_user)
56 |> fts_search(query_string)
57 |> trigram_rank(query_string)
58 |> boost_search_rank(for_user)
59 |> subquery()
60 |> order_by(desc: :search_rank)
61 |> maybe_restrict_local(for_user)
62 end
63
64 defp fts_search(query, query_string) do
65 query_string = to_tsquery(query_string)
66
67 from(
68 u in query,
69 where:
70 fragment(
71 """
72 (to_tsvector('simple', ?) || to_tsvector('simple', ?)) @@ to_tsquery('simple', ?)
73 """,
74 u.name,
75 u.nickname,
76 ^query_string
77 )
78 )
79 end
80
81 defp to_tsquery(query_string) do
82 String.trim_trailing(query_string, "@" <> local_domain())
83 |> String.replace(~r/[!-\/|@|[-`|{-~|:-?]+/, " ")
84 |> String.trim()
85 |> String.split()
86 |> Enum.map(&(&1 <> ":*"))
87 |> Enum.join(" | ")
88 end
89
90 defp trigram_rank(query, query_string) do
91 from(
92 u in query,
93 select_merge: %{
94 search_rank:
95 fragment(
96 "similarity(?, trim(? || ' ' || coalesce(?, '')))",
97 ^query_string,
98 u.nickname,
99 u.name
100 )
101 }
102 )
103 end
104
105 defp base_query(_user, false), do: User
106 defp base_query(user, true), do: User.get_followers_query(user)
107
108 defp filter_invisible_users(query) do
109 from(q in query, where: q.invisible == false)
110 end
111
112 defp filter_blocked_user(query, %User{} = blocker) do
113 query
114 |> join(:left, [u], b in Pleroma.UserRelationship,
115 as: :blocks,
116 on: b.relationship_type == ^:block and b.source_id == ^blocker.id and u.id == b.target_id
117 )
118 |> where([blocks: b], is_nil(b.target_id))
119 end
120
121 defp filter_blocked_user(query, _), do: query
122
123 defp filter_blocked_domains(query, %User{domain_blocks: domain_blocks})
124 when length(domain_blocks) > 0 do
125 domains = Enum.join(domain_blocks, ",")
126
127 from(
128 q in query,
129 where: fragment("substring(ap_id from '.*://([^/]*)') NOT IN (?)", ^domains)
130 )
131 end
132
133 defp filter_blocked_domains(query, _), do: query
134
135 defp maybe_resolve(true, user, query) do
136 case {limit(), user} do
137 {:all, _} -> :noop
138 {:unauthenticated, %User{}} -> User.get_or_fetch(query)
139 {:unauthenticated, _} -> :noop
140 {false, _} -> User.get_or_fetch(query)
141 end
142 end
143
144 defp maybe_resolve(_, _, _), do: :noop
145
146 defp maybe_restrict_local(q, user) do
147 case {limit(), user} do
148 {:all, _} -> restrict_local(q)
149 {:unauthenticated, %User{}} -> q
150 {:unauthenticated, _} -> restrict_local(q)
151 {false, _} -> q
152 end
153 end
154
155 defp limit, do: Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
156
157 defp restrict_local(q), do: where(q, [u], u.local == true)
158
159 defp local_domain, do: Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
160
161 defp boost_search_rank(query, %User{} = for_user) do
162 friends_ids = User.get_friends_ids(for_user)
163 followers_ids = User.get_followers_ids(for_user)
164
165 from(u in subquery(query),
166 select_merge: %{
167 search_rank:
168 fragment(
169 """
170 CASE WHEN (?) THEN (?) * 1.5
171 WHEN (?) THEN (?) * 1.3
172 WHEN (?) THEN (?) * 1.1
173 ELSE (?) END
174 """,
175 u.id in ^friends_ids and u.id in ^followers_ids,
176 u.search_rank,
177 u.id in ^friends_ids,
178 u.search_rank,
179 u.id in ^followers_ids,
180 u.search_rank,
181 u.search_rank
182 )
183 }
184 )
185 end
186
187 defp boost_search_rank(query, _for_user), do: query
188 end