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
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)
18 for_user = Keyword.get(opts, :for_user)
20 query_string = format_query(query_string)
22 maybe_resolve(resolve, for_user, query_string)
26 |> search_query(for_user, following)
27 |> Pagination.fetch_paginated(%{"offset" => offset, "limit" => result_limit}, :offset)
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, "@")
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))
44 defp search_query(query_string, for_user, following) do
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)
53 |> order_by(desc: :search_rank)
54 |> maybe_restrict_local(for_user)
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
66 query_string = to_tsquery(query_string)
73 (setweight(to_tsvector('simple', ?), ?) || setweight(to_tsvector('simple', ?), ?)) @@ to_tsquery('simple', ?)
84 defp to_tsquery(query_string) do
85 String.trim_trailing(query_string, "@" <> local_domain())
86 |> String.replace(~r/[!-\/|@|[-`|{-~|:-?]+/, " ")
89 |> Enum.map(&(&1 <> ":*"))
93 defp trigram_rank(query, query_string) do
99 "similarity(?, trim(? || ' ' || coalesce(?, '')))",
108 defp base_query(_user, false), do: User
109 defp base_query(user, true), do: User.get_followers_query(user)
111 defp filter_blocked_user(query, %User{info: %{blocks: blocks}})
112 when length(blocks) > 0 do
113 from(q in query, where: not (q.ap_id in ^blocks))
116 defp filter_blocked_user(query, _), do: query
118 defp filter_blocked_domains(query, %User{info: %{domain_blocks: domain_blocks}})
119 when length(domain_blocks) > 0 do
120 domains = Enum.join(domain_blocks, ",")
124 where: fragment("substring(ap_id from '.*://([^/]*)') NOT IN (?)", ^domains)
128 defp filter_blocked_domains(query, _), do: query
130 defp maybe_resolve(true, user, query) do
131 case {limit(), user} do
133 {:unauthenticated, %User{}} -> User.get_or_fetch(query)
134 {:unauthenticated, _} -> :noop
135 {false, _} -> User.get_or_fetch(query)
139 defp maybe_resolve(_, _, _), do: :noop
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)
150 defp limit, do: Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
152 defp restrict_local(q), do: where(q, [u], u.local == true)
154 defp local_domain, do: Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
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)
160 from(u in subquery(query),
165 CASE WHEN (?) THEN (?) * 1.5
166 WHEN (?) THEN (?) * 1.3
167 WHEN (?) THEN (?) * 1.1
170 u.id in ^friends_ids and u.id in ^followers_ids,
172 u.id in ^friends_ids,
174 u.id in ^followers_ids,
182 defp boost_search_rank(query, _for_user), do: query