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