Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / lib / pleroma / user / search.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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, "@"),
37 formatted_domain <- String.replace(domain, ~r/[!-\-|@|[-`|{-~|\/|:|\s]+/, "") do
38 name <> "@" <> to_string(:idna.encode(formatted_domain))
39 else
40 _ -> query_string
41 end
42 end
43
44 defp search_query(query_string, for_user, following) do
45 for_user
46 |> base_query(following)
47 |> filter_blocked_user(for_user)
48 |> filter_blocked_domains(for_user)
49 |> fts_search(query_string)
50 |> trigram_rank(query_string)
51 |> boost_search_rank(for_user)
52 |> subquery()
53 |> order_by(desc: :search_rank)
54 |> maybe_restrict_local(for_user)
55 end
56
57 @nickname_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~\-@]+$/
58 defp fts_search(query, query_string) do
59 {nickname_weight, name_weight} =
60 if String.match?(query_string, @nickname_regex) do
61 {"A", "B"}
62 else
63 {"B", "A"}
64 end
65
66 query_string = to_tsquery(query_string)
67
68 from(
69 u in query,
70 where:
71 fragment(
72 """
73 (setweight(to_tsvector('simple', ?), ?) || setweight(to_tsvector('simple', ?), ?)) @@ to_tsquery('simple', ?)
74 """,
75 u.name,
76 ^name_weight,
77 u.nickname,
78 ^nickname_weight,
79 ^query_string
80 )
81 )
82 end
83
84 defp to_tsquery(query_string) do
85 String.trim_trailing(query_string, "@" <> local_domain())
86 |> String.replace(~r/[!-\/|@|[-`|{-~|:-?]+/, " ")
87 |> String.trim()
88 |> String.split()
89 |> Enum.map(&(&1 <> ":*"))
90 |> Enum.join(" | ")
91 end
92
93 defp trigram_rank(query, query_string) do
94 from(
95 u in query,
96 select_merge: %{
97 search_rank:
98 fragment(
99 "similarity(?, trim(? || ' ' || coalesce(?, '')))",
100 ^query_string,
101 u.nickname,
102 u.name
103 )
104 }
105 )
106 end
107
108 defp base_query(_user, false), do: User
109 defp base_query(user, true), do: User.get_followers_query(user)
110
111 defp filter_blocked_user(query, %User{blocks: blocks})
112 when length(blocks) > 0 do
113 from(q in query, where: not (q.ap_id in ^blocks))
114 end
115
116 defp filter_blocked_user(query, _), do: query
117
118 defp filter_blocked_domains(query, %User{domain_blocks: domain_blocks})
119 when length(domain_blocks) > 0 do
120 domains = Enum.join(domain_blocks, ",")
121
122 from(
123 q in query,
124 where: fragment("substring(ap_id from '.*://([^/]*)') NOT IN (?)", ^domains)
125 )
126 end
127
128 defp filter_blocked_domains(query, _), do: query
129
130 defp maybe_resolve(true, user, query) do
131 case {limit(), user} do
132 {:all, _} -> :noop
133 {:unauthenticated, %User{}} -> User.get_or_fetch(query)
134 {:unauthenticated, _} -> :noop
135 {false, _} -> User.get_or_fetch(query)
136 end
137 end
138
139 defp maybe_resolve(_, _, _), do: :noop
140
141 defp maybe_restrict_local(q, user) do
142 case {limit(), user} do
143 {:all, _} -> restrict_local(q)
144 {:unauthenticated, %User{}} -> q
145 {:unauthenticated, _} -> restrict_local(q)
146 {false, _} -> q
147 end
148 end
149
150 defp limit, do: Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
151
152 defp restrict_local(q), do: where(q, [u], u.local == true)
153
154 defp local_domain, do: Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
155
156 defp boost_search_rank(query, %User{} = for_user) do
157 friends_ids = User.get_friends_ids(for_user)
158 followers_ids = User.get_followers_ids(for_user)
159
160 from(u in subquery(query),
161 select_merge: %{
162 search_rank:
163 fragment(
164 """
165 CASE WHEN (?) THEN (?) * 1.5
166 WHEN (?) THEN (?) * 1.3
167 WHEN (?) THEN (?) * 1.1
168 ELSE (?) END
169 """,
170 u.id in ^friends_ids and u.id in ^followers_ids,
171 u.search_rank,
172 u.id in ^friends_ids,
173 u.search_rank,
174 u.id in ^followers_ids,
175 u.search_rank,
176 u.search_rank
177 )
178 }
179 )
180 end
181
182 defp boost_search_rank(query, _for_user), do: query
183 end