1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.User.Search do
6 alias Pleroma.Pagination
10 @similarity_threshold 0.25
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)
19 for_user = Keyword.get(opts, :for_user)
21 query_string = format_query(query_string)
23 maybe_resolve(resolve, for_user, query_string)
27 |> search_query(for_user, following)
28 |> Pagination.fetch_paginated(%{"offset" => offset, "limit" => result_limit}, :offset)
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, "@")
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))
45 defp search_query(query_string, for_user, following) do
47 |> base_query(following)
48 |> filter_blocked_user(for_user)
49 |> filter_blocked_domains(for_user)
50 |> fts_subquery(query_string)
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)
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
67 query_string = to_tsquery(query_string)
75 ts_rank_cd((setweight(to_tsvector('simple', ?), ?) || setweight(to_tsvector('simple', ?), ?)), to_tsquery('simple', ?))
87 defp to_tsquery(query_string) do
88 String.trim_trailing(query_string, "@" <> local_domain())
89 |> String.replace(~r/[!-\/|@|[-`|{-~|:-?]+/, " ")
92 |> Enum.map(&(&1 <> ":*"))
96 defp base_query(_user, false), do: User
97 defp base_query(user, true), do: User.get_followers_query(user)
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))
104 defp filter_blocked_user(query, _), do: query
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, ",")
112 where: fragment("substring(ap_id from '.*://([^/]*)') NOT IN (?)", ^domains)
116 defp filter_blocked_domains(query, _), do: query
118 defp maybe_resolve(true, user, query) do
119 case {limit(), user} do
121 {:unauthenticated, %User{}} -> User.get_or_fetch(query)
122 {:unauthenticated, _} -> :noop
123 {false, _} -> User.get_or_fetch(query)
127 defp maybe_resolve(_, _, _), do: :noop
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)
138 defp limit, do: Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
140 defp restrict_local(q), do: where(q, [u], u.local == true)
142 defp local_domain, do: Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
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)
148 from(u in subquery(query),
153 CASE WHEN (?) THEN (?) * 1.5
154 WHEN (?) THEN (?) * 1.3
155 WHEN (?) THEN (?) * 1.1
158 u.id in ^friends_ids and u.id in ^followers_ids,
160 u.id in ^friends_ids,
162 u.id in ^followers_ids,
170 defp boost_search_rank(query, _for_user), do: query