fb2f3fedbf627e79cbfe33014062697f8cfb7a19
[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 @similarity_threshold 0.25
11 @limit 20
12
13 def search(query_string, opts \\ []) do
14 resolve = Keyword.get(opts, :resolve, false)
15 following = Keyword.get(opts, :following, false)
16 result_limit = Keyword.get(opts, :limit, @limit)
17 offset = Keyword.get(opts, :offset, 0)
18
19 for_user = Keyword.get(opts, :for_user)
20
21 query_string = format_query(query_string)
22
23 maybe_resolve(resolve, for_user, query_string)
24
25 results =
26 query_string
27 |> search_query(for_user, following)
28 |> Pagination.fetch_paginated(%{"offset" => offset, "limit" => result_limit}, :offset)
29
30 results
31 end
32
33 defp format_query(query_string) do
34 # Strip the beginning @ off if there is a query
35 query_string = String.trim_leading(query_string, "@")
36
37 with [name, domain] <- String.split(query_string, "@"),
38 formatted_domain <- String.replace(domain, ~r/[!-\-|@|[-`|{-~|\/|:|\s]+/, "") do
39 name <> "@" <> to_string(:idna.encode(formatted_domain))
40 else
41 _ -> query_string
42 end
43 end
44
45 defp search_query(query_string, for_user, following) do
46 for_user
47 |> base_query(following)
48 |> filter_blocked_user(for_user)
49 |> filter_blocked_domains(for_user)
50 |> fts_subquery(query_string)
51 |> subquery()
52 |> where([u], u.search_rank > @similarity_threshold)
53 |> boost_search_rank(for_user)
54 |> order_by(desc: :search_rank)
55 |> maybe_restrict_local(for_user)
56 end
57
58 @nickname_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~\-@]+$/
59 defp fts_subquery(query, query_string) do
60 {nickname_weight, name_weight} =
61 if String.match?(query_string, @nickname_regex) do
62 {"A", "B"}
63 else
64 {"B", "A"}
65 end
66
67 query_string = to_tsquery(query_string)
68
69 from(
70 u in query,
71 select_merge: %{
72 search_rank:
73 fragment(
74 """
75 ts_rank_cd((setweight(to_tsvector('simple', ?), ?) || setweight(to_tsvector('simple', ?), ?)), to_tsquery('simple', ?))
76 """,
77 u.name,
78 ^name_weight,
79 u.nickname,
80 ^nickname_weight,
81 ^query_string
82 )
83 }
84 )
85 end
86
87 defp to_tsquery(query_string) do
88 String.trim_trailing(query_string, "@" <> local_domain())
89 |> String.replace(~r/[!-\/|@|[-`|{-~|:-?]+/, " ")
90 |> String.trim()
91 |> String.split()
92 |> Enum.map(&(&1 <> ":*"))
93 |> Enum.join(" | ")
94 end
95
96 defp base_query(_user, false), do: User
97 defp base_query(user, true), do: User.get_followers_query(user)
98
99 defp filter_blocked_user(query, %User{info: %{blocks: blocks}})
100 when length(blocks) > 0 do
101 from(q in query, where: not (q.ap_id in ^blocks))
102 end
103
104 defp filter_blocked_user(query, _), do: query
105
106 defp filter_blocked_domains(query, %User{info: %{domain_blocks: domain_blocks}})
107 when length(domain_blocks) > 0 do
108 domains = Enum.join(domain_blocks, ",")
109
110 from(
111 q in query,
112 where: fragment("substring(ap_id from '.*://([^/]*)') NOT IN (?)", ^domains)
113 )
114 end
115
116 defp filter_blocked_domains(query, _), do: query
117
118 defp maybe_resolve(true, user, query) do
119 case {limit(), user} do
120 {:all, _} -> :noop
121 {:unauthenticated, %User{}} -> User.get_or_fetch(query)
122 {:unauthenticated, _} -> :noop
123 {false, _} -> User.get_or_fetch(query)
124 end
125 end
126
127 defp maybe_resolve(_, _, _), do: :noop
128
129 defp maybe_restrict_local(q, user) do
130 case {limit(), user} do
131 {:all, _} -> restrict_local(q)
132 {:unauthenticated, %User{}} -> q
133 {:unauthenticated, _} -> restrict_local(q)
134 {false, _} -> q
135 end
136 end
137
138 defp limit, do: Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
139
140 defp restrict_local(q), do: where(q, [u], u.local == true)
141
142 defp local_domain, do: Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
143
144 defp boost_search_rank(query, %User{} = for_user) do
145 friends_ids = User.get_friends_ids(for_user)
146 followers_ids = User.get_followers_ids(for_user)
147
148 from(u in subquery(query),
149 select_merge: %{
150 search_rank:
151 fragment(
152 """
153 CASE WHEN (?) THEN (?) * 1.5
154 WHEN (?) THEN (?) * 1.3
155 WHEN (?) THEN (?) * 1.1
156 ELSE (?) END
157 """,
158 u.id in ^friends_ids and u.id in ^followers_ids,
159 u.search_rank,
160 u.id in ^friends_ids,
161 u.search_rank,
162 u.id in ^followers_ids,
163 u.search_rank,
164 u.search_rank
165 )
166 }
167 )
168 end
169
170 defp boost_search_rank(query, _for_user), do: query
171 end