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