1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Search.DatabaseSearch do
7 alias Pleroma.Object.Fetcher
8 alias Pleroma.Pagination
10 alias Pleroma.Web.ActivityPub.Visibility
12 require Pleroma.Constants
16 @behaviour Pleroma.Search.SearchBackend
18 def search(user, search_query, options \\ []) do
19 index_type = if Pleroma.Config.get([:database, :rum_enabled]), do: :rum, else: :gin
20 limit = Enum.min([Keyword.get(options, :limit), 40])
21 offset = Keyword.get(options, :offset, 0)
22 author = Keyword.get(options, :author)
25 if :persistent_term.get({Pleroma.Repo, :postgres_version}) >= 11 do
33 |> Activity.with_preloaded_object()
34 |> Activity.restrict_deactivated_users()
36 |> query_with(index_type, search_query, search_function)
37 |> maybe_restrict_local(user)
38 |> maybe_restrict_author(author)
39 |> maybe_restrict_blocked(user)
40 |> Pagination.fetch_paginated(
41 %{"offset" => offset, "limit" => limit, "skip_order" => index_type == :rum},
44 |> maybe_fetch(user, search_query)
46 _ -> maybe_fetch([], user, search_query)
51 def add_to_index(_activity), do: nil
54 def remove_from_index(_object), do: nil
56 def maybe_restrict_author(query, %User{} = author) do
57 Activity.Queries.by_author(query, author)
60 def maybe_restrict_author(query, _), do: query
62 def maybe_restrict_blocked(query, %User{} = user) do
63 Activity.Queries.exclude_authors(query, User.blocked_users_ap_ids(user))
66 def maybe_restrict_blocked(query, _), do: query
68 def restrict_public(q) do
70 where: fragment("?->>'type' = 'Create'", a.data),
71 where: ^Pleroma.Constants.as_public() in a.recipients
75 defp query_with(q, :gin, search_query, :plain) do
77 Ecto.Adapters.SQL.query!(
79 "select current_setting('default_text_search_config')::regconfig::oid;"
85 "to_tsvector(?::oid::regconfig, ?->>'content') @@ plainto_tsquery(?)",
93 defp query_with(q, :gin, search_query, :websearch) do
95 Ecto.Adapters.SQL.query!(
97 "select current_setting('default_text_search_config')::regconfig::oid;"
103 "to_tsvector(?::oid::regconfig, ?->>'content') @@ websearch_to_tsquery(?)",
111 defp query_with(q, :rum, search_query, :plain) do
115 "? @@ plainto_tsquery(?)",
119 order_by: [fragment("? <=> now()::date", o.inserted_at)]
123 defp query_with(q, :rum, search_query, :websearch) do
127 "? @@ websearch_to_tsquery(?)",
131 order_by: [fragment("? <=> now()::date", o.inserted_at)]
135 def maybe_restrict_local(q, user) do
136 limit = Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
138 case {limit, user} do
139 {:all, _} -> restrict_local(q)
140 {:unauthenticated, %User{}} -> q
141 {:unauthenticated, _} -> restrict_local(q)
146 defp restrict_local(q), do: where(q, local: true)
148 def maybe_fetch(activities, user, search_query) do
149 with true <- Regex.match?(~r/https?:/, search_query),
150 {:ok, object} <- Fetcher.fetch_object_from_id(search_query),
151 %Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
152 true <- Visibility.visible_for_user?(activity, user) do
153 [activity | activities]