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 defp fts_search(query, query_string) do
58 query_string = to_tsquery(query_string)
65 (to_tsvector('simple', ?) || to_tsvector('simple', ?)) @@ to_tsquery('simple', ?)
74 defp to_tsquery(query_string) do
75 String.trim_trailing(query_string, "@" <> local_domain())
76 |> String.replace(~r/[!-\/|@|[-`|{-~|:-?]+/, " ")
79 |> Enum.map(&(&1 <> ":*"))
83 defp trigram_rank(query, query_string) do
89 "similarity(?, trim(? || ' ' || coalesce(?, '')))",
98 defp base_query(_user, false), do: User
99 defp base_query(user, true), do: User.get_followers_query(user)
101 defp filter_blocked_user(query, %User{} = blocker) do
103 |> join(:left, [u], b in Pleroma.UserRelationship,
105 on: b.relationship_type == ^:block and b.source_id == ^blocker.id and u.id == b.target_id
107 |> where([blocks: b], is_nil(b.target_id))
110 defp filter_blocked_user(query, _), do: query
112 defp filter_blocked_domains(query, %User{domain_blocks: domain_blocks})
113 when length(domain_blocks) > 0 do
114 domains = Enum.join(domain_blocks, ",")
118 where: fragment("substring(ap_id from '.*://([^/]*)') NOT IN (?)", ^domains)
122 defp filter_blocked_domains(query, _), do: query
124 defp maybe_resolve(true, user, query) do
125 case {limit(), user} do
127 {:unauthenticated, %User{}} -> User.get_or_fetch(query)
128 {:unauthenticated, _} -> :noop
129 {false, _} -> User.get_or_fetch(query)
133 defp maybe_resolve(_, _, _), do: :noop
135 defp maybe_restrict_local(q, user) do
136 case {limit(), user} do
137 {:all, _} -> restrict_local(q)
138 {:unauthenticated, %User{}} -> q
139 {:unauthenticated, _} -> restrict_local(q)
144 defp limit, do: Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
146 defp restrict_local(q), do: where(q, [u], u.local == true)
148 defp local_domain, do: Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
150 defp boost_search_rank(query, %User{} = for_user) do
151 friends_ids = User.get_friends_ids(for_user)
152 followers_ids = User.get_followers_ids(for_user)
154 from(u in subquery(query),
159 CASE WHEN (?) THEN (?) * 1.5
160 WHEN (?) THEN (?) * 1.3
161 WHEN (?) THEN (?) * 1.1
164 u.id in ^friends_ids and u.id in ^followers_ids,
166 u.id in ^friends_ids,
168 u.id in ^followers_ids,
176 defp boost_search_rank(query, _for_user), do: query